• 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

Switch with Boolean ???

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am trying to write a switch statement which will check the return statements from 3 called up methods ("february", "century", and "leap")...and based on the boolean values they return to "daysInMonth" certain commands are to be carried out......

is ist possible to use the code segment below the way i have done?
if not, what corrections could i make,....any suggestions?


 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"switch" works only with integral types. boolean values have their own branching control structure: the "if" statement.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm,
I'm a bit confused here:

because i was made to understand that if you had several "if statements" following each other you could put them all together into a switch control structure?

so if i have 20 possibilities i would have to put these all into "if statements"?

tahnx!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Booleans have just two values: true and false. Therefore a "boolean switch" could never have more than two branches. To have more than two branches, you'd actually need more than one boolean value -- i.e., you're testing more than one thing. A chain of if... else if... else statements lets you do just that.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay,
this is what I've decided to do.....how about it?



[ May 28, 2006: Message edited by: Wolfgang Obi ]
[ May 28, 2006: Message edited by: Wolfgang Obi ]
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wolf-
Just a little switch tip.
If you're going to take the same action for several cases, it is not necessary to explicitly code the action for each case. Just arrange your cases appropriatley.
The following will have the same affect as the switch you coded above.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by S Root:
Wolf-
Just a little switch tip.
If you're going to take the same action for several cases, it is not necessary to explicitly code the action for each case. Just arrange your cases appropriatley.
The following will have the same affect as the switch you coded above.



why don't we have a break after the If condition below
if (((isSchalt == true)&&(!(isCent)))||(isLeapCent == true)) days = dayGroup[1];


will it not become a fall through case ???
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is
.

It is good programming style to always write the curly braces, {}, althought they are not needed if the clause contains only a single statement. The curly braces are a general indicator in Java of a compound statement. This is known as a block of code. (See here)
[ May 29, 2006: Message edited by: wise owen ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quicker way to handle FebruaryCR
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would do this without a switch statement at all, you can do the whole thing in two lines of code:
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
I would do this without a switch statement at all, you can do the whole thing in two lines of code:



Well, except for the whole leap year thing...
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds like a job for java.util.Calendar. I whipped up some code, but I'm puzzled by its output:

Output:

2006-1 = 31
2006-2 = 31
2006-3 = 31
2006-4 = 30
2006-5 = 31
2006-6 = 30
2006-7 = 31
2006-8 = 31
2006-9 = 30
2006-10 = 31
2006-11 = 30
2006-12 = 31

What's up with February?
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic