Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch  RSS feed

 
priya vinay
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Khalid A. Mughal:
The first statement in the switch body must have a "case" label, or it is unreachable. This statement will never be executed since control can never be transferred to it. The compiler will flag this as an error.
But I think The first statement in the switch body must have a "case or default" label, or it is unreachable.....
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely right. But so is the author. The "default" case is still a case, you just don't use the word "case" before it.
Hope that helps!
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by priya varma:
From Khalid A. Mughal:
The first statement in the switch body must have a "case" label, or it is unreachable. This statement will never be executed since control can never be transferred to it. The compiler will flag this as an error.
But I think The first statement in the switch body must have a "case or default" label, or it is unreachable.....

You're right. The only other option is no statement at all inside the switch construct.
JLS:

The body of a switch statement is known as a switch block. Any statement
immediately contained by the switch block may be labeled with one or more case or default labels.
These labels are said to be associated with the switch statement, as are the values of the constant expressions (�15.28) in the case labels.
 
priya vinay
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Bosch:
The "default" case is still a case, you just don't use the word "case" before it.
Hope that helps!


Thanks for the quick response.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
default is a label rather than a case. Both are labels.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case somebody wants a boring switch to be more entertaining, try to predict what happens in this piece of code (and if it compiles at all!):
 
priya vinay
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles and the output will be
Sunshine in the summer
10
My case label at work
I guess mycase: is a general java label and noway related to Switch.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're correct. mycase is not related directly to the switch, but it does belong with the first valid case construct.
The code

doesn't compile because mycase is unreachable due to break.
 
pallavi pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more qs on control flow statements.
Which statements are true?
Select the three correct answers.
a.The conditional expression in an if statement can have method calls.
b.If a and b are of type boolean, the expression (a = b) can be the conditional expression of an if statement.
c.An if statement can have either an if clause or an else clause.
d.The statement if (false) ; else ; is illegal.
e.Only expressions which evaluate to a boolean value can be used as the condition in an if statement.
Given ans: a,b,e

My ans: b,c,e and i think a is not too clear. It should be -
The conditional expression in an if statement can have method calls with a return type boolean.
and why c is not correct?
Thanks
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pallavi
(a) is correct because all it says is that it 'can' have method calls. It doesn't say, "The conditional expression in an if statement can have any method call".
Can you give an example to prove that (c) is correct?
Cheers
Harwinder
[ November 14, 2003: Message edited by: Harwinder Bhatia ]
 
pallavi pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Harwinder.
class ifClass{
public static void main(String ags[]){
int a= 10;
if(a>0)
if(a%2==0)
System.out.println(a + " is a positive even number");
}}
class ifClass{
public static void main(String ags[]){
int a= 10;
if(a>0)
System.out.println(a + " is a postive number");
else
System.out.println(a + " is a negative number");
}}

--An if statement can have either an if clause or an else clause.
A'm I wrong in understanding the above statement??
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pallavi pandey:

if(a>0)
if(a%2==0)
System.out.println(a + " is a positive even number");
-------
if(a>0)
System.out.println(a + " is a postive number");
else
System.out.println(a + " is a negative number");

--An if statement can have either an if clause or an else clause.

Pallavi
OIC the reason for your confusion.
The statement An if statement can have either an if clause or an else clause does not refer to nested if statements.
If we break the statement down to 2 statements, it means:
1. You can have an if statement without a corresponding else clause - this is correct
2. You can have an else statement without a corresponding if clause - this is incorrect
Your first example is an example of an if statement nested inside another if statement. Both the if statements have no corresponding else clause. This is OK.
Your second example has an if clause and its corresponding else clause. The else clause is not nested inside the if.
Check this example out:

The else in the above example corresponds to the closest if.
Hope this helps.
----------------------
Cheers
Harwinder
[ November 14, 2003: Message edited by: Harwinder Bhatia ]
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something for the moderators to look into:
I cannot edit my previous post completely. When I click on the edit icon, it only displays the second half of my post starting from the [CODE] down to the end of the post (but not the first half). Looks like a bug to me ...
Thanks
Harwinder
 
pallavi pandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't pay much attenstion to the word 'clause' and i had my own assumptions.
Thanks for your explanation.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Harwinder Bhatia:
Something for the moderators to look into:
I cannot edit my previous post completely. When I click on the edit icon, it only displays the second half of my post starting from the [CODE] down to the end of the post (but not the first half). Looks like a bug to me ...
Thanks
Harwinder

Same problem here. It happens pretty often.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!