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


     
     
          var num = prompt["Enter Number"];
          if [num > 0]
          {
               alert["Given number is Positive!!!"];
          }
     
     

Output:

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

Syntax : If-Else statement

if [condition]
{
     //Statement 1;
}
else if[condition]
{
     //Statement 2;
}
else
{
     //Statement 3;
}


Example : Simple Program for If-Else Statement


     
     
          var no = prompt["Enter a Number to find Odd or Even"];
          no = parseInt[no];
          if [isNaN[no]]
          {
               alert["Please Enter a Number"];
          }
          else if [no == 0]
          {
               alert["The Number is Zero"];
          }
          else if [no % 2]
          {
               alert["The Number is Odd"];
          }
          else
          {
               alert["The Number is Even"];
          }
     
     

Output:

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

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



     
     var day = prompt["Enter a number between 1 and 7"];
     switch [day]
     {
          case [day="1"]:
               document.write["Sunday"];
               break;
          case [day="2"]:
               document.write["Monday"];
               break;
          case [day="3"]:
               document.write["Tuesday"];
               break;
          case [day="4"]:
               document.write["Wednesday"];
               break;
          case [day="5"]:
               document.write["Thursday"];
               break;
          case [day="6"]:
               document.write["Friday"];
               break;
          case [day="7"]:
               document.write["Saturday"];
               break;
          default:
               document.write["Invalid Weekday"];
               break;
     }
     

Output:

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

Syntax

for[initialization; test-condition; increment/decrement]
{
     //Statements;
}


Example : Palindrome Program using For Loop



     
     function palindrome[]
     {
          var revstr = " ";
          var strr = document.getElementById["strr"].value;
          var i = strr.length;
          for[var j=i; j>=0; j--]
          {
               revstr = revstr+strr.charAt[j];
          }
          if[strr == revstr]
          {
               alert[strr+" - is Palindrome"];
          }
          else
          {
               alert[strr+" - is not a Palindrome"];
          }
     }
     
     
          Enter a String or Number:

          
     

Output:

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

Syntax

while [condition]
{
     //Statements;
}


Example : Fibonacci Series Program using While Loop



     
          var no1=0,no2=1,no3=0;
          document.write["Fibonacci Series:"+"
"];
          while [no2

Chủ Đề