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
About Question enthuware.ocajp.i.v8.2.1282 :
Moderator: admin
-
- Posts: 2
- Joined: Wed Sep 04, 2019 1:41 am
- Contact:
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1282 :
What happened when you tried to compile it?
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Feb 17, 2021 6:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1282 :
Hi, point 4 in the explanation, the text states
Also, very small type in number 3 where the text states
Kind regards,
Seán.
. This suggests that true is a valid case constant. As you cannot switch on a boolean, is this a typo?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
Also, very small type in number 3 where the text states
but the code example switches on 100 and 200 (and not 300).... the following will not compile because 300 cannot...
Kind regards,
Seán.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1282 :
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!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Feb 17, 2021 6:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1282 :
No problem at all. I think the same typos are in the explanation for enthuware.ocajp.i.v8.2.1196 also.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1282 :
Yes, it is a common explanation displayed in multiple questions. Fixed in all.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 142 guests