• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on switch

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Arora
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice explanation gaurav !
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was an incorrect statement by me kesava, ignore it and thanks for correcting me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic