Skip to toolbar

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:


Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 1
  • Page views today : 1
  • Total visitors : 49,375
  • Total page view: 67,972

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.