This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on switch" Watch "Question on switch" New topic
Author

Question on switch

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
1. public class Test {
2. public static void main(String Args[]) {
3. int i =1, j = 0;
4. switch(i) {
5. case 2: j +=6;
6. case 4: j +=1;
7. default: j +=2;
8. case 0: j +=4;
9. }
10. System.out.println(�j =� +j);
11. }
12. }
What is the result?
A. 0
B. 2
C. 4
D. 6
E. 9
F. 13

Ans- D

Can someone expalin me why. i=0 , then it should go to case 0 . how come than case 2 is executed. Please help me out.


Thanks<br />Dinesh
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
Case 2 is not executed at all. Here's the flow :

case 2: i != 2 so skip;
case 4: i != 4 so skip;
default: if no other case has been accepted till now, accept this one. so j = j + 2; j = 2;
case 0: i == 0; j = j + 4; j = j + 2; j = 6;

The default case is always executed if no other case before it has been irrespective of whether cases after it will be executed or not.

Hope that explains it.
[ March 05, 2008: Message edited by: Gaurav Arora ]
myneni gopal
Greenhorn

Joined: Jun 16, 2004
Posts: 3
nice explanation gaurav !
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Dinesh, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
Gaurav, your statement The default case is always executed if no other case before it has been irrespective of whether cases after it will be executed or not. According to this statement the following program should produce output as 6 but it is 4 only. Can you explain why in this case it is 4 only.



According to your statement, the flow is as follows.
case 2: i != 2 so skip;
case 4: i != 4 so skip;
default: if no other case has been accepted till now, accept this one. so j = j + 2; j = 2;
case 0: i == 0; j = j + 4; j = j + 2; j = 6;
Vasy Costescu
Greenhorn

Joined: Nov 26, 2007
Posts: 3
Actually when you debug this program you will see that if i = 1 then the default will be executed and everything after it, because there is no break. If after the default statement you add a break then only the default statement will execute.

public class Test {
public static void main(String Args[]) {
int i =1, j = 0;
switch(i) {
case 2: j +=6;
case 4: j +=1;
default: j +=2;break;
case 0: j +=4;
}
System.out.println("j =" +j);
}
}

If i = 0 then the case 0 is executed and nothing else, because there is nothing after it.

So I guess the conclusion is that the default statement is executed only if no match can be found (and it doesn't matter if the match is after default). The position of the default is important only if it doesn't end with a break.
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
That was an incorrect statement by me kesava, ignore it and thanks for correcting me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on switch
 
Similar Threads
u_int8_t in java
Switch statement
switch
a swith/case question
a switch problem