Is there any difference between using multiple if statements and else if statements?

后端 未结 4 517
情深已故
情深已故 2021-02-01 14:46

This question pertains specifically to shell scripts, but could be about any programming language.

Is there any difference between using multiple if stateme

4条回答
  •  日久生厌
    2021-02-01 14:54

    if (x == 0) {
        // 1
    }
    
    
    if (x >= 0) {
        // 2
    }
    
    if (x <= 0) {
        // 3
    }
    

    Above code will produce different value than the code below for x=0.

    if (x == 0) {
        // 1
    } else if (x >= 0) {
        // 2
    } else {
       // 3
    }
    

    In the first case all the statements 1, 2, and 3 will be executed for x = 0. In the second case only statements 1 will be.

提交回复
热议问题