for, while, do…while

for, while, do…while

In JavaScript programming, for, while, and do while loops are used to execute a block of specific program logic code as per the requirement based on a particular program condition. There are mainly three types of program loops in JavaScript programming. Such as, for loop, while loop, and do…while loop. Where each loop in a JavaScript program completes a particular program purpose. Where the execution methods of all these loops are different.

for while do...while

So, let’s know better about for, while, and do while loops in JavaScript programming.

for Loop in JavaScript.

The for loop is applied in a JavaScript program. When the JavaScript programmer already knows how many times the for loop is to be executed from where to where in the program.

The main elements of a for loop are.

  • Initialization – It starts or sets the loop variable in the for loop.
  • Condition – It tests the condition given in the current program whether the for loop should continue or not.
  • Increment/Decrement – For loop modifies the value of the loop variable by incrementing or decrementing it after each iteration if the condition is true.

For loop syntax.

for (variable initialization; program condition; value increment/decrement) {

// Program code to be executed repeatedly if the condition is true

}

Basic Java Script for loop example.

for (let p = 0; p < 7; p++) {

console.log(p);

}

Explanation of for loop.

let p = 0 – Here p variable starts the for loop with the value 0 equal to p.

p < 7 – As long as p is less than 7 value here, the loop will continue.

p++ – Every time after each loop iteration, p variable will be increased by 1.

while Loop in JavaScript.

In any JavaScript program, the while loop continues running as long as the given condition is true. It tests the given condition before executing the program code block in JavaScript program. Remember, in the while loop, if the given condition is false initially, then the current program will not execute inside the while loop.

Syntax of while Loop.

while (program condition) {

// Program code that is executed repeatedly based on the condition given in the while loop

Example of while Loop.

let p = 0;

while (p < 9) {

console.log(p); p++; // it Increment p variable value to avoid infinite loop condition

}

Explanation of while Loop.

Here in the while loop, the program checks the first condition of p < 9 before each iteration. The while loop executes as long as the condition given here is true.

And p++ increments the value of p variable after each while loop iteration.

Remember – apply the use of while loop in JavaScript program carefully! If the given while loop program condition is not false, then your program may terminate by going into an infinite loop, it may freeze your program or continue running infinitely.

do…while loop in JavaScript.

The do…while loop in JavaScript program is used in the same way as the while loop, but with an important difference in do…while loop, where the do…while loop executes the program code block at least once before testing the given program condition. This is because in do…while loop the given condition is tested after the program code is executed.

Syntax of do…while loop.

do {

// Program code to be executed at least once in the current program

} while (program condition);

Example of do…while loop.

let p = 0;

do {

console.log(p);

p++; // p variable value Increment and p variable to avoid infinite loop condition

} while (p < 8);

Explanation of do…while loop.

The code inside the do…while loop do block is executed first, and then the given do…while loop condition p < 8 variable value is tested.

Since, here the program condition is tested after the first program execution, due to this the do…while loop will always run at least once, even if the given program condition is false initially.

Main difference between for loop, while loop, and do…while loop in JavaScript.

JavaScript with for Loop.

The for loop in JavaScript programs is best used when you know a specific number of iterations of a variable value from start to end in your program. The for loop is an ideal option for iterating through a program such as an array or range.

JavaScript with while Loop.

The while loop is ideal in JavaScript programs when you don’t know the number of iterations of the program variable value based on the given condition in your current program, and you want the while loop in that program to repeat the loop as long as the given program condition is true in the current program.

The given condition is tested before the while loop starts, so, if the given program condition is false at the start, the program code inside the program loop may not execute at all.

JavaScript with do…while Loop.

The do…while Loop in JavaScript programs is similar to the while loop, but the program code block is executed at least once, even if the given program condition is false.

The program condition is tested after each loop iteration, so, the do…while Loop runs at least once, without neglecting the given program condition.

Example of counting using for loop, while loop, and do…while for in JavaScript.

Let’s use all three types of loops to count numbers from 1 to 7 in a JavaScript program.

for Loop example in JavaScript.

console.log(“for loop explanation -“);

for (let p = 1; p <= 8; p++) {

console.log(p);

}

while Loop example in JavaScript.

console.log(“while loop explanation-“);

let p = 1;

while (p < 8) {

console.log(p);

p++;

}

Example of do…while Loop in JavaScript.

console.log(“do…while loop explanation”);

let p = 1;

do {

console.log(p);

p++;

} while (p < 8);;

Loop control statement in JavaScript.

JavaScript programming provides you some control flow statements, which are used to modify the default program behavior inside the program loop.

break statement – In JavaScript program breaks the loop based on the given break condition without ignoring the loop condition.

for (let q = 0; q <= 9; q++) {

if (q === 7) {

break; // break statement use to exit the loop when q is equal to or reach at 7

}

console.log(q); // Resul is – 0 1 2 3 4 5 6

}

continue statement – In JavaScript program avoids the current given program condition iteration, and continues moving with the next program loop statement.

for (let q = 0; q <=7; q++) {

if (q === 4) {

continue; // here continue statement Skip when q is equals or reach to 4

}

console.log(q); // Result – 0 1 2 3 5 6 7

}

Conclusion of for, while, and do while loops in JavaScript

The for loop is used in JavaScript programs when the programmer already knows the start to end loop statement print value number.

The while loop is useful in JavaScript programs when the programmer wants to execute the loop as long as a given program condition is true, but the start to end count of iterations of the given condition in the current program is not predefined.

With the do…while loop in JavaScript programs, you decide that the current program code should be executed at least once, and then continue as long as the given program condition is true.

Leave a Reply