Is break and continue same? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Is break and continue same?

explain more?

13th Mar 2018, 12:49 PM
Code Race
Code Race - avatar
7 Answers
+ 7
Example: 1.Continue for (int a=0;a<=10;a++){ if (a%2==1){ continue;} System.out.print(a); } //Output:02468 2.Break for (int a=0;a<=10;a++){ if (a%2==1){ break;} System.out.print(a); } //Output:0
13th Mar 2018, 2:04 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 7
The break statement exits from the loop, ending the loop cycle. Continue simply goes to the next iteration of the loop.
13th Mar 2018, 8:15 PM
Scott
+ 5
break ends the loop, continue only ends the current iteration. while(true) break; while(true) continue; The first line will be executed once, the other one is an infinite loop.
13th Mar 2018, 12:54 PM
Chris
Chris - avatar
+ 5
well both are used in loops but they vary in purpose 1-break is used to exit the loop when certain condition is reached 2-continue is used to skip the rest of loop statements and goes back to the start of the loop
13th Mar 2018, 12:58 PM
Ashraf Al_Absi
Ashraf Al_Absi - avatar
+ 4
The break statement terminates the loop (code block) immediately when it is encountered. The continue statement just skip the current iteration of loop, and loop start new iteration after checking loop condition.
13th Mar 2018, 12:59 PM
H. Ahmadian
H. Ahmadian - avatar
+ 3
no pal, both aren't same, break statement is used to terminate the loop when your desire case comes out. eg : I wish to stop the loop on 5th iteration, for(I=1;I <=10;I++) if(I==5) break; cout << I << " "; output will be : 1 2 3 4 it's the job of break statement. the continue statement, is useful to skip the just current loop iteration not to terminate the loop. for(I=1;I <=10;I++) if(I==5) continue; cout << I << " "; output will be : 1 2 3 4 6 7 8 9 10 you can see, we missed 5 here for the same program. I hope you will got the difference.
13th Mar 2018, 11:22 PM
Ajay Agrawal
Ajay Agrawal - avatar
+ 2
No. They are not same. Break stops, terminates loop and continue skips iteration. Take a look at this https://www.sololearn.com/Discuss/873950/?ref=app
13th Mar 2018, 12:54 PM
Sad
Sad - avatar