aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can't understand this tricky for loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can Watch "Can New topic
Author

Can't understand this tricky for loop question

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
public class ForSwitch
{
public static void main(String args[])
{
char i;
LOOP: for (i=0;i<5;i++)
{
switch(i++)
{
case '0': System.out.println("A");
case 1: System.out.println("B"); break LOOP;
case 2: System.out.println("C"); break;
case 3: System.out.println("D"); break;
case 4: System.out.println("E");
case 'E' : System.out.println("F");
}
}
}
}
What will it print?
Answer is C and F.
Can anybody explain?
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
I'll take a run at it...
Tricky part is that the case arguments have char values. Lesson here is that the unicode representation of the character zero (which is decimal 48) is not equal to zero.
char c = 0;
( c == '0' ); //is false
Also the increment on c happens after the switch statement is evaluated.
------ Loop iteration #1:
(at switch statement) i=0
no case matches nothing printed
------ Loop iteration #1:
(at switch statement) i=2 ( incremented from switch (i++) and for loop

case 2: matches and prints C then breaks
------ Loop iteration #2:
(at switch statement) i=4 ( incremented from switch (i++) and for loop

case 4: matches and prints E code continues to exec and prints a F (doesn't break out of case unless you code a break statement)
--------
At that point fall out of for loop...
So the result is...
C
E
F
At least that is the way I recon'it.


Tom
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Hi, i get the following result:
C
E
F
The explanation is that the first case is evaluated as '0' and not as 0. As you will see, before the switch the int value of i is 0, therefore none of the case matches; it's important to note that the switch first evaluates the cases matching with the 'i' value before the ++ operator to be used.
After 'E' is printed, as no break has been specified, also the next line is executed, with the result of 'F' to be printed.
Here some modified code to see what happens which will show the following result:
i before switch = 0
i before switch = 2
i = 3. Value to be printed:C(because switch is evaluating me with the previous value 2)
i before switch = 4
i = 5. Value to be printed:E(because switch is evaluating me with the previous value 4)
i = 5. Value to be printed:F(because no break has been specified)
Here is the code:
public class ForSwitch {
public static void main(String[] args) {

char i;
LOOP: for (i=0;i<5;i++)
{
System.out.println("i before switch = " + (int)i);
switch(i++)
{

case '0': System.out.println("i = " + (int)i + ". Value to be printed:A(because switch is evaluating me with the previous value" + (i-1) + ")" );
case 1: System.out.println("i = " + (int)i + ". Value to be printed:B(because no break has been specified)"); break LOOP;
case 2: System.out.println("i = " + (int)i + ". Value to be printed:C(because switch is evaluating me with the previous value " + (i-1) + ")"); break;
case 3: System.out.println("i = " + (int)i + ". Value to be printed (because switch is evaluating me with the previous value " + (i-1) + ")"); break;
case 4: System.out.println("i = " + (int)i + ". Value to be printed:E(because switch is evaluating me with the previous value " + (i-1) + ")");
case 'E' : System.out.println("i = " + (int)i + ". Value to be printed:F(because no break has been specified)");
}
}
}
}


Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
So does one have to remember all the values for the characters to solve this problem?
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Originally posted by Hemal Mehta:
So does one have to remember all the values for the characters to solve this problem?

Why would you think so?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't understand this tricky for loop question