Break and Continue Diagram in Java Example
Related Searches: java break, java continue, break statement in java, java break for loop, continue statement in java, break while loop, java exit for loop, break vs continue, java loop continue, continue vs break, java while loop break, what does continue do in java, break and continue in java, java continue vs break, break and continue, how to exit for loop in java, if continue, how to use break, java while loop continue, java exit while loop, continue in java loop, end for java, continue in java if statement, how to break a loop in java
ALSO READ: CopyOnWriteArrayList Class in Java [Complete Tutorial]
Introduction to Java break and Java continue statements
The Java break and continue statements are used to manage program flow. We can use them in a loop to control loop iterations. These statements let us to control loops and switch statements by enabling us to either break out of the loop or jump to the next iteration by skipping the current loop iteration.
In this tutorial, we will learn about the java break statement and java continue statements. We will cover how we can use them in a while and for loop by taking various examples. We will also see why the break statement is important in switch statements. In a nutshell, this tutorial is going to be one of the most informative tutorials about break and continue statements in Java programming language.
How Java break statement works (flow diagram)
The Java break statement is used to break the loop or switch statements. It breaks the current flow of the program at a specified condition. In the case of an inner loop, it breaks only the inner loop. The following is the simple diagram showing how the break statement works in a loop.
Notice that if the break statement/condition is TRUE
then the loop will stop and exit. But if the break condition is FALSE
, then body of the loop will be continue to be executed.
ALSO READ: Java date & time Format Explained [10+ Examples]
How Java continue statement works (flow diagram)
The Java continue statement is used to skip the body of the loop based on condition. So if the condition for continue statement is TRUE
then it will skip executing the body of the loop and will go to the start of the loop but if the condition is FALSE
then it will continue to go through the remaining body of the loop.
Using Java break and Java continue statements in a while loop
Java while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. You can learn more about the while loop from the article java while loop. The break statement is used in the while loop to control the loop and exit/stop the loop once the specified condition is achieved and the java continue statement is used to skip the execution of the loop for some specified conditions.
In this section, we will learn how we can use a java break and java continue statements in a while loop by taking examples, but first, let us discuss the syntax of the java break and java continue statements in a while loop.
ALSO READ: If Else Java Statement Explained [Easy Examples]
Syntax of Java break statement in a while loop
Java break statement stops the while loop even if the initial condition is true. The simple syntax of break statement in a while loop looks like this.
while(condition){ if(condition1){ break; } // while loop statements }
When the condition of the if statement inside the loops becomes true, the break statement will execute and the while loop will stop executing further.
Syntax of Java continue statement in a while loop
As we already discussed the java continue statement is used to skip the execution of the while loop for a specified condition. The simple syntax of the java continue statement looks like this:
while(condition){ if(condition1){ continue; } }
When the condition of the if statement inside the while loop becomes true, the continue statement will execute, and the rest of the statements of the loop will be skipped and a new iteration will start.
Example-1 Java break statement in a while loop
Now let us take an example and see if the break statement really stops the while loop or not. We will create an infinite while loop by giving a condition that is always going to be true, then we will use the break statement to stop execution when a specific condition becomes true. See the example below:
// class public class Main { public static void main(String[] args){ // variable int num = 0; // while loop while(true){ // condition for break if (num>5){ break; } // printing number System.out.println("counting...."+num); // incrementing the num num++; } } }
Output:
counting....0 counting....1 counting....2 counting....3 counting....4 counting....5
Notice that the loop was an infinite loop because the condition in the loop was always true but because of the break statement, we were able to stop the execution of the while loop.
ALSO READ: Java Switch Statement Explained [Easy Examples]
Example-2 Java continue statement in a while loop
Now let us take an example of a continue statement in a while loop. Let us say that we want to print the numbers from 1 to 10 skipping all the numbers that are divisible by 3. See the example below:
// class public class Main { public static void main(String[] args){ // variable int num = 1; // while loop while(num<11){ // condition for continue if (num%3==0){ num++; continue; } // printing number System.out.println("counting...."+num); // incrementing the num num++; } } }
Output:
counting....1 counting....2 counting....4 counting....5 counting....7 counting....8 counting....10
Notice that the java continue statement skipped all the numbers that were divisible by 3.
Java break and continue statement in a for loop
Java for loop provides a concise way of writing the loop structure. The for statement consumes the initialization, condition, and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. You can read about the for loop from the article about java for loop.
In this section, we will discuss how we can use break and continue statement in a java for loop.
Syntax of Java break statement in a for loop
The syntax of break statement inside for loop is similar to that of while loop. See the simple syntax of break statement inside for loop below:
for(initializer; condition; update){ if(condition1){ break; } // for loop statements }
When the condition of the if statement inside the for loop becomes true, the loop will be stopped.
ALSO READ: Thread in Java Explained [Simple Examples]
Syntax of Java continue statement in a for loop
The syntax of the java continue statement inside a for loop is similar to the one in the while loop. See the simple syntax below:
for(initializer; condition; update){ if(condition1){ continue; } // for loop statements }
When the condition inside the for loop becomes true, the continue statement will execute and all the other statements of the for loop will be skipped.
Example-1 Java break statement in a for loop
Now we know the basic syntax of the break statement in for loop, let us take a practical example and see how it actually works. In the following example, we will have an infinite for loop and we will use the break statement to stop execution once we get to number five.
// class public class Main { public static void main(String[] args){ // variable int num = 0; // java for loop for(; ;){ // condition for break statement if(num==5){ break; } // printing the number System.out.println("counting..."+num); num++; } } }
Output:
counting...0 counting...1 counting...2 counting...3 counting...4
Notice that we were able to successfully stop the execution of an infinite for loop using the break statement.
Example-2 Java continue statement in a for loop
Now, let us see how we can use the continue statement in java for loop. In the following example, we will print all the numbers from 1 to 10 excluding the multiples of 3. See the example below:
// class public class Main { public static void main(String[] args){ // java for loop for(int i =1; i<=10; i++){ // condition for continue statement if(i%3==0){ break; } // printing the number System.out.println("counting..."+i); } } }
Output:
counting...1 counting...2 counting...4 counting...5 counting...7 counting...8 counting...10
Notice that we were able to skip all the numbers divisible by 3.
ALSO READ: Java Predicate Explained [Practical Examples]
Java break statement in a switch statement
The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. You can read more about switch statements from an article java switch statements. In this section, we will see how we can use the break statement inside java switch statements. But first, let us see learn the syntax.
Syntax of Java break statement in a switch statement
The basic syntax of the Java break command in a switch statement is a little bit different than the while loop and for loop one. Because here we don't need to put if statement explicitly for a break statement. In fact, the cases act like if statements and we have to add the break command after each case. The simple syntax looks like this;
switch (key) { case value1: // statment break; case value2: // statment break; . . . default: // statement }
Notice that there are break statements after each case and when one of the cases becomes true, the break statement will be executed and stops the switch statement.
ALSO READ: Bitwise Operators in Java Explained [Practical Examples]
Example-1 Java break statement in switch statement
Now let us take an example and see how break statements in java switch works. See the example below:
public class Main { public static void main(String[] args){ // variable int num = 4; // java switch switch(num){ //case case 2: System.out.println("the number is 2"); break; //case case 4: System.out.println("the number is 4"); break; //case case 6: System.out.println("the number is 6"); break; //default case default: System.out.println("We couldn't find the number"); } } }
Output:
the number is 4
Notice that once the case becomes true, the statements inside it were executed along with the break statement, and the switch statement's further execution stopped.
Summary
The Java break and continue statements are the jump statements that are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression. These statements can be used inside any loops such as for, while, and switch statements. In this tutorial, we learned about the java break statement and the java continue statement.
We covered how we can use them in different java loops by taking various examples. Moreover, We also discussed the role of the break command in java switch statements by taking examples. All in all this tutorial covers all the necessary information that you need to know in order to start working with break and java continue statements.
ALSO READ: 8 different Escape Sequence in Java with Examples
Further Reading
Java break and continue
Java switch
Java loops
Source: https://www.golinuxcloud.com/java-break-continue-examples/
0 Response to "Break and Continue Diagram in Java Example"
Post a Comment