Before going into the basics of break and continue statements, we need to address their use in any given programming language. We’re already familiar with loops such as “for” loops, “while” loops, “do-while” loops. Essentially a loop is simply a code piece that helps to iterate through any given data structure. The data structure can be anything varying from Arrays to Trees.
In simple words, the break statement lets you break a “while” or a “for” loop somewhere in the middle. The loop will undeniably end after the condition, but break essentially helps to terminate it whenever we require and to it clearly saves auxiliary iterations.
An important note: Break statement only works in the case of loops. It doesn’t work in “if” statements.
Coming to the “Continue” statement, in contrast to the “Break” statement it is utilized in loop control structure whenever the code needs to:
The “Continue” statement incorporated in Java is merely used to continue the loop. Continue with the current flow of the program and skip the remaining code under specific conditions. In the case of the inner loop, only the inner loop continues.
If during any iteration, “Continue” is encountered the control flow will directly shift at the beginning of the loop disregarding the instructions below it
This statement has following uses: It is used to terminate the execution of a loop. It is used to come out of switch statement.
Knowledge2life
Knowledge2life
This statement is used to force an early iteration in loop skipping subsequent statements
Knowledge2life
Knowledge2life
Knowledge2life
Knowledge2life
Knowledge2life
|