JavaScript for Loop Flowchart and Programming Examples

JavaScript for loop

Description:

JavaScript for Loop- In this tutorial you will learn about for loop in JavaScript with programming examples and flowchart…

JavaScript for loop:

The for loop statement is used to execute the body of loop repeatedly for a fixed number of times. Therefore, for loop is also referred to as a counter loop. The format of for loop is different from both the while and do-while loop statement.

The general syntax or format of for loop structure is:

for(initialization; condition; increament/decreament)

{

Body of loop

}

Next_statement;

Where:

Initialization:

Specifies the statement used to assign value to a variable. It is executed only the first time when control enters the loop. You can initialize multiple variables separated by commas in this part of js for loop but at the semicolon is used. For example;

for( x = 1, y = 5;  x<= 5; x++)

typically, it is used to assign value to control variable that control the number of repetitions of the loop.

The initialization part of js for loop is optional. If it is omitted then a semicolon(;) is used in its place.

Condition:

Specifies a test condition. The js for loop continues to execute a body of loop as long as the test condition is true.

Increment / Decrement:

It is used to increase or decrease the value of a control variable. This part is executed after execution the body of loop. It is supposed as last statement of the body of loop. You can also use multiple increment or decrement statements or compound assignment expressions separated by commas. The use of increment/decrement is also optional.

All the of the js for loop structure are optional. In this case, the syntax of  JavaScript for loop structure is;

for(; 😉

the first semicolon is used in place of initialization part and second is used in place of condition part. It is noted that in the above syntax, there is no test condition and the loop becomes infinite loop. Javascript assumes that the condition is true.

The body of loop may contain single or compound statement. If there is a single statement in the body of the loop then the use of braces is optional. You can also omit the body of JavaScript for loop. In this case, use semicolon (;) in place of the body of the loop.



The flowchart of JavaScript for loop:

JavaScript for loop

A script is given below to print first 10 natural number using JavaScript for loop statement

JavaScript for loop


Execution of JavaScript for loop:

The JavaScript for loop is executed as follows:

  1. First Initialization part is executed. In most programs this part of JavaScript for loop statement is used to declare and initialize variables. It is executed only first time when control enters into the loop.
  2. After execution initialization part, the condition is evaluated. If it is nonzero (true), then body of loop is executed.
  3. After executing body of loop, increment/decrement part is executed. Increment/decrement part is considered as last statement of body of JavaScript for loop.
  4. After executing increment/decrement, control transfers to condition. If it is nonzero (true), then body of loop and increment/decrement are executed and control comes back to condition. This process is repeated until the condition becomes false. If the condition becomes false at any stage during execution of program, the loop terminates and computer control shifts to the statement that comes immediately after the body of JavaScript for loop statement i.e. to the “next statement; “.

Example How to calculate and print the product of odd integers from 1 to 10 using JavaScript Js for loop:

JavaScript for loop


How to print even integer from 1 to 15 in descending order using JavaScript Js for loop:

JavaScript for loop

How to use JavaScript for loop to generate Fibonacci series from 1 to 100:

JavaScript for loop

JavaScript for loop



JavaScript Nested Loop:

Like nested if selection structure, you can use a loop structure inside the body of another loop structure. Therefore, a loop structure inside the body of another loop structure is called nested loop.

You can use the for loop structure inside the body of while loop or do-while loop and vice versa. The concept of nested loop is explained in the following script:

In the above script, while loop structure contains for loop structure in its body. The while loop structure is executed three times. Each time the while loop structure is executed, the for loop structure is executed two times. the output of the above script will be as under:

JavaScript for loop


Example how to use while loop as a nested loop in JavaScript:

JavaScript for loop

Example how to print number in the pattern using nested while loop in JavaScript:

JavaScript for loop


JavaScript Continue Statement:

The continue statement can be used in the body of any loop structure. When this statement is executed, it skips the remaining statements in the body of loop and the execution control transfers to the beginning of loop. In case of while and do-while loop structure, the control shifts to the test condition. The condition is evaluated, if it is true then control shifts to the first statement of the body of loop.

In case of JavaScript for loop structure, when continue statement is executed in the body of  JavaScript for loop, then control shifts to the increment/decrement part. After taking action on increment/decrement part, the text condition is evaluated. If condition is true control shifts to the first statement of the body of JavaScript for loop and so on.

A script is given below to describe the continue statement.

In the above program, only first three statements inside the body of do-while loop are executed 5 times. the two statements after continue statement are not executed. It is because each time the continue statement returns the control to the test condition. After evaluating the condition, control shifts to the beginning of loop if condition is true. As you can see in below image

JavaScript for loop



JavaScript break statement:

Although break statement is use to terminate a case in the switch statement. The break statement is also used to terminate a loop. When the break statement is executed in a while , do-while, for or switch statement. The execution control exits from that structure and shifts to the statement that comes immediately after that structure.

A script is given below to explain the concept of break statement.

In the above script, the do-while loop is used. It should execute 10 times but it executes 5 times. when the value of the control variable ‘x’ is 5, the condition “ if(x==5)” becomes true, and the break statement is executed. So the loop is terminated and execution control exits from the body loop. the output you can see in the below image.

JavaScript for loop

In the case of a nested loop, if a break statement is used inside the body of the inner loop then only the inner loop is terminated. The control transfers to the statement of the outer loop that comes immediately after the inner loop structure. Similarly, if the break statement is used inside the body of the outermost loop structure then the nested loop structure will be terminated.

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: