Continue
Part of the Fundamentals section of Coddy's C journey — lesson 41 of 63.
The continue statement stops the current iteration and continues to the next iteration. For example:
for (int i = 3; i < 9; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}The loop will iterate through all of the numbers. When it reaches i=5 it will skip that iteration and continue to the next one. The output is:
3 4 6 7 8Notice, number 5 is not in the output.
Challenge
EasyYou are given a code that prints the numbers from 1 to 20 (including).
Your task is to add if and continue statements so that only the even numbers will be printed (2, 4, 6, ...).
Cheat sheet
The continue statement stops the current iteration and continues to the next iteration:
for (int i = 3; i < 9; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}This will output: 3 4 6 7 8 (skipping 5)
Try it yourself
#include <stdio.h>
int main() {
for (int i = 1; i <= 20; i++) {
printf("%d ", i);
}
return 0;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
3Operators
Arithmetic OperatorsModulo OperatorIncrement/DecrementAssignment OperatorsRelational OperatorsLogical Operators Part 1Logical Operators Part 2Logical Operators Part 3Recap Challenge