wellnwill Marathahalli Bangalore

For-each loop (Enhanced for loop)

The Enhanced for loop is specially designed loop to retrieve the elements from collection/Array.

while loop:

while loop is a looping control statement that allows code to be executed repeatedly based on a given condition.

If we do not know how many times loop statements will be executed then use while loop. Syntax:

 while (boolean condition) 
{
    statements to be executed
}

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

class Test122
{
  public static void main(String[] args)
  {
    while(1) 
    {
      System.out.println("Hello");
    } 
   System.out.println("Hi");   
  }
}
//O/P - CTE - int can not be converted to boolean.
class Test123
{
  public static void main(String[] args)
  {
    while(true) 
	{
      System.out.println("Hello");
   } 
   System.out.println("Hi");   
  }
}
//O/P - CTE - unreachable statement
class Test124
{
  public static void main(String[] args)
  {
     while(true) 
     System.out.println("Hello");
   }
}
// O/P - infinite loop
class Test125
{
   public static void main(String[] args)
   {
     while(true);
   }
}
// O/P - infinite loop
class Test126
{
   public static void main(String[] args)
   {
     while(true)
     int i=10;
   }
}
// O/P - variable declaration is not allowed here.
class Test127
{
  public static void main(String[] args)
  {
     while(true)
     {
       int i=10;
     }
  }
}
// O/P - infinite loop.
class Test129
{
  public static void main(String[] args)
  {
    final int a=1,b=2;
    while(a
class Test128
{
  public static void main(String[] args)
  {
    int a=1,b=2;
    while(a

do while loop:

A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of the block.

Syntax:

do{
  statements to be executed  
}while(boolean condition);  

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

class Test130
{
  public static void main(String[] args)
  {
    do
    System.out.println("Hi");  
    while(true) ;
  }
}
//O/P - infinite loop
class Test131
{
  public static void main(String[] args)
  {
   do; 
   while(true) ;
   }
}
// O/P - infinite loop

class Test132
{
  public static void main(String[] args)
  {
    do
    byte b1=10;
    while(true) ;
  }
}
//O/P - CTE - variable declaration is not allowed here

class Test133
{
  public static void main(String[] args)
  {
    do
   {
     byte b1=10;
   }
    while(true) ;
   }
}
// O/P - infinite loop
class Test134
{
  public static void main(String[] args)
  {
   do
   {
   byte b1=10;
   }
   while(true) ;
   System.out.println("Welcome");
   }
}
// O/P - CTE - unreachable statement


class Test135
{
  public static void main(String[] args)
   { 
    int x=1,y=2;
    do
     {
     byte b1=10;
     System.out.println(b1);
     }
    while(x>y);
    System.out.println("Welcome");
   }
}
// O/P - 10,welcome
class Test136
{
  public static void main(String[] args)
   { 
    final int x=1,y=2;
    do
    {
     byte b1=10;
     System.out.println(b1);
    }
    while(x
class Test0131
{
  public static void main(String[] args)
  {
   do; 
   while(true) ;
   System.out.println("Hi");
   }
}
// O/P - CTE - unreachable statement(“”Hi)

Transfer Statement :

These statements are used to transfer the control from one place to other place.

break statement:

Break Statement is used to break: for Loop , while Loop , do-while loop , switch and labeled block. The break statement has two forms: labeled and unlabeled. An unlabeled break statement terminates the innermost switch or loop statement, but a labeled break terminates a specific loop. We can not use break statement without switch, loop and labeled statement.

class Test139{
  public static void main(String[] args)
  {
    for (int i=0;i<5;i++)
    {
        System.out.println(i);
        break;		
    } 
    System.out.println("outside loop")
   }
}
// O/P - 0, outside loop






class Test140
{
  public static void main(String[] args)
  {
     for (int i=0;i<5;i++)
     {
       if(i==3)
	   {
          System.out.println(i);
	      break;
	   }
	   System.out.println(i);
     } 
     System.out.println("outside loop");	  
  }
}
// O/P - 0,1,2,3, outside loop
class Test141
{
  public static void main(String[] args)
  {
     for (int i=0;i<5;i++)
      {
        k1:{              //labeled block
	    if(i==3||i==4)
	    {
            System.out.println(i);
		    break k1;
	    }
      }
   } 
    System.out.println("outside loop");	  
  }
}
// O/P - 3,4 outside loop
class Test142
{
  public static void main(String[] args)
  {
	    int i=3;
	    k1:   //labeled block
	    {
	     if(i==3)
         {
            System.out.println(i);
		    break k1;
	      }
	    } 
    System.out.println("outside loop");	  
  }
}
// O/P - 3, outside loop
class Test144
{
  public static void main(String[] args)
  { 
     int i=3;
	 if(i==3)
	 {
        System.out.println(i);
	    break;
	  } 
   }
}
// O/P - CTE - break outside switch or loop.

continue Statement:

Continue statement is used to continue the execution of the current looping statements with the next iteration of the loop. There are two forms of continue statement in Java.

  • Unlabeled Continue Statement
  • Labeled Continue Statement

Continue Statement skips the Loop and Re-Executes Loop with new condition. We can not use continue statement without loop statement.

Labeled break and continue:

We can use labeled break to break a particular labeled loop in nested loops and labeled continue to continue a particular labeled loop in nested loops.

class Test143{
  public static void main(String[] args)
  {
	  for (int i=0;i<5;i++)
	  {
	    if(i%2!=0)
		{
		 continue;
		}
       System.out.println(i);		
      } 
    System.out.println("outside loop");	  
  }
}O/P-0,2,4,outside loop
class Test145{
  public static void main(String[] args)
  { 
      int i=3;
	  if(i==3)
      {
            System.out.println(i);
	        continue;
	  } 
  }
}
// O/P - CTE-continue outside loop.



L1:  //label L1
 for (int i=0;i < 3;i++)
  {
     System.out.println("i loop");	
     L2: //label L2
     for (int j=0;j<3;j++)
     {
         System.out.println("j loop");
         L3: //label L3
	 for (int k=0;k<5;k++)
	 {
	    System.out.println("k loop");
	    break L1;
	    break L2;
	    break;
        }

      }
               
    } 
Footer with Map