Switch+Statements


 * __A switch statement__** is a sequence of if/else/else that compares a //single interger value// against several //constant// alternatives that can be implemented.


 * It is used when you need to make multiple "if" statements.
 * It's easier to read, and faster to create a switch statement, comparing back to "if" statements.
 * It is only used if comparing to constants.

{ case : _ break; case : _ break; default: _ }**
 * switch(variableName)

the **case** is to compare to specific values several times. after the : you put whatever you want to do with it. insert a **break;** after each case to end and get out of a particular case. otherwise it will just go through all of them and just do the last case.
 * To catch anything else to keep from producing error use "default: " case. It is the catch-all for anything that has not come yet and as such should be placed at the end of the switch statement. Even if everything that is possible within the switch statement has been covered, a default should still be there. Remember, always account for stupid people.

ex. switch(num) { case 4: System.out.print("NBC"); break; case 5: System.out.print("FOX"); break; default: System.out.print("Unknown"); break; }

Disadvantages of using Java Switch statements:
 * **No ranges**. It doesn't allow ranges, i.e. Case 90-100. Many other languages do.
 * **Integers only**. It requires integers and doesn't allow useful types like String. Many other languages do.
 * **Error-prone**. It is prone to errors and a common source of bugs - forgetting a break or default, causing logic errors. Some languages have eliminated these dangerous situations.

When using a Java Switch statement, you are limited from utilizing ranges in the statement. Ranges, however, can be used indirectly. My first method for this is not using the break command. As you know, without a break in the code the program will simply go down the state preforming each desired task. We can use this to our advantage by placing all of the cases that would have the same command issued after them sequentially. However, we wouldn't use breaks until after the last case which would be the only item in that range with the desired command.
 * Fancy Switch Tricks**

ex. switch(num) { case 1: System.out.print("Gotta' "); break; case 2: case 3: case 4: case 5: case 6: System.out.print("Catch Em' "); break; default: System.out.print("All!"); break; }

Another fancy trick to immitate the advantages of a range is to utilize the default case more heavily. A range allows you to cover a vast range of values. Typically the default is used as a failsafe for a vast range of invalid values, however, it can be used to cover a vast range of valid ones too. However, this could make your program perform the wrong statement for invalid values. So be careful and make sure that invalid values are stopped before the switch statement is executed.

ex. switch(num) { case 1: System.out.print("don't"); break; default: System.out.print("do drugs"); break; }

__How to write a switch statement:__

code int digit; ... switch (digit) {   case 1: System.out.print("one"); break;    //This line of code checks if the int digit case 2: System.out.print("two"); break;  //equals to the case number. If it does, case 3: System.out.print("three"); break;  //it prints out the string that the line has. case 4: System.out.print("four"); break; case 5: System.out.print("five"); break; default: System.out.print("error"); break; //If digit does not equal any of the numbers }                                                    // above, it prints out error.

//The code above is the shortcut for the code below.

int digit; ...

if(digit == 1) System.out.print("one");   // If digit equals to 1, it prints out one. else if (digit == 2) System.out.print("two"); else if (digit == 3) System.out.print("three"); else if (digit == 4) System.out.print("four"); else if (digit == 5) System.out.print("five"); else System.out.print("error");

code The top code is the **switch statement**, the bottom code are set of codes that do the same job as the switch statement but in a long way.

More example:

code format="java5" public class Switch {    public static void main(int console) {        //Prints out the best video game console

switch(console) {            case 1: System.out.println("Best console is PS3"); break; case 2: System.out.println("Best console is Xbox 360"); break; case 3: System.out.println("Best console is Wii"); break; case 4: System.out.println("Best console is PC"); break; case 5: System.out.println("Best console is Nintendo DS"); break; case 6: System.out.println("Best console is PSP"); break; default: System.out.println("No console is the best"); break; }   } }

code Example:

code format="java" import java.util.Random; import java.awt.*;

class Switch {   public static void main(String[] args) {

Random month = new Random; // This code makes random number to chose a month int time = month.nextInt(12)+1; // The 12 months could be shown because it is random. And the 1 is the default, invalid month.

switch (time) {           case 1:  System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month.");break; }   } }

code