0

Why is output 17? it is calculated as follows: case 1( x= 3+3=6, case 3 (x=6+3=9), case 5 (x=9+3=12) , default (x=12+5=17)?

https://code.sololearn.com/c6oXUYzDr8QV/?ref=app

26th Mar 2021, 8:59 PM
Тимур Завьялов
Тимур Завьялов - avatar
5 Respuestas
+ 1
It is: switch(3) because x = 3; So it goes straight to case 3: (x = x + x = 6) Then it goes to case 5 because there is no break: (x = x + x = 12) Then it goes to default case cuz there is again no break: (x = x + 5 = 17)
26th Mar 2021, 9:53 PM
Michal Doruch
+ 1
There aren't any breaks in this switch case, so the code just go through every case after the one it matched. If you want the behavior to change, you can add the break explicitly: {x+=x; break;}
26th Mar 2021, 9:22 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
Is it so calculate?
26th Mar 2021, 8:59 PM
Тимур Завьялов
Тимур Завьялов - avatar
0
Can you explain me why it so?
26th Mar 2021, 9:00 PM
Тимур Завьялов
Тимур Завьялов - avatar
0
x=3 x= x+x = 3+3 =6 Then x=6 x = x+x = 6+6 =12 Then x=12 x = x+5 = 12+5 =17
27th Mar 2021, 9:44 AM
Rajeev kumar
Rajeev kumar - avatar