Skip to main content

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");


Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...