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 Cannot understand the output... 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 "Cannot understand the output..." Watch "Cannot understand the output..." New topic
Author

Cannot understand the output...

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

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

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
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

Joined: Jan 08, 2002
Posts: 54
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.


help you means help me
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jan 22, 2002
Posts: 5
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


Nice to meet you here. I like Java.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot understand the output...
 
Similar Threads
Help with english to morse code program, I have no idea what I'm doing
do {} while statement illegal start of type
Write a method that can be called, that will initialize the seating plan.
Robot - can't get symbols: !@#$^&*()_+
Can't understand this tricky for loop question