Break and Continue
#include <iostream.h> void main(){ int i; for(i=1; i<= 10; i++){ if(i%2==0){ continue; } cout<<i<<" "; } }
The output of the program is 1, 3, 5, 7, 9. Because when i=2, 4, 6, 8, 10 then the continue statement get exexuted and forced the loop to go to next iteration i.e i=3, 5, 7, 9; also it does not execute the "cout" statement.
Break: You have seen this statement used in switch statement to stop the progrom control to go to the next case same way the break statement is also used with loop to send the program control out of the loop. Simple words it terminates/ finishes the loop. See the example below-
#include <iostream.h>
void main(){
int i;
for(i=1; ; i++){
cout<<i<<" ";
if(i==10){
break;
}
}
}
See in the above program looping condition is not given, still it is not infinite loop. See in the loop body when the value of i=10 then the break statement will be executing and terminating the loop. The output is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
