Loops in Swift Code | 循环 Xúnhuán

Hello everybody! Today in class we have learned 👩‍🏫 how to write loops 循环 (Xúnhuán) in Swift code 👩‍💻. We learned two kinds of loops ➰.

The first type of loop that we learned was a loop ➿ that repeats itself for a certain amount of times⏲. We learned 🏫 how to write this kind of loop in Swift code. Here’s an example.

for i in 1 … 4 {
moveForward()
turnRight()
moveForward()
turnLeft()
}

for i in 1 ... 4 {
moveForward()
turnRight()
moveForward()
turnLeft()
}

Next we learned another loop ⭕ that repeats itself until the condition is false🙅‍♀️. This loop is called a while loop. Here’s an example.

while thereIsACoin {
moveForward()
collectCoin()
}

while thereIsACoin {
moveForward()
collectCoin()
}

In a while loop the loop will go forward ⏩ and collect the coin 💰 nonstop until the is no more coins ☹ in front of the coin collecter to collect. This is what we have accoplished today in class 👩‍🎓👨‍🎓.

We will learn much more very soon. Goodbye!!😃😃

In the meantime, please do your homework, and use the Swift documentation (aka “instructions”).

error: Content is protected !!