-3

If i is equal to 1, after this statement,

while (i++ <= 10){} 

i is taken as 2 i.e., incremented before evaluation in the block.

But if used in switch,

switch(i++){} 

i gets evaluated before incremented in the block.

Why these cases i++ behave differently?

Examples:

For While case:

#include <stdio.h>

int main()
{
    int age = 20;

    while (age++ <= 65)
    {
        if ((age % 20) == 0)
        {
            printf("You are %d years old\n", age);
        }       
    }

    return 0;
}

I expect this to print:

You are 20 years old
You are 40 years old
You are 60 years old

For switch case:

#include <stdio.h>

int main()
{
 int i = 0;

 while (i < 3)
 {
     switch (i++)
     {
         case 0:printf("print 0");
         case 1:printf("print 1");
         case 2:printf("print 2");
         default:printf("Oh no!");
     }
     putchar('\n');
 }

 return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Lyrk
  • 1,936
  • 4
  • 26
  • 48
  • How is that? I do not understand. – Sourav Ghosh Jun 01 '18 at 07:08
  • It should be the same in both cases. Please show a [mcve] and explain what you think the results should be, and what you're seeing that's different. – Barmar Jun 01 '18 at 07:09
  • @Barmar I added examples to both cases under the main post. – Lyrk Jun 01 '18 at 07:15
  • 2
    Put the code in the question, not at remote sites. – Barmar Jun 01 '18 at 07:17
  • In your "while" example, printing happens when `if ((age % 20) == 0)` is fulfilled. It has nothing to do with the `while` condition. – vgru Jun 01 '18 at 07:18
  • the *post-increment expression* does not behave differently (see the answers) but this illustrates two of the pitfalls with post-increment nicely :) – drRobertz Jun 01 '18 at 07:18
  • 2
    Note: all your case lables lack `break`. – Sourav Ghosh Jun 01 '18 at 07:19
  • Add the expected output of each program to the question, so we can understand what's confusing you. – Barmar Jun 01 '18 at 07:20
  • @Barmar I expect for while case to print: "You are 20 years old". Switch case is what I expected. No problem in there. – Lyrk Jun 01 '18 at 07:41
  • I'm voting to close this question as off-topic because it the next question today showing that the OP has not read the switch ... case chapter. He should buy a book , read it, complete all the exercises and start asking question. – 0___________ Jun 01 '18 at 07:48
  • @Lyrk `age % 20 == 0` is true for all multiples of `20`. So it should print 20, 40, and 60 years old. – Barmar Jun 01 '18 at 07:49
  • But it won't print `you are 20 years old` because it has incremented the variable before going into the body of the loop. – Barmar Jun 01 '18 at 07:51

4 Answers4

3

With this

while (i++ <= 10){} 

the following happens:

  • i starts off as 1
  • i (being one) gets compared to 10
  • i gets incremented to 2
  • the block gets executed, with i being 2

Interesting to note, if i starts off as 10, the while loop while be executed again, with i being 11.

With this:

switch(i++){}
  • i starts off as 1
  • the case to execute gets selected according to "1"
  • i gets incremented to 2
  • the case for "1" gets executed, with i being "2"
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
2

In fact, the real same happens in both case: the original value is used for comparison, it is incremented and the block is executed. Look at the following code:

#include <stdio.h>

int main() {
    int i=1;

    switch(i++) {
    case 1:
        printf("Case 1: %d\n", i);
        break;
    default:
        printf("Not 1: %d\n", i);
    }
    return 0;
}

Its output is:

Case 1: 2

which proves that i has been incremented before the block is evaluated.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

No, that cannot be the case.

In both the cases, the increment of value, the side effect of the post-increment operator takes place after the value computation of the result.

Quoting C11, chapter §6.5.2.4, (emphasis mine)

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).[...] The value computation of the result is sequenced before the side effect of updating the stored value of the operand. [...]

To elaborate

  • In case of while (i++ <= 10), the value of i, before increment is used to validate the loop condition.

  • In case of switch(i++){}, the value of i, before increment, is used to jump to the particular case. The unmodified (yet) value is used as the value of controlling expression, and after the evaluation of the controlling expression, the increment takes place.

To add, if you try to print the value of i in one of the case statements (say, case 1:), you can see it is in fact increased.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I added two examples under the main post. – Lyrk Jun 01 '18 at 07:16
  • @Lyrk Read the last part of my answer. – Sourav Ghosh Jun 01 '18 at 07:16
  • in while case, i's value before increment(1) is used to validate only but in while's block, i's incremented value(2) is used in calculations. In switch, i's before increment value is used in switch block. I mean 1 is used in switch block, not 2 as in the case of while. Why is that? – Lyrk Jun 01 '18 at 07:21
  • @Lyrk You're wrong. What makes you think that's happening? – Barmar Jun 01 '18 at 07:22
  • @Lyrk I think you're being fooled because of the fall-through behavior of `switch` when you leave out `break`. – Barmar Jun 01 '18 at 07:23
  • @Lyrk The jump to case, i.e, the value of teh controlling expression is computed before the value increment, so the old value is used to jump to teh case label. You try printing the value of `i` inside the case block and see for yourself, it'll print the incremented value. – Sourav Ghosh Jun 01 '18 at 07:23
  • @Barmar Quite possible, that is why I left a note below the oroginal question. :) – Sourav Ghosh Jun 01 '18 at 07:23
0

The while loop will not print You are 20 years old because the variable is incremented before executing the body of the loop. It started out as age = 20. The first

while (age++ <= 65)

tests whether 20 <= 65, so it goes into the loop. Then it increments age, so inside the loop it's age = 21. As a result, the test if (age % 20 == 0) will not succeed, and you don't get a message saying You are 20 years old.

The other multiples of 20 still print during future iterations of the loop.

Barmar
  • 741,623
  • 53
  • 500
  • 612