for and while loops. This comes in addition to the loop conditions.
Break statement
Thebreak statement is a loop control mechanism that terminates the loop and jumps out of it to the 1st statement (if any) after the loop.
The definition is simply the keyword break followed by a semicolon.
break statement, when i equals 5 we hit full stop. Thus the loop will print only numbers 1-4.
Continue statement
Thecontinuestatement is a loop control mechanism that skips the remainder of the statements in the code block and fast-forwards to the next iteration of the loop. It goes directly to the next evaluation of the condition.
The definition is simply the keyword continue followed by a semicolon.
continue statement, when i equals 3 we short-circuit the loop and go directly to i++. Thus the loop will print all numbers 1-9 except 3.
As always, it is crucial that you update the counter of
while loops. Pay attention to where you place your update in relation to the continue statement!