- 5

why is the output 40?

I do not get the default statement .Why are we looking for variable c ??instead we should have looked for value of a to get the right output as we see that switch case is applied for variable a . Maybe m wrong or not getting things right ,if anyone could help me with this. Thankyou

8th Jul 2017, 4:34 AM
ÂñĂșrĂ„dhā ƞÀdĂŁv
ÂñĂșrĂ„dhā ƞÀdĂŁv - avatar
3 RĂ©ponses
+ 14
You need to post the code here. Lessons/codes don't link here automatically.
8th Jul 2017, 4:45 AM
Hatsy Rei
Hatsy Rei - avatar
0
Hi @Anuradha, I'm assuming that you're referring to the last question in the switch statement section of the Java course. I'll copy that question below and try to explain it. ---------------------------------------------------- What is the output of the following code? int a = 11; int b = 12; int c = 40; switch (a) { case 40: System.out.println(b); break; default: System.out.println(c); } ---------------------------------------------------- Looking at the second line of code you can see that the variable being tested in this switch statement is "a". The switch statement will take that variable and test it to see if it matches any of the cases below it. If "a" does not match any of the cases, then the default case will be used. The value of "a" is 11, and that does not match the value in "case 40." Thus, the program uses the default case and prints the value of "c", which is 40. If the value of "a" was 40 instead of 11, then the first case in the switch statement would be used instead of default. Here is the same code as before, except I changed the value of "a" to 40 and "c" to 11 to show you what I mean: ------------------------------------- int a = 40; int b = 12; int c = 11; switch (a) { case 40: System.out.println(b); break; default: System.out.println(c); } ------------------------------------- The above switch statement will match "a" to "case 40" and the program will print out the value of b, which is 12.
19th Jul 2017, 7:35 AM
RUB1C0N