• Log in or sign up for an account here: https://www.sythe.org/login

Switch Blocks

iJava

.Previously known as RSGoldRush

$200 USD Donor New
Joined
Nov 21, 2011
Sythe Gold
491
Messages
1,204
Switch statements can be handy to use instead of using several if-then-else statements to do selected actions depending on a value.

So basically we begin by having an integer :

Code:
int val = 4;

Now we create the switch statement with the param "val" :

Code:
int val = 4;

switch(val) {

}

Now we need to add the cases, the cases work in the way that if val = the case number then whatever is in that case happens :

Code:
int val = 4;
        switch (val) {
            case 0:
                System.out.println("Case 0");
                break;
            case 1:
                System.out.println("Case 1");
                break;
            case 2:
                System.out.println("Case 2");
                break;
            case 3:
                System.out.println("Case 3");
                break;
            case 4:
                System.out.println("Case 4");
                break;
            default:
                System.out.println("Default");
                break;
        }

Basically the code above will print "Case 4" because val = 4 so case 4 will be called. You could do rand.nextInt(); for an antiban or something like that to randomize different actions each time. You could also use it for different items if you needed to do different actions by just sending a param to the void. The default case there will run if the case does not exist that val equals. So if I removed case 4 : like in the code below the case default would be run so "Default" would be printed.

Code:
int val = 4;
        switch (val) {
            case 0:
                System.out.println("Case 0");
                break;
            case 1:
                System.out.println("Case 1");
                break;
            case 2:
                System.out.println("Case 2");
                break;
            case 3:
                System.out.println("Case 3");
                break;
            default:
                System.out.println("Default");
                break;
        }
 
I would also like to stress the importance of the "break;" statement at the end.
If this statement were not there it will iterate through the case's and will be a huge mess.
For instance:
Code:
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter a number: ");
		switch(scan.nextInt()) {
			case 0:
				System.out.println("Case 0");
				break;
			case 1:
				System.out.println("Case 1");
			case 2:
				System.out.println("Case 2");
			case 3:
				System.out.println("Case 3");
			default:
				System.out.println("Default");
		}

If I were to enter a 1, the output would be:
Code:
Enter a number: 1
Test 1
Test 2
Test 3
Default

but, If I were to enter a 0, the out would be:
Code:
Enter a number: 0
Test 0

So, remember to add in those break statements.

iJava, If I offend you by adding on to this tutorial, send me a pm and it will be removed.
 
I would also like to stress the importance of the "break;" statement at the end.
If this statement were not there it will iterate through the case's and will be a huge mess.
For instance:
Code:
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter a number: ");
		switch(scan.nextInt()) {
			case 0:
				System.out.println("Case 0");
				break;
			case 1:
				System.out.println("Case 1");
			case 2:
				System.out.println("Case 2");
			case 3:
				System.out.println("Case 3");
			default:
				System.out.println("Default");
		}

If I were to enter a 1, the output would be:
Code:
Enter a number: 1
Test 1
Test 2
Test 3
Default

but, If I were to enter a 0, the out would be:
Code:
Enter a number: 0
Test 0

So, remember to add in those break statements.

iJava, If I offend you by adding on to this tutorial, send me a pm and it will be removed.


You're algood mate. I guess I forgot to mention more on breaks.
 
Runescape Gamblingrunescape gambling
Back
Top