• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot understand the output...

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried running the program , and I got the output:
C
E
F
Can any one explain me how do we get this output.I am a bit confused..
Sonir
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one is tricky.
The first time through the loop, i is zero. When the switch variable is evaluated, it is still zero (immediately after being evaluated, it gets incremented to 1 by the pre-increment operator).
The first case statement is NOT comparing the numeric value zero, but the character zero. This is a big difference. The evaluated value of i is numeric 0, and does not match any of the case statements, therefore no code gets executed in the first pass.
The second loop iteration starts, and i gets incremented...but remember it got bumped up by one in the last switch statement, so the second loop iteration, i has the value 2. Now the switch statement is executed for the second time. The value is evaluated as 2, and then i gets incremented to 3. The case where i=2 gets executed, and the string "C" is printed. Then execution breaks out of the switch statement and the next loop iteration begins.
The third time through the loop, i is evaluated as 4, then incremented by one, and the 4 case is executed, printing "E". Since there is no break after "E" is printed, the next statement also executes, printing F.
The loop test now fails, because i is equal to 5, so the loop exits.
IF the first case had been zero "0" instead of the character 0 ('0') nothing would have printed out, because the entire loop would have been broken out of by the labeled break in the first case statement. I missed those single quotes myself the first time I looked at this!
Rob
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to differentiate between the char '0' and char 0.

char c='0';
char i=0;
System.out.println(Integer.toHexString(c));//30
System.out.println(Integer.toHexString(i));//0
if (c!=i)
System.out.println(c+" is not equal to "+i);
// 0 is not equal to 0
why is that here in a regular System.out.println statement the output doesn't differentiate between two different values (0x30 and 0x00)?
[ January 22, 2002: Message edited by: Jennifer Wallace ]
[ January 22, 2002: Message edited by: Jennifer Wallace ]
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF the first case had been zero "0" instead of the character 0 ('0') nothing would have printed out, because the entire loop would have been broken out of by the labeled break in the first case statement. I missed those single quotes myself the first time I looked at this!

I don't think so.
If the first case is number "0",It will print out:A B.
Because first the value of i is "0",the first case will be executed,and prints out "A",then i incremented by one,and the second case is meeted,prints out "B",then the entire loop would have been broken out.
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
say case 1 was selected then what would have happened. I am asking beceause there is a label in Break there.
what would happen next if case 1 were to be selected ?
 
eric sun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice here, if the first case number is "0" , there will be error with imcompatible types. Here need byte, short, char, int type.
1:LOOP: for (i=0;i<5;i++) {
2: switch(i++) {
3: case '0': System.out.println("A");
4: case 1: System.out.println("B"); break LOOP;
5: case 2: System.out.println("C"); break;
6: case 3: System.out.println("D"); break;
7: case 4: System.out.println("E");
8: case 'E' : System.out.println("F");
9:}
10:}
Now, let explain why the answer is C E F step by step.
Loop 1 :
1: i=0
2: swith(i=0) but (0)i++ = 1 => i
3-9: no case equal i ( 0 )
10: act the i++ in for() (1)i++=2 => i
Loop 2 :
1: i=2
2: swith(i=2) but (2)i++ = 3 => i
5: case 2: System.out.println("C"); break;
10: act the i++ in for() (3)i++=4 => i
Loop 3 :
1: i=4
2: swith(i=4) but (4)i++ = 5 => i
7: case 4: System.out.println("E");
8: case 'E' : System.out.println("F");
10: act the i++ in for() (4)i++=5 => i
end
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Seany Iris:

I don't think so.
If the first case is number "0",It will print out:A B.
Because first the value of i is "0",the first case will be executed,and prints out "A",then i incremented by one,and the second case is meeted,prints out "B",then the entire loop would have been broken out.

Yup. you're right!
It will print A B before it breaks out.
Also, Eric, what did you mean by:
Notice here, if the first case number is "0" , there will be error with imcompatible types. Here need byte, short, char, int type.

The case label '0' is a char literal, and it's a perfectly valid case argument. You continue with your explaination ok and never mention this again. If you try compiling this code you'll see it works fine.
Rob
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic