Iterative Statement : for loop

Iterative Statement : for loop

                                         For Loop

  • for loop is the most commonly used in java.
  • if we know several iterations in advance the for loop is the best choice.

Syntex-:

for(initialization_section;condition_check;increment/decrement){
  loop body;
}



  • curly braces are optional without curly braces only one statement under for loop, which should not be declarative statements.

          example-:

      1    for(int i=0;i<20;i++)
                  int x=10; //invalid
     2   for(int i=0;i<20;i++); //valid
    3    for(int i=0;i<20;i++)
                 System. out.print("Hello");//print 20 time Hello 
    
                                                            Initialization Section
  • this part will be executed only once in the loop lifecycle 
  • here we can declare and initialize the local variable of for loop.
  • here we can declare any no. of variables but they should be of the same type by mistake if we are trying to declare different data type variables then we will get compile time error.
         example-:
          for(int i=0,String s="Hi";i<10;i++)//not valid
          for(int i=0,j=0;i<10;i++)//valid
          for(int i=0,int j=0;i<10;i++)//not valid
            
  • in the initialization, we can take any valid java statement including system.out.print().
         example-:
         int i=0;
         for(System.out.println("hello");i<10;i++){
            System.out.print("Java");
        }
       output-:
       hello
       Java Java Java Java....................Java
                                                conditional section
  • here we can take any valid java expression but should be of the type boolean.
  • this part is optional and if we are not taking anything then the compiler will always be true.
         example-:
         for(int i=0;;i++)
            System.out.print("hello");//print infinite time hello


                                              Increment/Decrement Section

in the increment/decrement section, we can take any valid java statement including System.out.print().

example-:
  for(int i=0;i<3;System.out.print("Hello")){
       i++;
  }
output-:
Hello Hello Hello


all three parts of for loop are independent of each other and optional
example-:
 for(;;){
    System.out.print("hello");
}
 for(;;);
both are infinite loop


                                                Unreachability 
case 1-:
  for(int i=0;true;i++){
    System.out.print("hello");
}
  System.out.print("hi");// compile time error unreachable statement


case 2-:
  for(int i=0;false;i++){
    System.out.print("hello");//compile time error unreachable statement
}
  System.out.print("hi");


case 3-:
  for(int i=0;;i++)//compile is always true{
    System.out.print("hello");
}
  System.out.print("hi");//compile time error unreachable statement


case 4-:
  int a=10,b=20;
  for(int i=0;a<b;i++){
    System.out.print("hello");
}
  System.out.print("hi");
output-:
  hello hello hello......................................................infinite time



case 5-:
int a=10,b=20;
  for(int i=0;a>b;i++){
    System.out.print("hello");
}
  System.out.print("hi");
output-:
  hi


case 6-:
final int a=10,b=20;
  for(int i=0;a<b;i++){
    System.out.print("hello");
}
  System.out.print("hi");// compile time error unreachable statement


case 7-:
final int a=10,b=20;
  for(int i=0;a>b;i++){
    System.out.print("hello");// compile time error unreachable statement
}
  System.out.print("hi");


Previous Post Next Post

Most Recent