-2
// 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++?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 4
    Your `if` block is never entered, so `continue` plays no part in your program's behavior. – Miles Budnek Aug 01 '23 at 17:59
  • 2
    `continue` does not work there. – 273K Aug 01 '23 at 17:59
  • 9
    Since the answer to your question is "Yes. Someone can explain the code" and that's completely useless for everyone, my recommendation is you step through the program with whatever debugger came with your development tools (get different tools if somehow you got a toolchain with no debugger) and watch exactly what happens as it happens. Debuggers are invaluable tools in understanding how a compiler interpreted code. – user4581301 Aug 01 '23 at 17:59
  • 1
    Key bit of knowledge needed to understand the code: 0 is always interpreted as false. – user4581301 Aug 01 '23 at 18:00
  • 1
    The if(0) part can be removed as it doesn't do anything. So basically, it's counting by 3, two in the loop increment, an one more during the i++ part. – JosephDoggie Aug 01 '23 at 18:01
  • 3
    whats the difference between "Output" and "I am getting" ? What makes you believe that the output should be different from the one you get? – 463035818_is_not_an_ai Aug 01 '23 at 18:05
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Aug 02 '23 at 06:55
  • please do not change the question after you received answers. The answers refer to code that has `if(0)`. Chaning your question invalidates the answers. Anyhow this question was closed, it is still unclear why you think the code should produce the "Output". If you didnt ask the question you wanted to ask you can open a new question – 463035818_is_not_an_ai Aug 02 '23 at 07:15

2 Answers2

4

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
3

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.

atru
  • 4,699
  • 2
  • 18
  • 19