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

Weekly challenge 3 data analyst google professional certificate

1 . Question 1 The manage stage of the data life cycle is when a business decides what kind of data it needs, how the data will be handled, and who will be responsible for it. 1 / 1  point True False Correct During planning, a business decides what kind of data it needs, how it will be managed throughout its life cycle, who will be responsible for it, and the optimal outcomes. 2 . Question 2 A data analyst is working at a small tech startup. They’ve just completed an analysis project, which involved private company information about a new product launch. In order to keep the information safe, the analyst uses secure data-erasure software for the digital files and a shredder for the paper files. Which stage of the data life cycle does this describe? 1 / 1  point Archive Plan Manage Destroy Correct This describes the destroy phase, during which data analysts use secure data-erasure software and shred paper files to protect private information. 3 . Question 3 In the analyze phase of the d

Prepare Data for Exploration: Weekly challenge 4

Prepare Data for Exploration: Weekly challenge 4 1 . Question 1 A data analytics team labels its files to indicate their content, creation date, and version number. The team is using what data organization tool? 1 / 1  point File-naming verifications File-naming references File-naming conventions File-naming attributes Correct 2 . Question 2 Your boss assigns you a new multi-phase project and you create a naming convention for all of your files. With this project lasting years and incorporating multiple analysts it’s crucial that you create data explaining how your naming conventions are structured. What is this data called? 0 / 1  point Descriptive data Named convention Metadata Labeled data Incorrect Please review the video on naming conventions . 3 . Question 3 A grocery store is collecting inventory data from their produce section. What is an appropriate naming convention for this file? 0 / 1  point Todays_Produce Produce_Inventory_2022-09-15_V01 Todays Produce 2022-15-09 Inventory

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