• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't understand this tricky for loop question

 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)");
}
}
}
}
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does one have to remember all the values for the characters to solve this problem?
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic