Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

in switch case some confusion

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
program that i made very basic but there is difference in output for char and int expression.
Please look at following code

class Switchtestint
{
public static void main(String args[])
{
int a=7;
switch(a)
{
case 1: System.out.println("1");
default: System.out.println("default");
case 2: System.out.println("2");
case 3: System.out.println("3");
}
}
}

output:
default
2
3


class Switchtest
{
public static void main(String args[])
{
char cha='z';
switch(cha)
{
case 'a': System.out.println("a");
default: System.out.println("default");
case 'b': System.out.println("b");
case 'z': System.out.println("z");
}
}
}


output:
z



Why is it so??

thanks
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case a,Since the value 7 is not avaliable in case statement it prints the value in default and since there is no break avaliable in default it prints both case 2 and 3.

But in the case of example b, The output is straight forward since z is avaliable in a case 'z': it just prints z.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Output is right.

in first sceanario
int a=7;
switch(a)
{
case 1: System.out.println("1");
default: System.out.println("default");
case 2: System.out.println("2");
case 3: System.out.println("3");
}
}

since their id no match for the a(7) hence it will go in the default case,after which break statement is not their hence it will fall throw all case.

in first sceanario
char cha='z';
switch(cha)
{
case 'a': System.out.println("a");
default: System.out.println("default");
case 'b': System.out.println("b");
case 'z': System.out.println("z");
}
}

since their is exact match for cha('z') hence it will not go in the default case.it will go in the case 'z',and since it is the last case output is z

hope you will get.need further clarification the ask
 
pawni jain
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Balajee n Tarunjava
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic