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.
[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.
[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.
© 2024 OneMinuteCode. All rights reserved.