About Question com.enthuware.ets.scjp.v6.2.257 :
Moderator: admin
About Question com.enthuware.ets.scjp.v6.2.257 :
Can Switch block only contain final constants and integers? Can the case expression be a local variable?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
If you mean something like:
int x = 0;
int y = 1;
switch(x){
case y : do something; //this is invalid.
}
Then, no.
HTH,
Paul.
int x = 0;
int y = 1;
switch(x){
case y : do something; //this is invalid.
}
Then, no.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
Thank you. Yes, I was referring to that exact situation. I have come upon various ambiguous answers to the same.
-
- Posts: 1
- Joined: Tue Feb 03, 2015 10:59 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
for(;;)
{
Math.random()<.05? break : continue;
}
Sorry but who can explain me whats wrong here?? NOt short please if you can or links would be appreciated!
{
Math.random()<.05? break : continue;
}
Sorry but who can explain me whats wrong here?? NOt short please if you can or links would be appreciated!
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
What did the compiler say when you tried to compile it?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu May 14, 2015 3:25 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
Can you tell me, why your answer has addition with such statement: "Even if break is inside the else block, it prevents the "fall through" to case 7." ? Because I tried a lot examples with switch, and this is just not true...
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
Can you post the code that you tried? And did the control go into your else block?
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu May 14, 2015 3:25 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
Code: Select all
class Test
{
public static void main(String args[])
{
int k = 9, s = 5;
switch(k)
{
default :
if( k == 10) { s = s*2; }
else
{
s = s+4;
break;
}
case 7 : s = s+3;
}
System.out.println(s);
}
}
Code: Select all
k = 9;
Code: Select all
k = 10;
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.257 :
Ok, I see the confusion. The explanation that you've quoted is associated with that particular option. That is why it says, "....fall through to case 7". In the given situation, the control does go to else and it is trying to explain that it is not necessary for break statement to appear to be out of the if/else part. In the given code, even though the break statement is in the else part, it will still cause the case to break.
The statement that you've quoted is not a general rule that break in else will cause the case to break. Of course, the condition has to be false first so that the break is executed.
HTH,
Paul.
The statement that you've quoted is not a general rule that break in else will cause the case to break. Of course, the condition has to be false first so that the break is executed.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 66 guests