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

How to Set Up Guest Access on Ruckus ZoneDirector – Step-by-Step Guide

 Are you looking to configure guest access on your Ruckus wireless network? In this blog, we’ll take you through the entire process of setting up secure guest access using Ruckus ZoneDirector. Whether you're an IT admin or a network manager, this guide will help you create a BYOD guest WLAN, set up guest pass authentication, and secure your network with wireless client isolation. Step-by-Step Tutorial Includes: Logging into the Ruckus ZoneDirector controller Configuring Guest Access services for BYOD devices Creating a dedicated guest WLAN Using guest pass authentication for added security Isolating guest devices on the network for better privacy Accessing the guest network from a client device By following this tutorial, you'll be able to provide a seamless and secure experience for visitors connecting to your WiFi network. Check out our video tutorial for a detailed walkthrough! #RuckusZoneDirector #GuestAccess #WiFiSetup #BYOD #WLANConfiguration #WirelessNetwork #NetworkSecu...

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