-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_05_switch.slide
82 lines (43 loc) · 1.65 KB
/
01_05_switch.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
# switch
## switch
syntax:
switch{
case condition1:
// do stuff
}
example:
.play -numbers _concepts/01_introduction/05switch/01switch.go /^func main/,/^}/
## switch: default
syntax:
switch{
case condition1:
// do stuff
default:
// do stuff
}
example:
.play -numbers _concepts/01_introduction/05switch/02default.go /^func main/,/^}/
## switch: alternative to if-else ladder
simplifies long if-else ladder
example: if else ladder
.play -numbers _concepts/01_introduction/05switch/03a_ifelse_ladder.go /^func main/,/^}/
## switch: alternative to if-else ladder
example: simplified with switch
.play -numbers _concepts/01_introduction/05switch/03b_switch.go /^func main/,/^}/
## switch: constant case expression
lets take another example
.play -numbers _concepts/01_introduction/05switch/04a_constant_case.go /^func main/,/^}/
: note: all the operations here are equals with color variable on the left side.
: .
: such operations can be simplified to.... (next slide)
## switch: constant case expression
that can be simplified as below
.play -numbers _concepts/01_introduction/05switch/04b_constant_case.go /^func main/,/^}/
## fallthrough
executes the next block without checking the condition
: unlike some programming languages, switch in Go doesn't fall through by default. we have to explicitly use the fallthrough keyword.
TODO: add fallthrough example
## break
TODO: need to understand if break can be used with switch without a for loop. and also need to understand why break is associated with a switch, rather than the for loop.
## type switch
_covered in later slides with interfaces_