• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question on switch

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice explanation gaurav !
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was an incorrect statement by me kesava, ignore it and thanks for correcting me.
 
No holds barred. And no bars holed. Except this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic