• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

switch doubt..

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, yes, that puts it in a different light.
You're right. continue can occur in a switch stmt.
Sashi
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic