• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Switch Case Statement

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

How can i add " You entered invalid score" to my program if user enters a score more than 100 and less than 0 ?

This is the program.Thank you.

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
than why you used default case?
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:than why you used default case?



I couldn't figure out what to do and that's why i am asking it here. I did it in if statement first and then my instructor wanted me to do the same thing in switch case.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like this:

 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:like this:




But then it finds an error with
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can throw an exception as well in case wrong score..
 
Serkan Kilic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:You can throw an exception as well in case wrong score..



i am not allowed to do that. We haven't seen it in the chapters.

Thank you anyways.
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serkan Kilic,

Try this

 
Marshal
Posts: 8404
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it good here? I don't think so.


 
Marshal
Posts: 76419
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:. . .
Try this

No, don't try that. There is something very wrong about that bit of code. Actually, two things very wrong with it. Apart from the poor indentation.

[edit]Maybe actually three things wrong with it[/edit]
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the score have to be validated in the switch statement?
I would validate the input as soon as it is available before proceeding to the switch.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie,

May I know where did I go wrong there?
 
Liutauras Vilda
Marshal
Posts: 8404
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:if (score > 0 || score <= 10)

Regardless the truth value of this boolean expression it evaluates to true always. It is called tautology (I have been taught about it by Campbell Ritchie).
So based on that, -100 would earn you a "F" grade.

 
Liutauras Vilda
Marshal
Posts: 8404
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:score <= 10

Now look at this part on its own. You do have cases 10, 9, 8, 7, 6. Why you'd need to check if it is less or equal 10 in a first place?

And one more thing. Score suppose to be validated before you pass it to switch statement (this is what Pawel mentioned).
 
Campbell Ritchie
Marshal
Posts: 76419
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another problem; please tell us what the likely values of score are at that point.
 
Liutauras Vilda
Marshal
Posts: 8404
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Earlier I wrote:Regardless the truth value of this boolean expression it evaluates to true always.

I wasn't accurate enough.

Here is the actual definition:
"A compound statement, that is always true regardless of the truth value of the individual statements, is defined to be a tautology."
 
Saloon Keeper
Posts: 14277
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paweł. You should write a separate method that translates a numeric score to an alphabetic one. Validate the input at the start of the method. Normally you would throw an exception for illegal arguments, but if you're not allowed, you can return null. Use the switch only for the translation step, not the validation.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras Vilda,

I get it now. OR executes if either value is true. If the value is negative, the first condition fails but the second condition becomes true. So it will always be true. Thank you so much.

Campbell Ritchie wrote:There is another problem; please tell us what the likely values of score are at that point.



Negative infinity to 5 and 11 to positive infinity will take you to default case. Am I right?


I thought of validating outside but since Serkan Kilic and Tushar Goel wer playing within the default case, I thought they wanted it there.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serkan Kilic wrote:But then it finds an error with


And why do you think that is? A simple fix might be:But only YOU can decide if that's sufficient.

My suggestion: Have a look at this page, and think about writing some methods that you can re-use; because - believe me - this is NOT the last time you're going to run into this problem.

HIH

Winston

[Edit] Ooops. I propagated an error. That first line should read:
  if (score < 0 || score > 100)
Corrected.
 
Rancher
Posts: 4801
50
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I agree with Paweł. You should write a separate method that translates a numeric score to an alphabetic one. Validate the input at the start of the method. Normally you would throw an exception for illegal arguments, but if you're not allowed, you can return null. Use the switch only for the translation step, not the validation.



Except apparently:
"
I did it in if statement first and then my instructor wanted me to do the same thing in switch case.
"

It seems to be a requirement to try and do this in the switch.
 
Stephan van Hulst
Saloon Keeper
Posts: 14277
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Missed that.

I have no idea what this is supposed to teach.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I have no idea what this is supposed to teach.


Maybe just switch statement mechanics, and what you can and can't get away with...

