While
while condition is true {
Execute these statements
}while lastRoll == newDiceRoll {
newDiceRoll = Int.random(in 1...4)
}Check the condition. If it is true, perform the code in the curlies, then loop back up to the condition and check it again. Continue to repeat the code in the curlies while the condition is true.
Once condition is false, skip the curlies above and move down to the next lines of code.
Note: If the condition is false when you first check it in the while statement, then you’ll never execute whats in the curlies.
import UIKit
var diceRoll: Int
var rollCOunt = 0
diceRoll = Int.random(in: 1...6)
while diceRoll != 6 {
diceRoll = Int.random(in: 1...6)
rollCOunt += 1
}
print("It took \(rollCOunt) rolls to roll a 6")Repeat while
repeat {
Execute these statements
}
while as long as condition is true
repeat {
newDiceRoll = Int.random(in 1...4)
} while lastRoll == newDiceRollOnce condition os false, skip the curlies above and move down to the next lines of code.
Note: since you dont check the repeat condition until after the curles execute, then you always execute what’s in the curlies at least once.
import UIKit
var diceRoll: Int
var rollCOunt = 0
repeat {
diceRoll = Int.random(in: 1...6)
rollCOunt += 1
print("Dice roll #\(rollCOunt) = \(diceRoll) ")
} while diceRoll != 6
print("It took \(rollCOunt) rolls to roll a 6")