-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathforloop.go
73 lines (51 loc) · 1.36 KB
/
forloop.go
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
package main
import (
"fmt"
a "github.com/benhalstead/gotraining/tutorial"
)
// There are no while or do while loops in Go. The for statement is flexible enough to simulate those
func main() {
// The standard 'for' statement follows the C convention with an initialisation, a test and an increment/decrement
a.Section("For statement")
for i := 0; i < 2; i++ {
fmt.Printf("Counter is %d\n", i)
}
// If you just have a test, you can simulate a while loop
a.Section("For statement as a 'while' loop")
n := 3
for n >= 0 {
fmt.Printf("Counter is %d\n", n)
n--
}
// continue 'skips' an iteration in a loop
a.Section("Using continue in a for loop")
for i := 0; i < 5; i++ {
if i == 2 || i == 4 {
fmt.Printf("Ignoring %d\n", i)
continue
}
fmt.Printf("Counter is %d\n", i)
}
// break exits the current structure early
a.Section("Breaking out of a for loop")
for i := 0; i < 5; i++ {
if i == 2 {
fmt.Printf("Breaking at %d\n", i)
break
}
fmt.Printf("Counter is %d\n", i)
}
// Labels can be used to break to specific a.Sections of code - useful for nested loops
a.Section("Breaking to a label")
MyLabel:
for i := 0; i < 2; i++ {
fmt.Printf("Outer is %d\n", i)
for j := 0; j < 3; j++ {
if i == 1 && j == 2 {
fmt.Printf("Breaking to label at %d:%d\n", i, j)
break MyLabel
}
fmt.Printf("Inner is %d\n", j)
}
}
}