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.

3 comments:

kate technologies said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
php application development
php web application development company india

Kamila said...

Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging
Dot Net coaching Institute in Chennai | Dot Net Training in Chennai | Dot Net Training Center in Chennai
Software Testing Training Institute in Chennai | Manual Testing Training in Chennai | Testing Courses in Chennai
Java Training Institute in Chennai | Java Course and Certification | Core Java Certification in Chennai
PHP Certification Class | PHP Course in Chennai | PHP Training Institute in Chennai

SURAJ said...

Thanks for your information.
https://livescorecri.blogspot.com/p/blog-page_5.html
https://www.chemistrypage.in/cbse-class-12th-chemistry-question-paper-analysis-set-1/