javaScript while loop

javascript while loops can executes a statement or series of statements until a specified condition is false

javascript while loops are a way of repeating the same block of code over and over.


JavaScript while loop

the while is an entry-controlled loop statement.

the while loop can executes a statement or series of statements until a specified condition is false

syntax:

         while(condition){
 
                                 statement_1;
                               
                                 statement_2;
                               
                                 statement_n;
                                 
                                 i++/i--     //loop control statement
                            }

the purpose of a while loop is to execute a statement or block statements repeatedly as long as condition is true. once condition becomes false or a break statement is encountered, the loop will be exited.


how it work :

"the while loop - condition test occurs before statement in the loop are executed.if the condition returns true, then the body of the loop is executed.after execution of the body, the condition is once again evaluated and if it is true, the body is executed once again.this process of repeated execution of the body continues until the condition finally becomes false and the control is transferred out of the loop".

example: the loop in this example will continues to run as long as the variable i is less than 10:

   <script>     
                 var  i = 0;
            
                 while ( i<10 ){
                        
                           document.write("<br/>"+"look this i ......"+i);
                         
                           ++i;      //variable increment
                        }
                               // i is now 10
  </script>

javaScript do while loop

The javascript do  while loop is a variant of the while loop. This loop will execute the code block once before checking condition, if the condition is true, then it will repeat the loop as long as the condition is true.

the javascript do while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once.

the while statement must end with semicolon(;)

syntax:

 


            do
              {
                  first-statement;
           
                  second-statement;
           
                  n____statements;
                  
                  i++/--i         loop control statement
            
            }while ( condition );
   <script>   
                  var    i = 0;            
                      do
                        {                 
             document.write("<br/>"+"the i value inside the do while loop -----"+i);
                         
                     ++i;      //variable increment statement
                        
                        }while ( i < 1 );
                        
                               // i is now 1
                               
  </script>
code output

note :- do not forget to increase the variable used in the condition, otherwise the loop will never end.


javaScript nested while loop

Javascript nesting of loops, that is, one while statement within another while statement.

syntax:nesting while loop statements

   <script>
       
           var  i =0 , j=0 ;
           
           while(condition){
           
                               outer_while_loop_statement_first;
                             
                               while(condition){
                               
                                               inner whileloop statements
                                           
                                           j++    //inner while loop control statement
                                           
                                         }
                            
                            outer_while_loop_statement_second;
                            
                                   i++    //outer while loop control statement
                          }

syntax: nesting do while loop

          do
              {
                  statement_1;
           
                  statement_n;                                   
                                     
                                        do
                                            {  statement_1
                                                                 
                                               statement___n;
            
                                           }while( condition );
            
            }while ( condition );

example

  <script>
  
           var  i = 0 ;
           
          do{    
                   var  j = 0 ;
          
                   do{
                            if( i === j ){
                         
                   document.write("<br/>"+"the i and jth value is equal"+ i+"   "+j);
                                     
                                 }
                      
                        ++j ;
                                 
                    }while( j<5 );
          
           ++i ;
                    
         }while( i<5 );
         
 </script>

javascript infinite while loops

an infinite loop occurs when a condition always evaluates to true.

here are some pseudocode for an infinite loop for a web server.

                       while( true )   {  // condition always true }

 if you forget to increase the variable used in the condition, the loop will never end.

     var i=0;        while( i < 5 )  {    --i ;  }

note : the infinite loop crash your browser so its never used in programming.

if you add 'break' to a loop, the loop will end, no matter whether the condition is false or not.

   while(true){document.write("this is infinite loop but break by the break keyword");break}