Skip to main content

How To Control A Loop In Javascript

 

How To Control A Loop In Javascript

Let’s discuss on how you can control your loop in javascript program, here we will discuss on the the use of break and continue statements,when you wish to come out of the loop without reaching the bottom or you just wish to skip a part of your code block and start a next iteration of the loop, remember to follow us in our social media platforms for more tutorials and live sessions we hold;
1. Break statement
It is applied when you wish to exit the loop early, lets have a syntax for break statement.
var x=1;
while(x<20)
{
if(x==5){
break; //it breaks out of loop completely
}
x=x=1;
document.write(x+ “<br />”);
}

2. CONTINUE STATEMENT

Continue statement tells the interpreter to skip the remaining code block and start a new iteration immediately. When it is encountered in a program it flows to the top to check the expression immediately and if the condition remains true it starts the iteration otherwise it comes out of the loop. Let’s have a syntax for a continue statement.

var x=1;
while(x<10)
{
x=x+1;
if(x==5){
continue;
}
document.write(x+ "<br />");
}

It will produce;
2
3
4
6
7
8
9
10
Use different variables and values and try it yourself

Using Labels To Control The Flow Of Th Loop

Label- it refers to an identifier followed by a colon, it is applied to a block of code or statement.

-There should not be any line breaks between the break or continue statement and its label name, there should not be any other statement between a label name and associated loop. Let’s have an example.

outerloop: this is the label name
for(var m=0; m<5; m++)
{
document.write("outerloop: "+ m +"<br />");
innerloop:
for(var p=0; p<5; p++)
{
if(p>3)break; //it quits the innermost loop
if(m==2) break innerloop;
if(m==4)break outerloop; //it quits the outerloop
document.write("innerloop:"+p+" <br />");
}
}

Thanks for reading through my tutorial on how to control your looping, next we will discuss very important part of this programming language called FUNCTIONS, ensure you don’t miss out by following our tutorials on our social media platforms.

Comments

Popular posts from this blog

Cyber Attack Countermeasures : Module 4

 Cyber Attack Countermeasures :  Module 4 Quiz #cyber #quiz #coursera #exam #module #answers 1 . Question 1 CBC mode cryptography involves which of the following? 1 / 1  point Mediation of overt channels Mediation of covert channels Auditing of overt channels Auditing of covert channels None of the above Correct Correct! CBC mode is specifically designed to close covert communication channels in block encryption algorithms. 2 . Question 2 Which is a true statement? 1 / 1  point Conventional crypto scales perfectly well Conventional crypto scales poorly to large groups Conventional crypto does not need to scale All of the above Correct Correct! The symmetric key based method inherent in conventional cryptography does not scale well to large groups. 3 . Question 3 Public Key Cryptography involves which of the following? 1 / 1  point Publicly known secret keys Publicly known private keys Publicly known public keys All of the above ...

Cyber Attack Countermeasures : Module 2 Quiz

Cyber Attack Countermeasures: Module 2 Quiz #cyber #quiz #course #era #answer #module 1 . Question 1 “Identification” in the process of authentication involves which of the following? 1 / 1  point Typing a password Keying in a passphrase Typing in User ID and password Typing in User ID None of the above Correct Correct! The definition of identification involves providing a user’s ID (identification). 2 . Question 2 Which of the following statements is true? 1 / 1  point Identifiers are secret Identifiers are not secret Identifiers are the secret part of authentication All of the above Correct Correct! Identifiers for users are generally not viewed by security experts as being secret. 3 . Question 3 Which of the following is not a good candidate for use as a proof factor in the authentication process? 1 / 1  point Making sure the User ID is correct Typing in a correct password Confirming location, regardless of the country you are in The move...

Rectangular Microstrip Patch Antenna

Microstrip is a type of electrical transmission line which can be fabricated using printed circuit board technology, and is used to convey microwave-frequency signals. It consists of a conducting strip separated from a ground plane by a dielectric layer known as the substrate. The most commonly employed microstrip antenna is a rectangular patch which looks like a truncated  microstrip  transmission line. It is approximately of one-half wavelength long. When air is used as the dielectric substrate, the length of the rectangular microstrip antenna is approximately one-half of a free-space  wavelength . As the antenna is loaded with a dielectric as its substrate, the length of the antenna decreases as the relative  dielectric constant  of the substrate increases. The resonant length of the antenna is slightly shorter because of the extended electric "fringing fields" which increase the electrical length of the antenna slightly. An early model of the microst...