JavaScript Loops

Loops are used to iterate and execute group of statements under the certain number of times and condition. In Javascript there are many statement are available that are define loop statement and control the execution of loop. But loop execution are based on particular condition. When condition are true then loop execution are possible otherwise loop are not executed statements.

For Loop

For-loop are used to iterate group of statement under certain situations. There are three version are available in Javascript.

for (initialization; termination;increment/decrement) {
    //statement's
}
for (variable in object) {
//code here
}
for (variable of iterable)) {
//code here
}

While Loop

While loop is executing statement under certain condition expression. When expression result are true then this loop execute statements. Otherwise this are terminate the loop execution process.

while(Condition){
//loop statements (body of loop)
//update Expression Condition
}

Do while

do-while loops are first executing given loop statement, instruction. And after that this loop are check terminating condition at the end. The main advantage of this loop that, it will be execute loop statement at least one time. When inside loop are not provide other terminate condition.

do{
//statements
//Update Expression Condition
}while(Expression);

Loop Control Statement

Conditional statement normally control loop statement execution flow But that is not control loop execution (terminate and continue execution). Loop statements such as continue and break are manage loop execution process within the loop.

Continue Statement

continue statement are used to change execution follow of loop. This statement are used in inside a loop in specified condition. When continue statement are executed they are not execute other next remaining statement of loop body.

for loop continue statement

For example suppose you are iterating an array which are contain n element, If in case element value are equal to false then you are need to use a continue statement.

var array = [1,9,1,false,2,false,true,"One"];

for (let i=0;i<array.length;i++){
	
	if(array[i]==false){
		//when element contain false 
		continue;
	}

	console.log(array[i]);
}
1
9
1
2
true
One

Note that in for loop initialization are occur only once when loop execution are start. When use continue statement inside for-loop. That is first visiting updatedCondition section and after that checking the loop terminating conditions. When condition are reliable to execute loop statement then execute loop statements.

while loop continue statement
var array = [1,9,1,false,2,false,true,"One"];
let i=0;
while (i<array.length){

	if(array[i]==false){
		i++; //note that incrementing the value of (i) by one
		//when element contain false 
		continue;
	}

	console.log(array[i]);
	i++;
}
1
9
1
2
true
One

In while-loop, continue statement execution are back to check loop termination condition. When loop termination condition is false then the loop execution are start again at the top of the loop.

do while continue statement

Then control are back to check loop termination condition if that is valid then executed next upcoming instruction of loop.

var array = [1,9,1,false,2,false,true,"One"];
let i=0;
do{

	if(array[i]==false){
		//when element contain false 
		continue;
	}

	console.log(array[i]);
	
}while (++i<array.length);
1
9
1
2
true
One

Break Statement

break statement are used to terminate execution flow of loop. Especially when inside a loop are get the accepted result and there are some special case when you'll are no need to execute loop statement again.

For-loop break statement

break statement can be define at any point of within the loop with particular condition and without specified condition. That is terminatecurrent loop execution.

While loop break statement

break statement are similar in while and do-while loop.

do while loop break statement


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved