HW#25 Practice of While Loops

We’ve covered the use of Loops and I did For Loops in Playgrounds the other day. Today, I’m going to practice While Loops. It’s also in Learn to Code 1.

As it mentions in the introduction, we can use a while loop to repeat a command or set of commands, while a condition is true.

While nailIsStickingOut{

hammerNail()

}

It is a relatively simple way to repeat a command.

1. The first challenge is Running Code While…

This one is very simple.

The code is as follows:

while isOnClosedSwitch {

toggleSwitch()

moveForward()

}

2. The second challenge is Creating Smarter While Loops.

Here we just need to remember ! means not. So while !isBlocked means while it is not blocked. Of course, we need to use if statement to accomplish this task. We will need to write the command: isOnClosedSwitch, toggleSwitch(). Here is the code:

while !isBlocked {

if isOnClosedSwitch {

toggleSwitch ()

}

moveForward()

}

3. The third challenge is Choosing the Correct Tool. This one is to use the best loop to collect all the gems. We need to find the pattern first. As we can see from the game, in order to collect a gem, we have to do the following movements.

moveForward()turnLeft()collectGem()turnRight()

So, we will write a function of the above movements, which is func turnAndCollectGem(). Then we use while loop to repeat the command.

The code is as follows:

func turnAndCollectGem() {

moveForward()

turnLeft()

moveForward()

collectGem()

turnRight()

}

while !isBlocked{

turnAndCollectGem()

}

4. The fourth challenge is Four by Four. This task is to choose the best loops and toggle the switches. Again, we just need to calculate the movements we need to finish the work. We will use both while loop and if statement. The code is:

while !isBlocked{

moveForward()

moveForward()

moveForward()

turnRight()

if isOnClosedSwitch{

toggleSwitch()

}

}

5. The fifth challenge is Turned Around. Here we are to choose the tools that will help us find the most efficient solutions. After studying the game, we need to collect the gems and in order to do that, we have to basically move, collect gem, and turn. So, we can first declare a function: func moveCollectTurn(), which includes the movements: moveForward(), collectGem(), turnLeft()

Then, we will use for loops to repeat the above commands in the func.

The code is as follows:

func moveCollectTurn(){

moveForward()

collectGem()

turnLeft()

}

for i in 1 … 4 {

moveCollectTurn()

moveCollectTurn()

moveForward()

turnRight()

}

6. The sixth challenge is Land of Bounty. This is to find an efficient solution that works the best. In fact, there are many different approaches. The most important part is how to declare the function that helps us to toggle switches and collect gem. This one took me a little longer than I thought, but it was fun! Here I wrote my code as follows:

func checkSwithGem(){

if isOnClosedSwitch{

toggleSwitch()

} else if isOnGem {

collectGem()

}

}

func solveLine() {

moveForward()

while !isBlocked {

checkSwithGem()

moveForward()

}

}

solveLine()

turnRight()

moveForward()

turnRight()

solveLine()

turnLeft()

moveForward()

turnLeft()

solveLine()

7. The seventh challenge is Nesting Loops. This one is what we spent most of time learning in tonight’s class. It’s a bit complicated but not that hard if you observe carefully. Basically, in this task, the nesting loops are to let us use a loop inside another loop to move around a spiral.

The code is as follows:

while !isBlocked {

while !isOnGem{

moveForward()

}

collectGem()

turnLeft()

}

8. The eighth challenge is Random Rectangles. This task is to use nested loops and conditions to move around a changing world. After having finished the Nesting Loops practice, this one is not hard at all. Here is the code:

while !isBlocked {

while !isBlocked {

moveForward()

}

turnRight()

}

toggleSwitch()

9. The last one is You’re Always Right. This one is to let us apply what we’ve done in this practice to write any coding or pattern we like as long as we can accomplish the task. While I was tackling this task, it reminded me of the video game “Monument Valley”.

I love this game!

If you have played this wonderful game (the music is fantastic), I think it’ll be very easy to reinforce the concept of While Loops here. The code is as follows:

while !isBlocked {

while !isBlocked {

moveForward()

if isOnClosedSwitch {

toggleSwitch()

}

}

turnRight()

if isOnGem {

collectGem()

}

}

Well, this is the power of While Loops! I reckon it is really fun. Until next time, cheers!

--

--

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

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