What is the "continue" keyword and what role does it play in Java?

Asked 1 years ago, Updated 1 years ago, 124 views

I saw the keyword continue for the first time, and what it is, what it is, what it does, and when I should do, and when I should use it I'm curious.

java continue keyword

2022-09-21 17:00

1 Answers

The continue statement can only be used within the repeat statement, and if the continue statement is encountered during the repetition, it moves to the end of the repeat statement and moves on to the next iteration. In the case of a for statement, it moves to an increase or decrease expression, and in the case of a while statement and a do-while statement, it moves to a conditional expression. The continue statement differs from the break statement in that it continues to perform the next iteration without departing from the entire repetition statement. It is mainly used in conjunction with the if statement, so that if certain conditions are satisfied, the sentences after the continue statement are not performed, and proceed to the next iteration. It is useful to exclude cases where certain conditions are satisfied during the entire repetition. 1

[Execution result]
1 
2 
4 
5 
7 
8 
10 

The number between 1 and 10 was output, but those that are multiples of 3 were excluded. When divided by 3 using the remaining operators, if there is no remainder, it is a multiple of 3, so in this case, the continue statement is executed and the subsequent sentence is not executed, and the next iteration is carried out.

When multiple repetitive statements are overlapped, you can leave one or more repetitive statements or skip repetition by naming them in front of the repetitive statements and naming break and continue statements. 2

[Execution result]
2*1=2 
2*2=4 
2*3=6 
2*4=8 

This is an example of outputting a multiplication table. The outermost For door was named Loop1. And when j is 5, break statements are performed. A break statement without the name of the repeat sentence can escape only one repeat sentence to which it belongs, but if you name the repeat sentence and name the break sentence, you can escape one or more repeat sentences. When j was 5, it was only output up to the fourth line of the second stage because it was moved out of Loop1. If the name of the repeating sentence was an unspecified break sentence, four lines from 2nd to 9th would have been printed. Let's predict what results will be obtained by changing the three annotated break statements and continue statements and compare them with the execution results.


2022-09-21 17:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.