c++ problem

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{

int i =8,x=3;
switch (i % 2 ? 0:1)
{
case 0: x/=2;
case 1: x+=3;
case 2: x*=4;
}
cout<<"x = "<<x;
return 0;
}
why x=24 and switch (i % 2 ? 0:1) what it mean
anyone can explain to me
i % 2 evaluates as 0 ...
... which translates to boolean false ...
... which means that the result of the ternary expression i % 2 ? 0 : 1 is the latter of the two options, namely 1.

So the switch does case 1: that is, it adds 3 to x, making x become 6.

THEN

it drops through to the next case 2 (because there was no break; in between) ...
... multiplies x by 4 ...
... making x become 24.

Last edited on
thank you your explanation.
i understand.
Registered users can post here. Sign in or register to post.