Tuesday, October 21, 2014

IF condition statement in javascript , Switch case statement in javascript

  • CONDITIONAL STATEMENTS:
  • Conditional statements in JavaScript are used to perform different actions based on different conditions.
  • Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
  • In JavaScript we have the following conditional statements:
  • if statement - use this statement if you want to execute some code    

                       only if a specified condition is true

  • if...else statement - use this statement if you want to execute some code if the condition is true and another code if the condition is false
  • if...else if....else statement - use this statement if you want to select

                                             one of many blocks of code to be executed

  • switch statement - use this statement if you want to select one of

                               many blocks of code to be executed

IF STATEMENT:

  • You should use if statement if you want to execute some code only if a specified condition is true.

Syntax:
        If <condition>
{
JavaScript code executed if condition becomes true.
}

Example:
<script language = "javascript">
var x = 5,y=6;
if (x>y)
{
document.write(' x is greater');
}
</script>

IF...ELSE STATEMENT:

  • If you want to execute some code if a condition is true and another code if the condition is not true, use if....else statement.

Syntax:

            if (condition) 
            { 
Code to be executed if condition is true 
            } 
            else 
            { 
Code to be executed if condition is not true
            } 

 

Example:
<script language = "javascript">
x = 5;
y = 6;
if (x > y)
{
document.write('x is greater');
}
else
{
document.write('y is greater');
}
</script>

IF…ELSE IF…ELSE STATEMENT:
  • It is used when user have two or more condition to check for execution of code.

Syntax:
          if <condition1>
{
          Code if condition 1 is true;
}
else if <condition2>
{
          Code if condition 2 is true;
}
else
{
          Code if no condition becomes true;
}

Example:
            <script language = "javascript">
                        x=5;
     y=6;
     z=7;
     if(x>y && x>z)
     {
                 document.write('x is greater');
     }
     else if(y>x && y>z)
     {
                 document.write('y is greater');
     }
     else
     {
                 document.write('z is greater');
     }
            </script>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


SWITCH STATEMENT:

  • use the switch statement to select one of many blocks of code to be executed

 

Syntax:
        switch (n)

          { 
    case 1: 
               Execute code block 1 
               break;     
    case 2: 
               Execute code block 2 
               break; 
    default: 
               Code to be executed if n is 
               Different from case 1 and 2
           } 
 


  • First we have a single expression n (most often a variable), that is evaluated once.

 

  • The value of the expression is then compared with the values for each case in the structure.
  • If there is a match, the block of code associated with that case is executed.

 

  • Use break to prevent the code from running into the next case automatically.

No comments: