+ 6
What is break
9 Answers
+ 17
Actually, we use break statement when we need to get out of a loop(execution). It stops the execution of the loop then and there! For Eg.,
int i;
for(i=1; i<5; i++) {
if(i==3)
break;
}
System.out.print(i); /* outputs 3. As the loop only runs thrice... */
+ 13
@Mark :
On certain occasions, it is useful to force an early iteration of the loop i.e., you might want to continue running the loop but stop processing the remainder of code in its body for this particular iteration. This is, in effect, a goto just pass the body of the loop to the end of the loop. The continue statement performs such an action.
In while and do-while loops, the continue statement causes a control to be transferred directly to the conditional expression that controls the loop. In the for loop, the control goes first to the iteration portion of the forstatement and then to the conditional expression. And for all three loops (for, while, and do-while), any intermediate code is bypassed.
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if(i%2 == 0)
continue;
System.out.println();
}
/* This code prints :
0 2 4 6 8
and
1 3 5 7 9 */
+ 11
break is also used to prevent fall through in switch case i.e. it makes the control come out of the switch block without executing any further case statements........
+ 11
of course! @Chirag is right :)
+ 9
@Dayve your code should print this
0 1
2 3
4 5
6 7
8 9
see here
https://code.sololearn.com/c6DHnlwhBdw8/?ref=app
+ 6
There is also a continue statement that can be used, but I don't fully understand its function. Can someone clarify?
+ 6
@Dayve
Thank you sir, that is very helpful.
0
It is a statemen to vome out if condition satisfy