+ 6

What is break

26th May 2017, 4:16 PM
utkarsh yadav
utkarsh yadav - avatar
8 Respostas
+ 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... */
26th May 2017, 4:28 PM
Dev
Dev - avatar
+ 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 */
26th May 2017, 6:14 PM
Dev
Dev - avatar
+ 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........
26th May 2017, 4:36 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 11
of course! @Chirag is right :)
26th May 2017, 4:38 PM
Dev
Dev - avatar
+ 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
27th May 2017, 2:38 AM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 6
There is also a continue statement that can be used, but I don't fully understand its function. Can someone clarify?
26th May 2017, 6:07 PM
Mark Paramonte
Mark Paramonte - avatar
+ 6
@Dayve Thank you sir, that is very helpful.
26th May 2017, 7:36 PM
Mark Paramonte
Mark Paramonte - avatar
0
It is a statemen to vome out if condition satisfy
28th May 2017, 7:28 AM
fahad
fahad - avatar