Can someone explain the output of this code

118 views Asked by At
// 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++?

2

There are 2 answers

0
atru On

To understand what happens in your for loop you need to keep in mind which value of i enters the loop. In C++ it is the current value, starting in your case with i = 0.

The code never enters the if (0) block since 0 is treated as false. 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 being i = 1. Now the loop increment rule adds 2 to i, making it i = 3 which 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 i a 6 etc. I suggest you follow this explanation with a piece of paper and understand the remaining outputs yourself.

0
463035818_is_not_an_ai On

In your code, here:

    if (0){
        continue;
      
    }

The integer literal 0 is contextually converted to bool, the result is always false (non-zero integers would yield true). The if statement can be removed without altering the behavior, because continue is never actually executed:

int main() {
    
    for (int i = 0; i<=15;i+=2){
        cout<<i<<" ";
        i++;
    }            
}

If possible either increment the counter in the body (of a while loop) or via the iteration-expression of the for loop, but not both. This is simpler and achieves the same as your code:

    for (int i = 0; i<=15;i+=3){
        cout<<i<<" ";
    }         

On the other hand, to get this output:

 0 3 5 7 9 11 13 15

You can increment in every iteration by 2, only in the first iteration, when i==0 you increment one additional time:

    for (int i = 0; i<=15;i+=2){
        cout<<i<<" ";
        if (i == 0) ++i;
    }         

How does continue work. Does it skip if block only or whole for loop ?

continue skips the remaining part of the enclosing loop. For details see here https://en.cppreference.com/w/cpp/language/continue.

why is it not going on i++?

In your code continue has no effect on the following i++ because if(0) is a dead branch.


If you want to use continue you can turn above logic around and do the additional increment in every iteration, but before that you continue when i is not 0:

    for (int i = 0; i<=15;i+=2){
        cout<<i<<" ";
        if (i != 0) continue;
        ++i;
    }  

The condition can be written as if (i) (see above, contextual conversion of 0 to bool yields false). Perhaps that is where you are coming from and instead of removing the != 0 you removed i != from the condition.