Let's Loop...
While loop - checking condition before running the code.
Repeat while - Running the code before checking condition.
== While Loop Guessing Game ==
import Foundation // import foundation framework which has random number generator
let numberIAmThinking = 20
var currentGuess = -1
print("Thinking of a number between 1 and 20. Guess what it is.")
while currentGuess != numberIAmThinking {
//While will run until condition is not true
//Int(arc4dandom_uniform(20) creates a random # 0 to 19
currentGuess = Int(arc4random_uniform(20)) + 1
print("Hmmm... Let me guess. Is it \(currentGuess)")
}
print("The current guess number is \(currentGuess)")
== Repeat While - Let's Shrink Me - ==
import Foundation
var shrinking = "Help, I am shrinking away...!"
repeat {
print(shrinking)
shrinking = String(shrinking.characters.dropLast())
}
while shrinking.characters.count > 0
== Homework ==
- Create 1 variable for each student name in the class
- Concatenate the variables
- Use while or repeat while to shrink the name to 3 characters