HW#24 Practice of “For Loops”

After having done quite a lot in Storyboard, I decided to do some more practice in Swift Playgrounds. Today I chose “For Loops” to do some coding.

As it mentions in the introduction, to break down coding tasks, we write functions for repeated patterns. Now, we’ll call one function multiple times using a loop. With a loop, we write our code just once and enter the number of times to repeat it. I’m sure many of us have done that already for other Storyboard assignments.

1. The firs challenge is Using loops. This one is easy. The code I wrote is as follows:

for i in 1 …5 {

moveForward()

moveForward()

collectGem()

moveForward()

}

2. The second challenge is Looping All the Sides.

Again, this one is simple. The code is as follows:

for i in in …4 {

moveForward()

collectGem()

moveForward()

moveForward()

moveForward()

turnRight()

}

3. The third challenge is To the Edge and Back.

This one starts to get a bit challenging. One thing we need to notice is that a func turnaround() has to be declared and in the function there are two times of turnLeft(). Another func is func moveTwoToggleAndReturn() we can declare to include the necessary movements, including toggling switches.

Then, we add the loop at the end.

Here is the code I wrote:

func turnAround(){

turnLeft()

turnLeft()

}

func moveTwoToggleAndReturn(){

moveForward()

moveForward()

toggleSwitch()

turnAround()

moveForward()

moveForward()

turnAround()

}

for i in 1…4{

moveTwoToggleAndReturn()

turnRight()

}

4. The fourth challenge is Loop Jumper. This one is not hard at all.

The code is as follows:

for i in 1…5{

moveForward()

turnLeft()

moveForward()

moveForward()

collectGem()

turnRight()

}

As we can see from this code, how powerful a loop is. It saves us a lot of time!

5. The fifth challenge is Branch Out. This one isn’t that difficult either. What we need to do first is to closely see what action we must take to achieve the goal. Then, we can decide what to put in our code. Again, at the beginning, we need to declare a func —func turnaround(), which includes the action turnLeft() twice.

Then, we declare another func — funchandleBigStairs(), which includes the loop for moving forward seven times. Then, we add toggleSwitch() and turnaround(); and put another loop for moving forward 7 times. After that, we make a move, turnRight(). Last, we add a loop for 3 times, which includes moveForward() twice, turnRight() once and handleBigstairs().

The code is as follow:

func turnAround(){

turnLeft()

turnLeft()

}

func handleBigStairs(){

for i in 1 … 7 {

moveForward()

}

toggleSwitch()

turnAround()

for i in 1 … 7 {

moveForward()

}

turnRight()

}

for i in 1 … 3 {

moveForward()

moveForward()

turnRight()

handleBigStairs()

}

6. The sixth challenge is Gem Farm. This one is to decompose multiple patterns into functions and loops. It gets a bit daunting but I managed to do it. Here is the code:

func turnAround(){

turnRight()

turnRight()

}

func toggleTwoSwitches(){

for i in 1 … 2 {

moveForward()

toggleSwitch()

}

turnAround()

for i in 1 … 2 {

moveForward()

}

}

func collectTwoGems(){

for i in 1 … 2 {

moveForward()

collectGem()

}

turnAround()

for i in 1 … 2 {

moveForward()

}

}

func cleanOneRowOfSwitchesAndGems(){

toggleTwoSwitches()

collectTwoGems()

}

for i in 1 … 2 {

turnLeft()

cleanOneRowOfSwitchesAndGems()

turnRight()

moveForward()

}

turnLeft()

cleanOneRowOfSwitchesAndGems()

As we can see now, we have to use a lot of loops in this challenge. It’s not easy but fun!

7. The seventh challenge is Four Stash Sweep. This is to practice pattern finding, decomposition, functions, and for loops.

Again, we must study what kind of action we need and how to execute it. This task consists of what we have done previously and it’s a good review and practice. Here is the code:

func takeRightCorner() {

moveForward()

turnRight()

moveForward()

turnAround()

}

func turnAround(){

turnRight()

turnRight()

}

func moveAndCollectThree() {

for i in 1 … 3 {

collectGem()

moveForward()

}

}

func handleCluster(){

moveForward()

moveForward()

collectGem()

turnAround()

takeRightCorner()

moveAndCollectThree()

}

for i in 1 … 4 {

handleCluster()

}

Well, one thing I learnt is that it is very critical to declare a function using func. And we can create our own type names for the func. One thing we have done a lot in our Storyboard assignments. Cool, right?

That’s it for this practice. Cheers!

--

--

Jerski
彼得潘的 Swift iOS / Flutter App 開發教室

Studied Linguistics in university and have been intrigued by code writing. Now this journey of Swift just began.