Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1282 :

Posted: Wed Sep 04, 2019 1:44 am
by p1880554z
What, if anything, is wrong with the following code? void test(int x){ switch(x){ case 1: case 2: case 0: default : case 4: } }


was thinking variable x in switch is not declared since it was only visible within the method

Re: About Question enthuware.ocajp.i.v8.2.1282 :

Posted: Wed Sep 04, 2019 1:55 am
by admin
What happened when you tried to compile it?

Re: About Question enthuware.ocajp.i.v8.2.1282 :

Posted: Mon Sep 27, 2021 6:49 am
by Seán Kennedy
Hi, point 4 in the explanation, the text states
All case labels should be COMPILE TIME CONSTANTS. This means, you can have literals such as 'a', 5, 10, and true or a final variable
. This suggests that true is a valid case constant. As you cannot switch on a boolean, is this a typo?

Also, very small type in number 3 where the text states
... the following will not compile because 300 cannot...
but the code example switches on 100 and 200 (and not 300).

Kind regards,
Seán.

Re: About Question enthuware.ocajp.i.v8.2.1282 :

Posted: Mon Sep 27, 2021 8:17 am
by admin
You are right. As the first bullet states, boolean is not allowed for switch. 300 should indeed be 200. Fixed.
thank you for your feedback!

Re: About Question enthuware.ocajp.i.v8.2.1282 :

Posted: Mon Sep 27, 2021 8:32 am
by Seán Kennedy
No problem at all. I think the same typos are in the explanation for enthuware.ocajp.i.v8.2.1196 also.

Re: About Question enthuware.ocajp.i.v8.2.1282 :

Posted: Mon Sep 27, 2021 8:58 am
by admin
Yes, it is a common explanation displayed in multiple questions. Fixed in all.