• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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
 
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic