·  JavaScript – Syntax – Conditional Statements – for

 

- Info:

  - This tutorial shows how to use if...else conditional statements.

 

- Example:

  - Create D\Temp\JavaScript\Test.html

 

    Test.html

    <script type="text/javascript" language="javascript">

      

      //SIMPLE.   Output is "1 2 3 4".

      for(i=1;i<=4;i++){

        window.alert(i);

      }

   

      //COMPLEX.  Output is "1 2 3".

      for(i=1, j=5 ; i<=4 && j>2 ; i++,j--){

        window.alert(i);

      }

   

      //BREAK.    Output is "1 2 3" since we break out of for loop before condition i<4 is reached.

      for(i=1;i<=4;i++) { 

        window.alert(i);

        if(i==3) { break; }

      }

   

      //CONTINUE. Output is "1 2 4" since display of number 3 is skipped.

      for(i=1;i<=4;i++) {

        if(i==3) { continue; };

        window.alert(i);

      }

   

    </script>