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

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...

Prepare Data for Exploration : weekly challenge 1

Prepare Data for Exploration : weekly challenge 1 #coursera #exploration #weekly #challenge 1 #cybersecurity #coursera #quiz #solution #network Are you prepared to increase your data exploration abilities? The goal of Coursera's Week 1 challenge, "Prepare Data for Exploration," is to provide you the skills and resources you need to turn unprocessed data into insightful information. With the knowledge you'll gain from this course, you can ensure that your data is organised, clean, and ready for analysis. Data preparation is one of the most important processes in any data analysis effort. Inaccurate results and flawed conclusions might emerge from poorly prepared data. You may prepare your data for exploration with Coursera's Weekly Challenge 1. You'll discover industry best practises and insider advice. #answers #questions #flashcard 1 . Question 1 What is the most likely reason that a data analyst would use historical data instead of gathering new data? 1 / 1...

Cracking Passwords Using John the Ripper: A Complete Step-by-Step Guide

Cracking Passwords Using John the Ripper: A Complete Step-by-Step Guide In today's post, we’re diving into a practical lab exercise that shows how to use John the Ripper, one of the most effective password-cracking tools in cybersecurity. Whether you're an IT professional or a cybersecurity student, mastering John the Ripper will help you understand password vulnerabilities and enhance your penetration testing skills. Lab Objective: The goal of this lab is to crack the root password on a Linux system (Support) and extract the password from a password-protected ZIP file (located on IT-Laptop). Both tasks are performed using John the Ripper. Steps to Crack the Root Password on Support: Open the Terminal on the Support system. Change directories to /usr/share/john . List the files and open password.lst to view common password guesses. Use John the Ripper to crack the root password by running john /etc/shadow . Once cracked, the password is stored in the john.pot file for future u...