Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

switch & case + for loop

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
In Flow Control and Exception Handling section, in Khalid's book, I have an excercise question:
Which of these combinations of switch expression types and case label value types are legal within a switch statement?
Select ALL valid answers:
(a) switch expression of type int and case label value of type char.
(b) switch expression of type float and case label value of type int.
(c) switch expression of type byte and case label value of type float.
(d) switch expression of type char and case label value of type byte.
(e) switch expression of type boolean and case label value of type boolean.
My answer is: (a) and (d) are correct.
But, the book answers say only (a) is correct. Explaination at the back for choice (d) as incorrect is :
" The type of the case labels must be assignable to the type of the switch
expression".

To back my answer for choice (d), I have a sample code which compiles and even runs alright.
**************************
class test174
{
public static void main(String args[])
{
char month=65;
final byte b1=65;
final byte b2=97;
switch(month)
{
case b1:
System.out.println("65");
break;
case b2:
System.out.println("97");
break;
}
}
}
****************************
Can somebody explain why I am wrong???

Also, the following line is legal:
for(int i=0, j=5, k=56;i<5; i++) // multiple declarations of int type
{
do something;
}
but, why is the line below illegal:
for(int i=0, j=5, byte k=56;i<5; i++) //multiple declarations of multiple datatypes
{
do something;
}
Can I not declare variables of more than one primitive datatypes in the initialization section of a for loop?

Thanks in advance!!
Bye,
Tualha Khan.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, there must have been typographical error, though you should check errata to see for your self.
switch(char) and case byte is legal. Though switch(char) and case: short is not, probably thats what the writer intended.
However you second question refers to negate a simple principle of declaration and initialization and that is you DO NOT mix declaration and initialization of types with in a single statement. Also and therefore(int i = 0, int k = 0....) is illegal and so is for(int i= 0; double d = 0.9..........)...
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actuall Amond, switch(char) case short will work because a switch only works on ints. So in switch (xxx), xxx needs to be an int or something that can be converted to an int (byte, short, char).
Then the case statements need to be a value that can be determined at compile time and then must also be int's or able to be implicitily casted to ints. So in the question a and d are correct, but a short would work also.
So to sum it up, the switch can be byte, short, char, or int, but it will be converted to an int. And the case has to be a value determined at compile time, and it must also be of type int, so byte, short, char or int.
Bill
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic