Chapter: Conditional Code

Conditional statements are the cornerstone of programming. Again, there were no special features worth mentioning, except for the fact that we also covered Boolean expressions in this topic.

So Rosi learned right away that there is a TRUE or a FALSE behind a conditional expression. So according to this basic principle.

1
2
3
4
5
6
7
// Boolean expressions in conditional statements.
if true {
    ...    
}
if false {
    ...
}

last-challenge-for-loops

Challenge solution code: Conditional Code (last challenge)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// MIT License (MIT) Copyright (c) 2021 Rosi Regner
// Challenge: Conditional Code (last challenge)

func onGem() {
    collectGem()
    turnRight()
    moveForward()
    moveForward()
    moveForward()
    turnLeft()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    turnRight()
    moveForward()
    moveForward()
    moveForward()
    turnRight()
}
func onSwitch() {
    toggleSwitch()
    turnLeft()
    moveForward()
    collectGem()
    turnRight()
    turnRight()
    moveForward()
    turnLeft()
}

for i in 1 ... 6 {
    moveForward()
    if isOnGem {
        onGem()
    }
    if isOnClosedSwitch {
        onSwitch()
    }
}

Our verdict

Another small building block for learning programming successfully completed. :-)

Read next: Learn to Code 1 - Chapter Logical Operators