Switches -- What are they? Switches Vs. if statements

Discussion in 'Resources' started by Tehmaker, Oct 10, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Tehmaker

    Switches:
    Show Spoiler

    Every day, people use if statements. If statements are used way too much. Switches are more efficient, and accomplishes the same task.
    Example:
    Code:
    int strawberry = 5;
    switch(strawberry)
    {
        case 1:
        {
                  System.out.println("Int strawberry is equal to 1");
                  break;
          }
          case 2:
          case 3:
          {
                System.out.println("Strawberry == 2 or 3");
                break;
          }
          default:
          {
                System.out.println("This is what happens if the int isn't one of our cases");
                break;
            }
    }
    
    String Example:
    (Litterly stole this from java docs :p )
    NOTE: Only useable in Java 7
    Code:
    switch (month) {
                case 1:  monthString = "January";
                         break;
                case 2:  monthString = "February";
                         break;
                case 3:  monthString = "March";
                         break;
                case 4:  monthString = "April";
                         break;
                case 5:  monthString = "May";
                         break;
                case 6:  monthString = "June";
                         break;
                case 7:  monthString = "July";
                         break;
                case 8:  monthString = "August";
                         break;
                case 9:  monthString = "September";
                         break;
                case 10: monthString = "October";
                         break;
                case 11: monthString = "November";
                         break;
                case 12: monthString = "December";
                         break;
                default: monthString = "Invalid month";
                         break;
            }
    [FONT=Georgia]
    [/FONT]
    Notes:
    • Don't forget to include a default.
    • Switch statements can not be used for strings (unless you are using Java 7)
    • if you do not include a break, the code will continue until it finds one
    • you can put if statements inside switches

    If Statement:
    Show Spoiler

    If statements can be used for anything, but generally they involve more typing than switch statements when dealing with variables. You can use if statements to deal with strings effectively.
    Example:
    Code:
    String steven;
    int yes = 1;
    if(yes == 1)
    {
        steven = "hello";
    }
    



    Switches can be used to directly detect changes to a variable. They are very efficient, and are used like an if statment.
    My second example under switches shows what can be done with switches, to save time. You can check the case of as many numbers as you want:
    Code:
    int num = 123;
    switch(num)
    {
        case 1:
        case 2:
        case 3:
        {
              //Code here   
              break;
        }
        default:
        {
              //code
              break;
        }
    }
    
    Unfortunately, switch statements can not be used for strings until Java7.

    If statements can be used for anything, and can perform the same task that a switch completes, but should be used if you are not dealing with a single variable.
     
    remremrem likes this.
  2. Offline

    Ultimate_n00b

    Maybe explain the differences with words?
     
    Skyost likes this.
  3. Offline

    Tehmaker

    I just wanted to alert "coders" that switches exist, and that they are more efficient than:
    Code:
    if(num == 1 || num == 2 || num == 3)
    {
     
    }
    
     
  4. Offline

    deathknife

    Code:java
    1. int num = 123;
    2. switch(num)
    3. {
    4. case 1:
    5. case 2:
    6. case 3:
    7. {
    8. //Code here
    9. }
    10. }


    Wouldn't any of those get called? As num is 123, and there's no case 123 or default.
     
  5. Offline

    beechboy2000

    I think on Java 7, you can use strings in switch statements.
     
  6. Offline

    Tehmaker


    Well, yes, and no..... Let me fix


    Really? hmm, I'll go read up on it, and update my thread if that is true.

    You're right. Just did a half assed job of adding that in :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. Offline

    deathknife

    I don't see how your string examples uses strings in case. It uses case 1-12, then modifies the variable monthString accordingly to the case.

    Althought, I tried this;
    Code:
    String asd = "aa";
    switch(asd)
    {
    case "aa":
    {
    System.out.println("A");
    break;
    }
    case "bb":
    {
    System.out.println("B");
    break;
    }
    }
    
    My IDE(Eclipse) didn't give any errors, but I haven't tested it if it actually works.
     
  8. Offline

    ZeusAllMighty11

    You can't use a switch statement on a single object like that, deathknife . Instead, you would need to do something like:

    Code:
    switch(asd.length){
        case 1:  // length is 1 
            break;
    }
    

    Java 7 introduced switching on strings, by the way, as mentioned above.






    None of those would be called. 1 2 and 3 are not 123, thus return or 'break', or do the 'default'.
     
    Cirno and deathknife like this.
  9. Offline

    Goblom

    How can i use this with lines on a sign ?

    Code:java
    1. switch (sign.getLines()) { //?
    2. case huh:///?????
     
  10. Offline

    desht

    You can't. switch only works on byte/short/char/int (and their associated wrapper classes), Enum values, and (in Java 7), String.

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html (can't believe no one's linked to that already - that tells you all you need to know about the switch statement).
     
  11. Offline

    xTrollxDudex

    Main statement of this thread: Learn java.

    If people actually learned how to use java, then they wouldn't need to learn something as obscure as this after saying "I'm too lazy to have this many if statements can some1 plz heylp meh?"
     
  12. Offline

    Tirelessly

    Is this not contradictory?
     
Thread Status:
Not open for further replies.

Share This Page