// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i<=15;i+=2){
cout<<i<<" ";
if (0){
continue;
}
i++;
}
}
Output - 0 3 5 7 9 11 13 15
I am getting 0 3 6 9 12 15
How does continue work. Does it skip if block only or whole for loop ? why is it not going on i++?
To understand what happens in your
forloop you need to keep in mind which value ofienters the loop. In C++ it is the current value, starting in your case withi = 0.The code never enters the
if (0)block since0is treated asfalse. Therefore, the loop increments i by 1 each time it evaluates. So given that in the first round of evaluation $i = 0$ as it enters the loop body, it exits it beingi = 1. Now the loop increment rule adds 2 toi, making iti = 3which you see in the input.Next steps all follow the same pattern. You add a 1 in the loop body, then 2 at the next "entrance" making
ia 6 etc. I suggest you follow this explanation with a piece of paper and understand the remaining outputs yourself.