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;
}