-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor.go
59 lines (52 loc) · 1.15 KB
/
for.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
package main
import "fmt"
func main() {
/*
1st statement: initializer
2nd statement : boolean result statement
3rd statement: incrementer
*/
fmt.Println("\nBasic Loop")
for i := 0; i < 10; i++ {
fmt.Println(1)
}
/*
Using multiple initialized values
*/
fmt.Println("\nTwo variables")
for i, j := 0, 10; i < 10; i, j = i+1, j-1 {
fmt.Println(i, j)
}
/*
You can actually define the iterator variable outside
of the for scope, leaving the first statement empty
MIND THE SEMICOLON!
*/
fmt.Println("\nOuter scope variable")
i := 0
for ; i < 10; i++ {
fmt.Println(i)
}
/*
You can also remove the third statement.
In this example we're increasing i inside the loop (otherwise we'd get an infinite loop)
MIND THE SEMICOLON!
*/
fmt.Println("\nIncrementer moved inside the loop")
for i = 0; i < 10; {
fmt.Println(i)
i++
}
/*
In Go the while keyword doesn't exist.
A while loop is just a special for loop.
You can have the same result others languages achieve with while
by removing the first and third statements
NO NEED TO WRITE SEMICOLONS
*/
fmt.Println("\nWhile loop")
i = 0
for i < 10 {
fmt.Println(i)
}
}