Control and looping structure in javascript

1. If  Statement

  • IF statement is a conditional branching statement.
  • In 'IF' statement, if the condition is true a group of statement is executed. And if the condition is false, the following statement is skipped.

Syntax : If statement

if(condition)
{
     //Statement 1;
     //Statement 2;
}


Example : Simple Program for IF Statement


     
     
     

Output:

Control and looping structure in javascript

2. If – Else Statement

  • If – Else is a two-way decision statement.
  • It is used to make decisions and execute statements conditionally.

Flow Diagram of If – Else Statement

Control and looping structure in javascript

Syntax : If-Else statement

if (condition)
{
     //Statement 1;
}
else if(condition)
{
     //Statement 2;
}
else
{
     //Statement 3;
}


Example : Simple Program for If-Else Statement


     
     
     

Output:

Control and looping structure in javascript

Control and looping structure in javascript

Control and looping structure in javascript

Control and looping structure in javascript

3. Switch Statement

  • Switch is used to perform different actions on different conditions.
  • It is used to compare the same expression to several different values.

Flow Diagram of Switch Statement

Control and looping structure in javascript

Syntax

switch(expression)
{
     case condition 1:
          //Statements;
          break;
     case condition 2:
          //Statements;
          break;
     case condition 3:
          //Statements;
          break;
     .
     .
     case condition n:
          //Statements;
          break;
     default:
          //Statement;
}


Example : Simple Program for Switch Statement



     

Output:

Control and looping structure in javascript

Control and looping structure in javascript

4. For Loop

  • For loop is a compact form of looping.

  • It includes three important parts:
    1. Loop Initialization
    2. Test Condition
    3. Iteration
  • All these three parts come in a single line separated by semicolons(;).

Flow Diagram of 'For' Loop

Control and looping structure in javascript

Syntax

for(initialization; test-condition; increment/decrement)
{
     //Statements;
}


Example : Palindrome Program using For Loop



     
     


          Enter a String or Number:

          
     


Output:

Control and looping structure in javascript

Control and looping structure in javascript

5. For-in Loop

  • For-in loop is used to traverse all the properties of an object.
  • It is designed for looping through arrays.

Syntax

for (variable_name in Object)
{
     //Statements;
}

6. While Loop

  • While loop is an entry-controlled loop statement.
  • It is the most basic loop in JavaScript.
  • It executes a statement repeatedly as long as expression is true.
  • Once the expression becomes false, the loop terminates.

Flow Diagram of While Loop

Control and looping structure in javascript

Syntax

while (condition)
{
     //Statements;
}


Example : Fibonacci Series Program using While Loop



     

Output:

Control and looping structure in javascript

7. Do-While Loop

  • Do-While loop is an exit-controlled loop statement.
  • Similar to the While loop, the only difference is condition will be checked at the end of the loop.
  • The loop is executed at least once, even if the condition is false.

Flow Diagram of Do – While

Control and looping structure in javascript

Syntax

do
{
     //Statements;
}
while(condition);


Example : Simple Program on Do-While Loop



     

Output:
0
1
2
3
4
5

Difference between While Loop and Do – While Loop

While LoopDo – While Loop
In while loop, first it checks the condition and then executes the program. In Do – While loop, first it executes the program and then checks the condition.
It is an entry – controlled loop. It is an exit – controlled loop.
The condition will come before the body. The condition will come after the body.
If the condition is false, then it terminates the loop. It runs at least once, even though the conditional is false.
It is a counter-controlled loop. It is a iterative control loop.

8. Break Statement

  • Break statement is used to jump out of a loop.
  • It is used to exit a loop early, breaking out of the enclosing curly braces.

Syntax:
break;

Flow Diagram of Break Statement

Control and looping structure in javascript

9. Continue Statement

  • Continue statement causes the loop to continue with the next iteration.
  • It skips the remaining code block.

Flow Diagram of Continue Statement

Control and looping structure in javascript

Syntax:
continue;

What are control structures in JavaScript?

The control structures within JavaScript allow the program flow to change within a unit of code or function. These statements can determine whether or not given statements are executed - and provide the basis for the repeated execution of a block of code.

What are looping control in JavaScript?

Loop control statements are certain JavaScript statements that interrupt the normal flow of the program. They direct the program control to a specific location in the code. Therefore, sometimes, we also call them “Jump Statements”..
continue..
break..
label..

What are looping control structures?

Control structures alter the normal sequential flow of a statement execution. Loops allow the a block of statements to be executed repeatedly without actually writing them down numerous times.

What are the 4 control structures?

if-else conditionals, case statements, for loops, and while loops are all control structures.