Switch Blocks

Discussion in 'Programming General' started by iJava, May 22, 2012.

Switch Blocks
  1. Unread #1 - May 22, 2012 at 2:30 AM
  2. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Switch Blocks

    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;
            }
     
  3. Unread #2 - May 26, 2012 at 12:09 AM
  4. A Man(level-3)
    Joined:
    Jun 28, 2010
    Posts:
    288
    Referrals:
    0
    Sythe Gold:
    3

    A Man(level-3) Forum Addict

    Switch Blocks

    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.
     
  5. Unread #3 - May 26, 2012 at 7:50 AM
  6. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Switch Blocks


    You're algood mate. I guess I forgot to mention more on breaks.
     
< Question about threading | [VB.NET][TUT] Class CountryIP (With C# example) >

Users viewing this thread
1 guest


 
 
Adblock breaks this site