About Question com.enthuware.ets.scjp.v6.2.257 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.257 :

Post by ETS User »

Can Switch block only contain final constants and integers? Can the case expression be a local variable?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by admin »

If you mean something like:

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.

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by Guest »

Thank you. Yes, I was referring to that exact situation. I have come upon various ambiguous answers to the same.

faristhebest
Posts: 1
Joined: Tue Feb 03, 2015 10:59 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by faristhebest »

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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by admin »

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.

pushpull
Posts: 14
Joined: Thu May 14, 2015 3:25 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by pushpull »

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...

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by admin »

Can you post the code that you tried? And did the control go into your else block?
Paul.
If you like our products and services, please help us by posting your review here.

pushpull
Posts: 14
Joined: Thu May 14, 2015 3:25 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by pushpull »

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);
   }
}
This is the code from the question. If you change the

Code: Select all

k = 9;
to

Code: Select all

k = 10;
it won't fall into the else and the break won't happen.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.257 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 66 guests