+ 1
What is difference between break and continue statements in c++?
4 Antworten
+ 1
break: Break out of the current loop and proceed to the next line of code
continue: Go back to the loop statement and iterate (i.e. using this, one can skip the code for some specific value of a variable)
Example for continue:
for(a=1;a<=5;a++) {
if(a==3) {
continue;
}
cout<<a;
}
The above code will print all integers from 1 to 5 except 3 because the continue statement forced the program to skip the code below and continue iteration/looping.
+ 1
Break will terminate your statement (switch, for, do, while).
Continue will skip your statement.
eg,
for(i=1; i<=5; i++){
if(i == 2){
continue;
}
if(i == 4){
break;
}
cout << i;
}
will return : 13
+ 1
Break 'breaks out' (leaves) the looping block as if it were the LAST thing for the loop to do.
Continue is like break, except it jumps immediately to the NEXT loop iteration (conditions are tested and so of course it may also exit the loop).
0
The major difference between break and continue statements in C language is that a break causes the innermost enclosing loop or switch to be exited immediately. Whereas, the continue statement causes the next iteration of the enclosing for , while , or do loop to begin.