Course Introduction to Programming
Break and Continue
In this lesson, you will learn two commands, continue and break.
To exemplify the commands, we will use the following program as a base, that prints the numbers from 0 to 5.
#include<stdio.h>
int i;
for(int i = 0; i < 6; i++){
printf("%d\n", i);
}
Continue
The continue command skips the rest of code and jumps to the next iteration (cycle) of a repetition structure.
#include<stdio.h>
int i;
for(int i = 0; i < 6; i++){
if(i == 2){
continue;
}
printf("%d\n", i);
}
When executing this program, we can notice that the number 2 is not printed. This happens because when
Break
The break command ends a repetition loop.
#include<stdio.h>
int i;
for(int i = 0; i < 6; i++){
if(i == 2){
break;
}
printf("%d\n", i);
}
In this case, when running the program, we can notice that only the numbers 0 and 1 are printed. This happens because when the
Now you know how to use continue and break commands to manipulate repeat structures.