Unable to get behaviour of Switch case in java

后端 未结 8 694
迷失自我
迷失自我 2021-01-23 14:07

I have written small code in java 6

public class TestSwitch{

public static void main(String... args){
    int a = 1;
    System.out.println("start");
          


        
相关标签:
8条回答
  • 2021-01-23 14:27
    switch(a){
        case 1:{
            System.out.println(1);
            case 3:
    

    You cannot nest cases like this. Switch should look either like :

        switch(a){
        case 1:
            System.out.println(1);
            break;
        case 3:
           ....
    

    or like this :

    switch(a){
        case 1:
            System.out.println(1);
            switch(a) {
                case 3:
                    //...
                    break;
                case 5 :
                    //...
    

    And if you don't add break at the end of a case, the execution will continue after. You should add a break at the end of each cases if they should be executed separately.

    0 讨论(0)
  • 2021-01-23 14:34

    Switch replaces if else's but switch syntax != If else syntax.

    You forgot to put break after each case.

    So conditions under fall through.

    Example:

    case 0:
              mColor.setText("#000000");
              break;
    

    You can find that in docs

    The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

    public static void main(String... args){
            int a = 1;
            System.out.println("start");
            switch(a){
                case 1: 
                    System.out.println(1);
                    break;
                case 2:
                      System.out.println(2);
                      break;
                case 3:
                    System.out.println(3);
                    break;
                case 4:
                    System.out.println(4);
                    break;
                case 5:
                    System.out.println(5);
                    break;
                case 7:
                    System.out.println(7);
                    break;
                default:
                    System.out.println("nothing");
    
                }
    
    0 讨论(0)
  • 2021-01-23 14:35

    As their no break statement in case 1: the execution directly jumps to case 2: and ends up printing "start 1 2 end"..

    0 讨论(0)
  • 2021-01-23 14:36

    You have not added break statement before case 2.

    Refer this to know more about fall through.

    Each break statement terminates the enclosing switch statement. Control flow continues
    with the first statement following the switch block. The break statements are necessary
    because without them, statements in switch blocks fall through: All statements after
    the matching case label are executed in sequence, regardless of the expression of
    subsequent case labels, until a break statement is encountered.
    
    0 讨论(0)
  • 2021-01-23 14:37
    int a = 1;
    System.out.println("start");
    switch (a) {
    case 1: {
        System.out.println(1);
        break;
    }
    case 3: {
        System.out.println(3);
        break;
    }
    case 4: {
        System.out.println(4);
        break;
    }
    case 2: {
        System.out.println(2);
        break;
    }
    case 5: {
        System.out.println(5);
        //no break will fall through and print 7 too 
    }
    case 7: {
        System.out.println(7);
        break;
    }
    default:{
        System.out.println("none");
    }
    
    }
    
    0 讨论(0)
  • 2021-01-23 14:40

    You have wrong closing braces before case 2. case 3,4 are interpreted as labels not cases.

    0 讨论(0)
提交回复
热议问题