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

Default value in switch construct

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
I am using the rules round up game of this site and there i find a question that
"In a switch construct, the default value will execute if no values match the switch() construct.
I make it false b'coz i think that a default value may also execute if there is no break statement with the previous one."
But the forum says it true, "if there are no matching construct found the default will run."
please help,
Nisheeth
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int x=10;
switch(x){
case 1: System.out.println("one");
case 2: System.out.println("two");
case 3: System.out.println("three");
default: System.out.println("default");
}
the above code will work properly without break .
or if you will give the break in each case.
there is no effect on th ecode.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code Mr Iftikhar gave will seem to work the same with or without breaks in each case if x is not 1, 2, or 3! However, the program works differently if x is 1,2,or 3, depending on whether or not you have a break in each case. Just want to make that point clear.
[ February 09, 2002: Message edited by: Junilu Lacar ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"In a switch construct, the default value will execute if no values match the switch() construct.
I make it false b'coz i think that a default value may also execute if there is no break statement with the previous one."
But the forum says it true, "if there are no matching construct found the default will run."


There is nothing false about the statement just because default may execute under other conditions. Watch out for convoluted thinking like this on the real test.
Also don't assume that default will be the last case in a switch statement - there is no requirement for that, it is just common practice.
Bill
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic