-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_04_if.slide
101 lines (58 loc) · 1.67 KB
/
01_04_if.slide
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# if
## if
syntax
if <condition>{
// do stuff
}
comparison operators: **== != < <= > >=**
.play -numbers _concepts/01_introduction/04if/01if.go /^func main/,/^}/
.link https://go.dev/ref/spec#If_statements see specs
## if multiple conditions
if <multiple conditions>{
// do stuff
}
logical operators: **&& || !**
examples:
if (a > b) && (i == j){}
if !(a > b){}
if a <= b{} // same as above not example
: for simplicity of the example, we are using empty curly parenthesis. but in real scenarios, we will have code within
.play -numbers _concepts/01_introduction/04if/02if.go /^func main/,/^}/
## else
syntax
if <condition>{
// do stuff
}else{
// do other stuff
}
example
.play -numbers _concepts/01_introduction/04if/03else.go /^func main/,/^}/
## elseif
syntax
if <condition>{
// do stuff
}else if <other condition>{
// do other stuff
}else{
// do someother stuff
}
example
.play -numbers _concepts/01_introduction/04if/04elseif.go /^func main/,/^}/
## quiz
## quiz
what is the output
.play -numbers _concepts/01_introduction/04if/05q1.go /^func main/,/^}/
: answer: syntax error. else should always start in the same line as the closing curly braces of the previous if/else-if block
## quiz
what is the output
.play -numbers _concepts/01_introduction/04if/05q2.go /^func main/,/^}/
: answer: "nothing is correct". the else case will be executed
## nested if
syntax:
if <condition>{
// do stuff
if <condition>{
// do stuff
}
}
.play -numbers _concepts/01_introduction/04if/06nestedif.go /^func main/,/^}/