Continue Statement in Java
Suppose a person wants code to execute for the values as per the code is designed to be executed but forcefully the same user wants to skip out the execution for which code should have been executed as designed above but will not as per the demand of the user. In simpler words, it is a decision-making problem as per the demand of the user.
Consider a man is climbing up to go to his house in between there are 11 stairs. Being in hurry to climb up he directly stepped onto 3 staircases and then 4, 5, 6, 7, 8, 9 and jumps to last one. During this he missed out staircase 1st, 2nd and 10th and he completed the goal to reach his house. He continued his journey skipping staircase of. his choices.
In computers, it interprets staircases which is/are supposed to be skipped as ‘continue’. The action to miss out execution which are supposed to be executed, is interpreted as continue statement be it any programming language.
Continue statement is often used inside in programming languages inside loops control structures. Inside the loop, when a continue statement is encountered the control directly jumps to the beginning of the loop for the next iteration instead of executing the statements of the current iteration. The continue statement is used when we want to skip a particular condition and continue the rest execution. Java continue statement is used for all type od loops but it is generally used in for, while, and do-while loops.
- In the case of for loop, the continue keyword force control to jump immediately to the update statement.
- Whereas in the case of a while loop or do-while loop, control immediately jumps to the Boolean expression.
Syntax: continue keyword along with a semicolon
Flow Chart of Continue Statement
The above flowchart is most important for the understanding of this keyword. Always remember the condition is always placed inside diamond boxes and statements in rectangular boxes. Now jumping onto the implementation part
Case 1: Continue statement inside for loop
In this program, illustration for how to use the continue statement within For loop. When the value of ‘i’ becomes 10 or 12, the continue statement plays its role and skip their execution but for other values of’ ‘i’ the loop will run smoothly.
0 1 2 3 4 5 6 7 8 9 11 13 14 15
Case 2: Continue statement inside while loop
In the above program, we give example, how to use the continue statement within the While loop. When the value of count becomes 7 or 15, the continue statement plays its role and skip their execution but for other values of the count, the loop will run smoothly.
20 19 18 17 16 14 13 12 11 10 9 8 6 5 4 3 2 1 0
Case 3: Continue statement inside do while loop
In the above program, we give example, how to use the continue statement within the do-While loop. When the value of i becomes 4 or 18, the continue statement plays its role and skip their execution but for other values of i, the loop will run smoothly.
0 2 6 8 10 12 14 16 20 22 24 26 28 30 32 34
Case 4: Continue statement inside Inner loop(Nested Loop)
In the above program, we give example, how to use the continue statement within Nested loops. When the value of i becomes 3 and j become 2, the continue statement plays its role and skip their execution but for other values of i and j, the loop will run smoothly.
1 * 1 1 * 2 1 * 3 2 * 1 2 * 2 2 * 3 3 * 1 3 * 3 4 * 1 4 * 2 4 * 3
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.