Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

swith and case statements

 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using switch and case statements. It is indicated in Bill Brogden's book, 'Exam Prep' that, "Each case must specify an integer constant followed by a colon."(p141) I understand this; but I tried using a character instead of an integer in a case statement (like case 'A') and it ran fine. Am I to assume that characters are interpreted as constant integers?
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Mughal:
"The type of the integral expression must be char, byte, short or int. The case label values must be assignable to the type of the integral expression."
Hope this helps.
------------------
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Adrian said, it must be of type integral. case 'A' can be converted to an int value, so it is ok. Final variables and expressions like case 1+4 are ok also because the compiler knows those values. Ask yourself will the compiler be able to determine the value of the case statement at compile time and is the value an int or able to be converted to an int (byte, char, short, or int).
 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic