File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Switch char

 
joao ferreira
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What letters will be printed by this program?

//C
//E
//F


I don't understand why
C,E,F
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What were you expecting and why ?
 
meghana chintanippu
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even i have the same doudt how is the case getting matched with i= 2 but not for i=0,1. can you please tell the conversion of int to char in this question
 
Sanju Nair
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first iteration of for loop, value assigned to var i of type char is 'int' value 0. While executing the switch statement, value of i is still int 0(as the operator is port-incremental). Since '0' (char) and 0 (int) are not the same, control exists from the switch block as there is no matching case statement. And since, the switch statement contains the post-incremental operator, value of i is 1 now. At the end of the first iteration of for loop, i is again incremented by 1(update clause of for loop). Which means, in the second iteration value of i is 2(int) - hence printing "C". In third iteration, value of i is 4(int), hence printing "E" and since there is no break statement, it executes the next statement as well, hence printing "F" too.

Hence, you get the output -
C
E
F

Debugging the code will give you insight into how control flows.
 
Roel De Nijs
Sheriff
Posts: 9170
76
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanju Nair wrote:Debugging the code will give you insight into how control flows.

Or you can add a few well-placed SOPs to print the value of the char variable, as shown in the next code snippet.

 
joao ferreira
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot Now I understand
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic