If and Switch same task, different results?

Ruchir Baronia

I have a switch and an if statement, and they are equivalent. They should both take a string, if yes do something, and if no do another thing. The if statement does nothing regardless of what is entered, but the switch statement does. Why is that?

Here is the if statement:

if (yes.equals("yes")){
    System.out.println ("Enter your first number");
    fnum = x.nextDouble();
    System.out.println ("Enter your second number");
    snum = x.nextDouble();
    calculations(fnum, snum);
}
if (yes.equals("No")) {
    System.out.println("okay, bye then!");
}

Here is the switch:

switch (yesno){
    case "yes":  
        System.out.println ("Enter your first number");
        fnum = x.nextDouble();
        System.out.println ("Enter your second number");
        snum = x.nextDouble();
        calculations(fnum, snum);
        break;
    case "no":
        System.out.println("k bye");

This is not a duplicate, because the issue is in the if statement. I have been marked duplicate for my switch.

Stephen C

I have a switch and an if statement, and they are equivalent.

Actually, they are NOT equivalent:

  • The if version is checking for "yes" or "No". But the switch version of checking for "yes" or "no". Since the checks are case sensitive in both cases, you will get different results if the input is "no" ... or "No".

  • The two versions check different variables; i.e. yes and yesno. This could make a difference, depending on the context.

Either of these could explain the different behaviour you are seeing.


I thought that yes and yesno would be equal, but they weren't...? Any idea why?

Well, clearly the names are different so they cannot be the same variables.

However, the fact that they are different variables does not necessarily mean that they have different values. It is the values that determine the code's behaviour.

Certainly, if yesno contained a proper "yes" or "no" value and yes contained something else (such as ""), then that would give the behaviour you are observing. (Obviously this is a hypothetical diagnosis. If you want a more concrete answer, show us the relevant code.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

The same XPaths - different results

From Dev

Different results for same numbers

From Dev

The same XPaths - different results

From Java

Different results for same mathematical operations

From Dev

Same code returns different results

From Dev

Same simple calculation, different results

From Dev

Laravel "same" code different results

From Dev

Same Linux command with different results

From Dev

same python script, but different results

From Dev

Different results using the same maths in different browsers

From Dev

Different results with same inputs but different ways of inputting

From Dev

Different results with LocalDateTime by different calls with same parameter

From Dev

same switch case is used but the definition is different

From Dev

same switch case is used but the definition is different

From Dev

Same Python code, same data, different results on different machines

From Dev

Same code executed on different servers (same version) yields different results

From Dev

Same Python code, same data, different results on different machines

From Dev

Same Queries on Same database at same time but Different Results?? [Big Query]

From Java

Same regex have different results in Java and JavaScript

From Dev

characterIndexForPoint returning different results for same parameters

From Dev

QCryptographicsHash gives different results for same string

From Dev

The same query on Sparql gives different results

From Dev

Is it HATEOAS compliant? GET same address with different results

From Dev

Comparing same character gives different results

From Dev

BrowserStack, Sauce Labs - same test, different results

From Dev

Same input into [math]::round returns different results

From Dev

App Engine: different results for same objectify query

From Dev

Different results between clang and gcc with same code

From Dev

Getting different results for the same equation in a function and the shell

Related Related

  1. 1

    The same XPaths - different results

  2. 2

    Different results for same numbers

  3. 3

    The same XPaths - different results

  4. 4

    Different results for same mathematical operations

  5. 5

    Same code returns different results

  6. 6

    Same simple calculation, different results

  7. 7

    Laravel "same" code different results

  8. 8

    Same Linux command with different results

  9. 9

    same python script, but different results

  10. 10

    Different results using the same maths in different browsers

  11. 11

    Different results with same inputs but different ways of inputting

  12. 12

    Different results with LocalDateTime by different calls with same parameter

  13. 13

    same switch case is used but the definition is different

  14. 14

    same switch case is used but the definition is different

  15. 15

    Same Python code, same data, different results on different machines

  16. 16

    Same code executed on different servers (same version) yields different results

  17. 17

    Same Python code, same data, different results on different machines

  18. 18

    Same Queries on Same database at same time but Different Results?? [Big Query]

  19. 19

    Same regex have different results in Java and JavaScript

  20. 20

    characterIndexForPoint returning different results for same parameters

  21. 21

    QCryptographicsHash gives different results for same string

  22. 22

    The same query on Sparql gives different results

  23. 23

    Is it HATEOAS compliant? GET same address with different results

  24. 24

    Comparing same character gives different results

  25. 25

    BrowserStack, Sauce Labs - same test, different results

  26. 26

    Same input into [math]::round returns different results

  27. 27

    App Engine: different results for same objectify query

  28. 28

    Different results between clang and gcc with same code

  29. 29

    Getting different results for the same equation in a function and the shell

HotTag

Archive