Fallowing icon

Thursday, September 27, 2012

for loops


       The for loop is similar to the do-while or while loops. It's can use easier than while loops, because for loop have three elements. Those are initialization,termination and increment. Initialization and increment statements are got one command in for statements, It's very use full for create conditions and use easily in java.

The stricter of the for statement-  

       
            for(initialization;boolean expression;increment){

                     statement1;

                     statement2;
                     ...................; 
                     }

1) The initialization

       The initialization is used for create the variable and assign value to create particular condition. 
Important- We cannot change for loop order. 
        
Examples-  int i=0;
           int i=1;
           int j=3;

2) The Termination

       Termination expression is used for get boolean results(true or false). We use the variable was already declared to create termination. When we not use termination, it get like never false value(infinity) in java.

Examples-  i<10;
           j>=4;
           i<x    (x is a another variable in a program)

3) The increment
    
       Increment is use for add or subtract value in the variable was already declared after each iteration through the loop.

Examples-  i++;
           i--;
           j++;


The process of for statements- 




  1) Start the for loop and execute initialization. 
  2) Throw the boolean expression and give the new variable's value
  3) When the boolean expression is passed true, execute all the statements in for loop body. 
  4) When the for loop block statements are executed, throw increment statement.
  5) After the execution, throw again the boolean expression and execute.  
  6) When the boolean expression is passed false, throw the program in for loop.




Example 1-  


                  public class Test { 
    
                             public static void main(String args[]){
      
                                         String myName="Boston";                        //declare the variable myName and assign the value Boston
                                         for(int i=0;i<4;i++){                                     //declare the for loop 
                
                                         System.out.println(i+" time "+myName);

                                         }
                             }
                  }










      The for loop is run fore times, because we create variable i and assign to 0. then check below the value to 4 in boolean expression. First time i=0 and 0<4, It's true. So boolean expression pass true. after add one. then process the program till the i=4. In that time boolean expression is passed false first time in the program, then the program is throw out from the loop.



Example 2-



              public class Test { 
    
                      public static void main(String args[]){      
                                 int prosNo=1;                                           //create the variable prosNo and assign the value 1
                                 for(int i=1;i<4;i++){                                  //create the for loop
                                           for(int j=0;j<i;j++){                         //create another for loop in previous one. 
               
                                                 System.out.println("Processing no ="+prosNo+"             i ="+i+" j ="+j);
                                                 prosNo++;                             //add one for prosNo value
                                             }    
                                  }
                        }
                }



   
        

     


       Frist we create variable presNo and assign to the value  is 1. Then create for statement and give i<4 for boolean expression. After create for statement in previous for statement and get the number of times satiate i<j. After print it presNo , i and j variables.


 Discussion- 
            
          In this example create for loop in another for loop. When the first time program is running i=1, first loop expression is i<4. Now its true, then program executed second for loop. In that time j=0, then check j<i in expression. 0<1 is true, then execute print statement which in for loop block. Now i=1, then i<j statement is executed again. In this time expression is pass false, then program throw in second for loop. Now execute first for loop again and add 1 to i variable. Now i=1. Then execute i<4 expression. This process is processing again and again until i<4 expression is passed false.
      


No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment