Java-for( ) loop / Chapter -5/Flow Control




c. for ( ) loop :- 

  • If we know the number of iterations in advance then we should use for ( ) loop. 
  • It is the most commonly used loop in java.
  • Curly braces{ } are optional and without Curly braces { } we can take one statement under while which should not be the declarative statement.


Syntax:-
javabychetan.blogspot.com
                                                
(a) initialization section;-


  • This part will be executed only once in the whole life cycle of the for() loop. Here we can declare and  initialize a local variable.we can declare any number of variable but they should be the same type . If we are trying to declare different data type then we will get compile time error. Re declaration is also not allowed. 
example:
int i=0, j=0;             valid
int i=0, String s ="javabychetan";             invalid  (different data type)
int i=0,int j=0;                                          invalid ( Re declaration is not allowed)


  • In the initialization section, we can take any valid java statement including 'System.out.println' also.

 example:-
                int i=0;
for(System.out.println("you are reading javabychetan");i<3:;i++)
{

             System.out.println("you are reading javabychetan");                valid

}

(b) condition-check section;-

  • Here we can take any valid java expression but this should always be of the boolean type .
  • This section is optional.
  • We can leave it blank and  in this condition compiler will always place true here.

(c) Increment / Decrement section:-

  •  In the increment-decrement section we can take any valid java statement including System.out.println( ).

 example:-
                int i=0;
for(System.out.println("Hello");i<3:;System.out.println("Hii"))
{
               i++;                                                              valid

}
output is                 Hello
                              Hii
                              Hii
                              Hii
                     
Note: All three parts of for( ) loop are independent of each other and optional.
for( ;  ; )

             (System.out.println("Hello");  
}

Output :  This Is Infinite loop and Hello will be printed for infinite times.


              Important Loop Holes

example 1:-
 
for(int i=0 ; true ; i++)
{
    System.out.println(" javabychetan");        
}
 System.out.println("JAVA");
      ↑
      invalid
 Compile Time Error: Unreachable state


example 2:-
 
for(int i=0 ; false ; i++)
{
      System.out.println(" javabychetan");        
   }
 System.out.println("JAVA");

      invalid
 Compile Time Error: Unreachable state

example 3:-
 
for(int i=0 ; ; i++)
{
    System.out.println(" javabychetan");        
}
 System.out.println("JAVA");
      ↑
      invalid
 Compile Time Error: Unreachable state


example 4:-
 
int i=10,j=20;
for(int x=0 ; i < j ; x++)
{
    System.out.println(" javabychetan");        
}
 System.out.println("JAVA");

     Output is:-      javabychetan
                        javabychetan
                        javabychetan  .........(infinite times)


example 5:-
 
int i=10,j=20;
for(int x=0 ; i > j ; x++)
{
    System.out.println(" javabychetan");        
}
 System.out.println("JAVA");

Output is:-     JAVA


example 6:-
 
final int i=10,j=20;
for(int x=0 ; i < j ; x++)
{
    System.out.println(" javabychetan");        
}
 System.out.println("JAVA");
  ↑
      invalid
 Compile Time Error: Unreachable state



example 7:-
 
final int i=10,j=20;
for(int x=0 ; i > j ; x++)
{
    System.out.println(" javabychetan");        
      }
  System.out.println("JAVA");
  
      invalid

 Compile Time Error: Unreachable state


Next:Chapter -6/Flow Control/ for each loop
Back:Chapter -4/Flow Control/ do-while () loop

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com