2

If an if statement gets called and all the conditions are true, do all the else if statements get called also?

like:

if coins > 19 && speedLvl == 1 {
    speedLvl = 2
    coins = coins - 20
}

else if coins > 49 && speedLvl == 2 {
    speedLvl = 3
    coins = coins - 50
            }

else if coins > 99 && speedLvl == 3 {
    speedLvl = 4
    coins = coins - 100
}

If the player has 1000 coins do then speedLvl the go to 4 ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • "If the player has 1000 coins do then speedLvl the go to 4 ?" no, would you like it to? – Alexander Sep 27 '17 at 15:46
  • @luchsi Your question it is unclear. Please edit it and add what is the expected results. Looks like you should use a switch statement and you can use fallthrough keyword. You can check this answer to see how it works https://stackoverflow.com/questions/31782316/cant-convert-uicolor-from-hex-colour-string-in-swift/31782490?s=1|3.9465#31782490 – Leo Dabus Sep 27 '17 at 16:03

2 Answers2

4

No, and you can visualize it like this:

if coins > 19 && speedLvl == 1 {
    speedLvl = 2
    coins = coins - 20
}
else {
    if coins > 49 && speedLvl == 2 {
        speedLvl = 3
        coins = coins - 50
    }
    else {
        if coins > 99 && speedLvl == 3 {
            speedLvl = 4
            coins = coins - 100
        }
    }
}

Although this code would be more easily written in Swift 4 as:

switch (speedLvl, coins) {
case (1, 20..<50):
    speedLvl += 1
    coins -= 20

case (2, 50..<100): 
    speedLvl += 1
    coins -= 50 

case (3, 100...):
    speedLvl += 1
    coins -= 100

default: break;
}

or better yet, perhaps:

let levelUpCosts = [0, 20, 50, 100]

let levelUpCost = levelUpCosts[speedLvl]
if levelUpCost < coins {
    coins -= levelUpCost
    speedLvl += 1
}

If you want to multiple level ups to be possible, all in one shot, then you can do something like this:

let levelUpCosts = [0, 20, 50, 100]

var affordedLevelUpsCost = 0
let affordedLevelUps = levelUpCosts.lazy.prefix(while: { cost in
    let newCost = affordedLevelUpsCost + cost
    let canAffordLevelUp = newCost < coins
    if canAffordLevelUp { affordedLevelUpsCost = newCost }
    return canAffordLevelUp
})

speedLvl += affordedLevelUps.count
coins -= affordedLevelUpsCost
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • Curious, what is the name of this new Swift4 feature: `case (1, 20..<50):` – mfaani Sep 27 '17 at 16:17
  • 1
    @Honey That's just tuple matching, it's not new to Swift 4. The half range operator `100...` is what's new from Swift 4 – Alexander Sep 27 '17 at 16:26
  • Interesting, I just tried that in Swift 3 ie doing `100...` will result in the following error: `'...' is not a postfix unary operator`. Yet in Swift4, it would just work... – mfaani Sep 27 '17 at 18:23
0

No, the if-else conditions will move on after a successful condition is reached.

So, if coins > 19 && speedLvl == 1 if this condition is true, the rest of the else-if conditions won't even be checked.

If the first if-statement is not true, then it will go to each if-else statement until it reaches a true condition. Once it reaches a true if-else condition, it will not check any remaining if-else conditions.

If you would like each of these conditions to be checked, remove the else and have each as a stand alone if-statement to check each condition individually without any dependency on the other conditions.

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73