In this module we learn about conditional code. The key here is to use an “if” statement on each tile to check what is there. If it is a switch that is closed you’ll have to toggle it on. If it is a gem, you’ll have to collect it.
What is important to note is that a tile can have a switch that is either on or off, or it can have a gem. It is purely random which means you have no choice but to use an if statement.
This is common in real coding scenarios where you don’t know what kind of input you are receiving so you have to use if statements to test for a condition and then deal with it appropriately.
Checking for switches
This challenge introduces the if statement where you have to use:
if isOnClosedSwitch { toggleSwitch() }
The idea is to become familiar with the if statement and use it 3 times for each of the tiles. It could be optimised by using a for loop but that is introduced in further challenges.
moveForward() moveForward() if isOnClosedSwitch { toggleSwitch() } moveForward() if isOnClosedSwitch { toggleSwitch() } moveForward() if isOnClosedSwitch { toggleSwitch() }
Using else if
Here, the else if concept is introduced. If by itself is fine, but in real world applications, the else if statement is used because often there are many conditions that need to be checked.
The way this challenge is structured is that a tile can either be a switch or a gem unlike the previous challenge where they were all switches. Therefore else if needs to be used.
moveForward() if isOnClosedSwitch { toggleSwitch() } else if isOnGem { collectGem() } moveForward() if isOnClosedSwitch { toggleSwitch() } else if isOnGem { collectGem() }
Looping conditional code
This challenge makes use of your prior knowledge of for loops in combination with conditional code and also uses a combination of the previous concept, the else if statement.
for i in 1 ... 12 { moveForward() if isOnClosedSwitch { toggleSwitch() } else if isOnGem { collectGem() } }
Conditional climb
This challenge attempts to introduce the concept of a boolean condition. A boolean has always been used because isOnGem or IsOnClosedSwitch are boolean conditions that are either true or false but the challenge aims to explicitly highlight that in an if statement, the condition is always a boolean.
There is a slight trick here where the loop has to repeat 16 times. It is not immediately obvious why it is 16 but this is explained in the video below.
for i in 1 ... 16 { if isOnGem { collectGem() turnLeft() } else { moveForward() } }
Defining smarter functions
This challenge continues to reuse all the concepts taught and requires the use of for loops, else if statements and also functions. This is why it is super important to understand each concept before moving on because they continue to be used in future challenges.
Here, the function
func collectOrToggle() { if isOnClosedSwitch { toggleSwitch() } else if isOnGem { collectGem() } }
helps to make the decision on each tile much neater.
func collectOrToggle { if isOnClosedSwitch { toggleSwitch() } else if isOnGem { collectGem() } } for i in 1 ... 2 { moveForward() moveForward() collectOrToggle() } turnLeft() moveForward() moveForward() turnLeft() for i in 1 ... 2 { moveForward() moveForward() collectOrToggle() } turnRight() moveForward() turnRight() for i in 1 ... 2 { moveForward() moveForward() collectOrToggle() }
Boxed in
This challenge is interesting because along with using all the previous concepts, you have to figure out how to write the for loop. This means you have to identify a pattern that you want to repeat and also get into a starting position of the pattern.
A little bit of thinking is required for this challenge. Check out the solution below or the video if required.
func checkTile() { if isOnClosedSwitch { toggleSwitch() } else if isOnGem { collectGem() } } moveForward() turnLeft() for i in 1 ... 4 { checkTile() moveForward() checkTile() turnLeft() moveForward() }
Decision tree
This challenge is different in that the switches and gems don’t randomly change positions. This means you can use them as markers to change your route. That is, when you reach a gem on the main path, that is a cue to turn right. When you reach a closed switch, turn left.
The other concept is to wrap some of the code into functions such as “solveRightSide()” and “solveLeftSide()”.
func solveRightSide() { collectGem() turnRight() moveForward() moveForward() moveForward() turnLeft() moveForward() collectGem() turnLeft() turnLeft() moveForward() turnRight() moveForward() moveForward() moveForward() turnRight() } func solveLeftSide() { toggleSwitch() turnLeft() moveForward() collectGem() turnLeft() turnLeft() moveForward() turnLeft() } for i in 1 ... 5 { moveForward() if isOnGem { solveRightSide() } else if isOnClosedSwitch { solveLeftSide() } }