Can someone explain to me the logic behind this output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain to me the logic behind this output

Following is the code I've had in a quiz recently and I tried but am unable to understand logic behind the answer for the Output.. #include <iostream> using namespace std; int main() { int x=3; switch(3){ case 1: {x+=x;} case 3: {x+=x;} case 5: {x+=x;} default : {x+=5;} } cout <<x; return 0; OUTPUT: 17

1st May 2019, 10:55 AM
Abhishek Survase
Abhishek Survase - avatar
2 Answers
+ 6
You're not using break statements so cases 3, 5 and default are executed. x = 3 case 3: x+=x // x = 6 case 5: x+=x // x = 12 default: x+=5 // x = 17
1st May 2019, 10:59 AM
Anna
Anna - avatar
+ 1
Anna Omg I totally missed that.. Thankyou soo much
1st May 2019, 11:15 AM
Abhishek Survase
Abhishek Survase - avatar