Basic Java – 18 || Difference between break and continue keywords.

Both break and continue are the reserved keywords in java. These are mainly used while using switch or loops in the program.

Workflow of the program by using break and continue keywords.

break and continue

In the above image, we can see the difference between these two keywords. When we are using a break keyword it exits from the current loop or a switch block immediately.

  • So, the output of the above code will be, as the control exit from the loop when the value of i=5.
    • 1
    • 2
    • 3
    • 4

On the other hand, when we are using a continue keyword it skips the remaining statements and moves to the next iteration but never exit from the loop. It exits from the loop only when it reaches to the condition defined by the loop.

  • So, the output for the continue statement will be as mentioned below. It only skips the value 5 while printing but the loop would remain continue until value of i=10.
    • 1
    • 2
    • 3
    • 4
    • 6
    • 7
    • 8
    • 9
    • 10

break – Code Example

package breakContinue;

/**
 * @author ashok.kumar
 *
 */
public class breakContinueExample {
	public static void main(String[] args)
	{
		  for(int i=1;i<=10;i++) 
		  { 
			  if(i==5) 
				  break; 
			  System.out.println(i); 
		  }
		  System.out.println("Execution completed...");		
	}
}

Console Output

 1
 2
 3
 4
 Execution completed…

continue – Code Example

package breakContinue;

/**
 * @author ashok.kumar
 *
 */
public class breakContinueExample {
	public static void main(String[] args)
	{
		  for(int i=1;i<=10;i++) 
		  { 
			  if(i==5) 
				  continue; 
			  System.out.println(i); 
		  }
		  System.out.println("Execution completed...");		
	}
}

Console Output

 1
 2
 3
 4
 6
 7
 8
 9
 10
 Execution completed…

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


1462total visits,2visits today

Leave a Reply

Your email address will not be published. Required fields are marked *