my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch doubt.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch doubt.." Watch "switch doubt.." New topic
Author

switch doubt..

Surekha Meda
Greenhorn

Joined: Sep 15, 2005
Posts: 1
Hi Ranchers,

1. which of the following are true for a switch statement?

A]all switch statements must have a default label
B]there must be atleast one label for each code segment
C]keyword continue can never occur
D]no case label may follow a default label
E]char literal can be used

I'm sure about 'E'. Is 'B' true or false?

Regards,
Surekha.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
1. which of the following are true for a switch statement?

A]all switch statements must have a default label

False. default is not mandatory.

B]there must be atleast one label for each code segment

False. Here's where the compiler is your true friend. Just the kind of stuff that might make it to the SCJP. Who in their right mind would write an empty switch?! Ours not to reason why...

C]keyword continue can never occur

True. continue belongs to loops (for, while and do ... while). The very purpose of a continue stmt is to skip the rest of the execution of a loop so in a switch construct a continue doesn't make sense.

D]no case label may follow a default label

Not true. There is no restriction on the placement of the default, or the case statements, for that matter. case stmts don't need to be "sequential", as in a case 4 doesn't have to be later than a case 2 or a case 1, if all three of them are present.

E]char literal can be used

True. case statements will accept any integer argument less than long. Hence byte, short, char and int can be used (as chars nothing more than unsigned 16 bit ints). Anything that can implicitly be promoted to an int is legal.

I'm sure about 'E'. Is 'B' true or false?
Have you tried the compiler?
Also one more comment in passing: case arguments MUST be int literals OR constants (final int values (or anything that can be promoted to int)). NOTHING ELSE. You've been warned!

Sashi
[ December 09, 2005: Message edited by: Sasikanth Malladi ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Referring to the JLS, it is true that it is legal to have a switch statement with an empty block but when it isn't empty, a code segment must be preceded by at least one label. So I would say the answer to B is true.


Junilu - [How to Ask Questions] [How to Answer Questions]
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Hi sasikanth,

C]keyword continue can never occur

True. continue belongs to loops (for, while and do ... while). The very purpose of a continue stmt is to skip the rest of the execution of a loop so in a switch construct a continue doesn't make sense.


we can place continue statement in switch when the switch statement is in a loop.Try the following program



Regards,
Krishna.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Junilu, you're right. I misunderstood the question (and that's how one loses points on the exam).
Hari, in this case the continue belongs to the for (the innermost loop from the continue) and not to the switch. If you put in a print stmt after the switch ends but before the for ends, the print stmt will be skipped.
If you had a return in the switch would you say that the switch would terminate upon hitting the return or the method enclosing the switch?
Sashi
[ December 10, 2005: Message edited by: Sasikanth Malladi ]
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Hi sasi,

you are right. I will accept that too. But if a question appears in exam, what we have to answer? It belongs to the loop I know, but placing "continue" statement in switch is legal u know. I am thinking in that way....
Plz give me..feedback.. whether my assumption is correct or not?

Regards,
krishna.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Hi, Hari, no your assumption is false. A continue can be placed inside a switch ONLL if the switch is enclosed by a loop.
In fact, to clear your confusion, there is no relationship between the continue and the switch: they're independent of each other. You can have the continue without the switch and vice-versa. Remember, this continue is ONLY associated with the outer loop.
If you do see a question on the exam, make sure that you identify the continue with the enclosing loop rather than the switch.
HTH,
Sashi
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
--------------------------------------------------------------------------
A continue can be placed inside a switch ONLY if the switch is enclosed by a loop.
-------------------------------------------------------------------------

---------------------------------------------------------------
C)keyword continue can never occur in a switch statement
---------------------------------------------------------------

In the following program, switch statement is not enclosed by a loop.
continue statement is placed in a switch block.


class Test
{
public static void main(String args[])
{
switch(1)
{
case 1: for(int i=1;i<=5;i++)
if(i==2) continue;else System.out.println(i);

{System.out.println("end of switch");}

}//switch
}//main
}//Test

output:
1
3
4
5

end of switch
--------------------------------------

1. which of the following are true for a switch statement?

A]all switch statements must have a default label
B]there must be atleast one label for each code segment
C]keyword continue can never occur
D]no case label may follow a default label
E]char literal can be used
------------------------------------------
E- is true.
(B) Statement in a switch body must have a case label, or it is unreaclable.Hence B is true.

class Test
{
public static void main(String args[])
{
switch(1)
{
case 1:{ for(int i=1;i<=5;i++)
if(i==2) continue;else System.out.println(i);}break;

{System.out.println("end of switch");}

}//switch
}//main
}//Test

Result:

Test.java:10: unreachable statement
{System.out.println("end of switch");}
^


Regards
Naresh
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Kumar, you're right. Point granted. It's more a question semantics.
Let me re-word my prior stmt:
A continue cannot belong to a switch stmt.
I guess that'll make everybody happy, including the compiler.
Quote from the KB book "continue statements must be inside a loop; otherwise, you'll get a compiler error. break statements must be used inside either a loop or a switch statememt".
Sashi
[ December 12, 2005: Message edited by: Sasikanth Malladi ]
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Yes, Continue statement must be inside a loop. continue statement cannot belong to switch statement.

but option (C) is "keyword continue can never occur"
in the given example 'continue' statement belongs to for loop. But it is placed in a switch block. So 'continue' statement can occur in a switch block along with a loop.


Regards
Naresh
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I agree that the wording is pretty vague. They might have intended to make sure that a person knows that break and not continue is used inside a switch. But the way they have stated it, you could have a continue inside a switch if the switch is in a loop, or there is loop in a code block in the switch.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Ah, yes, that puts it in a different light.
You're right. continue can occur in a switch stmt.
Sashi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: switch doubt..