And I have to admit that I rather like Serkan's. It's neat, compact, and pretty readable.

Moreover, his actual problems have nothing to do with the swtich statement itself.

Winston
 
Campbell Ritchie
Marshal
Posts: 76419
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:. . .

Campbell Ritchie wrote:There is another problem; please tell us what the likely values of score are at that point.



Negative infinity to 5 and 11 to positive infinity will take you to default case. Am I right?
. . .

Probably not. That did not answer my question and you have probably got those values wrong.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Probably not. That did not answer my question and you have probably got those values wrong.


Maybe 0 to 5 and 11 to positive infinity? No, integer takes negative values right? I am confused.
 
Campbell Ritchie
Marshal
Posts: 76419
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked you what the value of score is when you reach the default part of the switch. What will the value of score on your line 13 be, if you enter the following values:-
0 100 35 65 99999999 -9999999999?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I asked you what the value of score is when you reach the default part of the switch. What will the value of score on your line 13 be, if you enter the following values:-
0 100 35 65 99999999 -9999999999?



For 0, its 0
For 100, its 10
For 35, its 3
For 65, its 6
For 99999999, its 9999999
For -9999999999, its -999999999
 
Campbell Ritchie
Marshal
Posts: 76419
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:. . .
For 0, its 0

Correct

For 100, its 10

Incorrect

For 35, its 3

Incorrect

For 65, its 6

Incorrect

For 99999999, its 9999999

Incorrect

For -9999999999, its -999999999

Incorrect
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie,

Sorry these will be the values of score/10

The values of score will be

0 100 35 65 99999999 -9999999999
 
Campbell Ritchie
Marshal
Posts: 76419
365
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:. . .
Sorry these will be the values of score/10
. . .

Yes, you have got it now
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe actually three things wrong with it.



1 is score value

2 is validation inside switch

3 is ?
 
Campbell Ritchie
Marshal
Posts: 76419
365
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YOu have already been told. The if condition was a tautology.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Switch statements are very fast and become a simple jump statement on an assembly level. They are faster than an if or series of if else statement - probably 3 times as fast. It doesn't matter if you have 3 or 300 case statements, the program will jump to the proper case. So a switch statement with 3 statements will be no faster that a switch statement with 300 cases, and a switch statement with 300 cases will be 3 times as fast as one if/else statement. The switch becomes a simple jump statement. In your program the fastest way is to put a case from 10 to 0 and a default case for an illegal entry. The compiler will count all your lines of code, push the value you are switching and make it an offset for a jump statement.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Lau wrote:Switch statements are very fast and become a simple jump statement on an assembly level.


That might be true for C, but Java doesn't use assembler.

They are faster than an if or series of if else statement - probably 3 times as fast...


Really? Can you give us a link to the JLS or JVM spec that confirms that?

What you say may be true, but even if it is, it's not a good reason to turn a straightforward piece of logic into an arcane value simply to use a switch statement.

Winston
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"That might be true for C, but Java doesn't use assembler. "



Well it is true for Java as well and that is the reason you MUST use a constant as a case and not a variable. The compiler/assembler turns the constant into an offset and that would be impossible to do with a variable. Again mysticism and programming strike me as somewhat weird.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Lau wrote:Well it is true for Java as well and that is the reason you MUST use a constant as a case and not a variable. The compiler/assembler turns the constant into an offset and that would be impossible to do with a variable. Again mysticism and programming strike me as somewhat weird.


Where's the mysticism? All I asked was for you to support your assertion with a link to the JLS or JVM spec - which you have failed to do. So until then, I hope you don't mind if I take your speculation with a pinch of salt.

[Edit] If anyone's interested, there's some information about it here, and it suggests that there is more than one implementation or switch, depending on whether the "cases of the switch can be efficiently represented as indices into a table of target offsets" (but it doesn't elaborate too much on when that might be the case).

Winston
 
Beware the other head of science - it bites! Nibble on this message:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic