Friday 18 November 2011

Conditional Structure and Looping Structure


Ø       Conditional Structure

Conditional statement in javascript are used to perform different action based on different condition. Very often when you write code, you want to perform different action for different decision.

            In javascript we have the following conditional statement.

·         Simple If Statement

Use this statement if you want to execute some code only if a specified condition is true.

            Syntax


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

Example

<html>
          <head>
                   <script type="text/javascript">
                             var a=40;
                             var b=80;
                             if(a!=b)
                             {
                                      document.write("a and b are not same value");
                             }
</script>
          </head>
          </html>

·         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.

            Syntax


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

                   }

Example

<html>
          <body>
                   <script type="text/javascript">
                             var m1=40;
                             var m2=50;
                             var m3=90;
                             if(m1>=40 && m2>=40 && m3>=40)
                             {
                                      document.write("Result : Pass");
                             }
                             else
                             {
                                      document.write("Result : Fail");
                             }
</script>
          </body>
          </html>

·         If – Else - if Statement

Use this statement if you want to select one of many blocks of code to be executed.

Syntax

                        if(condition1)
                   {
                             Code to be executed if condition is true
                   }
                   else if(condition2)
                   {
                             Code to be executed if condition is true
                   }
                   else if(condition_N)
                   {
                             Code to be executed if condition is true
                   }
                   else
                   {
                             Code to be executed if condition is false
                   }

Example

<html>
          <body>
                   <script type="text/javascript">
                             var m1=80;
                             var m2=50;
                             var m3=90;
                             var tot=m1+m2+m3;
                             var per=tot/3;
if(per>=70)
                             {
                                      document.write("Class : Dist");
                             }
                             else if(per<=60)
                             {
                                      document.write("Class : First");
                             }
                             else if(per<=50)
                             {
                                      document.write("Class : Second");
                             }
                             else
                             {
                                      document.write("Class : Third");
                             }
</script>
          </body>
          </html>

·         Switch Statement

You should use the switch statement if you want 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 case1 and case 2;
                                      break;
                   }
                       
    • First we have a single expression n that is evaluated once
    • The value of 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.
Example

<html>
          <body>
                   <script type="text/javascript">
                             var d = new Date();
                             var theday = d.getDay();
                             switch(theday)
                             {
                                      case 5:
                                                document.write("Finally Frieday");
                                                break;
                                      case 6:
                                                document.write("Super Sunday");
                                                break;
                                      case 0:
                                                document.write("Sunday");
                                                break;
                                      default:
document.write("I am looking forward to this weekend");
                                                break;
                             }
</script>
          </body>
          </html>

Ø       Looping Structure

Very often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.

In javascript there are three different kind of loops.

1.for loop
2.while loop
3.do while loop

·         The for loop

Loops through a block of code a specified number of times. The for loop is used when you know in advance how many times the script should run.

            Syntax


                        for(initialization; condition; increment or decrement)
                   {
                             Code  to be executed
                   }

The for loop has three section initialization, condition, increment or decrement.

Initialization: means the variable is to be started with some values. This means starting value to be given to the variable.

Condition: condition section check the condition if the condition is true then code to be executed else executed the next statement after for loop.

Increment or decrement: to process the loop we have to either increment the variable or decrement the variable otherwise the loop will not process.

Example

<html>
          <body>
                   <script type="text/javascript">
                             Var a;
                             for(a=1;a<=10;a++)                    
                             {
                                      document.write(a);
                             }
</script>
          </body>
          </html>

·         The while loop

The while loop is used when you want the loop to execute and continue executing while the specified condition is true.

            The increment of the number is done manually inside the loop.

            Syntax


                        while(condition)
                   {
                             Code  to be executed
                             Increment or decrement of the variable
                   }

Example

<html>
          <body>
                   <script type="text/javascript">
                             Var a=1;
                             while(a<=10)                    
                             {
                                      document.write(a);
                                      a=a+1;
                             }
</script>
          </body>
          </html>

·         The do…while loop

The do while loop is a variant of the while loop. The loop will always execute a block of code  once, and then it will repeat the loop as long as the specified condition is true.

This loop always be executed once, even if the condition is false, because the code are executed before the condition is tested.

Syntax          

                   do
                   {
                             Code  to be executed
                   }
while(condition)

Example

<html>
          <body>
                   <script language="javascript">
                             var a=1;
                             do
                             {
                                      document.write(a+”<br/>”);
                                      a=a+1;
                             }
                             while(a<=10)                    
</script>
          </body>
     </html>

Ø       Javascript break and continue statement

            There are two special statement that can be used inside loops.

·         Break

The break command will break the loop and continue executing the code that follows after the loop (if any).

Example

<html>
          <body>
                   <script language="javascript">
                             var a;
                             for(a=1;a<=10;a++)
                             {
                                      if(a==5)
                                      {
                                                break;
                                      }
                                      document.write(a+”<br/>”);
                             }       
</script>
          </body>
     </html>

·         Continue

The continue command will break the current loop and continue with the next value.

Example

<html>
          <body>
                   <script language="javascript">
                             var a;
                             for(a=2;a<=10;a++)
                             {
                                      if(a%2==0)
                                      {                                                                                                                            document.write(a+”<br/>”);
                                                continue;
                                      }
                                      else
                                      {
                                                break;
                                      }
                             }       
</script>
          </body>
     </html>

·         For..in loop

The for..in statement is used to loop through the elements of an array or through the property of an array.

            Syntax


                        for(variable IN object)
                   {
                             Code  to be executed
                   }

            Example

<html>
          <body>
                   <script language="javascript">
                             var arr = new Array();
                             arr[0] = “PHP”;
                             arr[1] = “VB”;
                             arr[2] = “C++”;
                             for(x in arr)
                             {
                                      document.write(arr[x]+”<br/>”);
                             }
</script>
          </body>
          </html>

Posted By : Hemangi Zala

No comments:

Post a Comment