[Solved] Else statement run even when if statement is true (Swift) [closed]


It does not crash, it doesn’t even compile. The following is probably what you want:

var isPrime = true

var number = "Four"

var i = 2

if let numInt = Int(number) {
    while i < numInt {
        if numInt % i == 0  {
            isPrime = false
        }
        i += 1
    }
} else {
    print("Sorry, this is not a valid number")
}

This will print "Sorry, this is not a valid number". If you set var number = "123" it will run the loop.

0

solved Else statement run even when if statement is true (Swift) [closed]