wellnwill Marathahalli Bangalore

Looping/iterative Statements

Loops are used to repeat a block of code.

for() loop

If we know how many times loop statements will be executed then use for loop.

  • 1st variable initialization will be performed only once.
  • 2nd condition will be checked.
  • 3rd Body of the loop will be executed.
  • 4th variable updation will be performed, again start from 2nd statement.

Curly bracket is optional when for loop has only one statement but that statement should not be declarative statement.


Valid loop -

1.	for(int i=0,true;i++)                           
 
2.      for(int i=0;true;i++) ; 
        System.out.println(“Hi”);

Invalid loop -

for(int i=0;true;i++)  
     int x=10;

variable initialization:

In this section, we can declare and initialize local variable of for loop. We can declare any number of variables with same data type.

Valid loop ->

1.	for(int i=0;true;i++) {}              2. for(int i=0,j=0,k=0;true;i++) {}                                         }     

Invalid loop ->

1.	for(int i=0,int j=0;true;i++) {}        2.    for(int i=0,short j=0;true;i++) {}   

In this section, we can write any valid java statement.

class Test0138
{
    public static void main(String[] args)
	{
     	int i=0; 
        for(System.out.println("Hi"); i<1; i++) 
        {
            System.out.println("Hello");
        }       
    }
}
class Test0139
{
  public static void main(String[] args)
  {
    int i=0; 
    for(System.out.println(“Hi”); i<1; System.out.println(“Hello”)) 
    {
        i++;
    }
 }
} 

condition :

It should be a valid java boolean expression. This section is optional if user is not providing any value for this section, complier will add true boolean literal by default. All three section of for loop are optional.

•	for(; ; )//infinite loop
    {
        System.out.println(“Hello”);
    }    
•	for(; ;);    // infinite loop
class Test137
{
  public static void main(String[] args)
  {
   System.out.println('A'); 
   System.out.println('B'); 
   System.out.println('C'); 
   System.out.println('D'); 
   System.out.println('E');    
  }
}
// O/P - A,B,C,D,E,
class Test138
{
  public static void main(String[] args)
  {
    for (int i=65;i<70;i++)
    {
       System.out.println((char)i); 
    }    
  }
}
// O/P - A,B,C,D,E
class Test0137
{
  public static void main(String[] args)
  {  
        int i=0;
         for (i=0;i<2;i++);//executed for i=0,1,2
         {
          System.out.println(i); //local block
         }    
  }
}
//O/P - 2
class Test117
{
  public static void main(String[] args)
  {
    for(int i=0;true;i++) 
    {
      System.out.println("Hi");
    } 
    System.out.println("Bye");   
  }
}
// O/P - CompileTimeException
           - unreachable statement
class Test118
{
  public static void main(String[] args)
  {
   for(int i=0;i<1;i++) 
   {
    System.out.println("Hi");
   } 
   System.out.println("Bye");   
  }
}
// O/P - Hi,Bye
class Test119
{
  public static void main(String[] args)
  {
   for(int i=0;;i++) 
   {
    System.out.println("Hi");
   } 
   System.out.println("Bye");   
  }
}
// O/P - CompileTimeException
           - unreachable statement
class Test120
{
  public static void main(String[] args)
  {
   for(int i=0;false;i++) 
   {
    System.out.println("Hi");
   } 
   System.out.println("Bye");   
  }
} 
// O/P - CompileTimeException
           - unreachable statement
Footer with Map