-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops.go
71 lines (50 loc) · 1.28 KB
/
loops.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
package examples
import (
"fmt"
)
func Loops() {
//FOR LOOP
for i := 0; i < 10; i++ {
fmt.Println("FOR LOOP WITH A COUNTER", i)
}
//Golang doesn't have "while" or "do while" loops but we can acheive the
//same with "for" loop by passing only boolean condition
i := 0
for i < 10 {
fmt.Println("FOR LOOP AS WHILE", i)
i++
}
//FOR loop without any condition will create an infinite loop.
i = 0
for {
if i >= 10 {
break //ending the loop on 10th iteration.
}
fmt.Println("INFINITE FOR LOOP", i)
i++
}
//We can also skip the condition part when writing a counter for loop. (also creates an infinite loop)
for j := 0; ; j++ {
if j >= 10 {
break //ending the loop on 10th iteration.
}
fmt.Println("COUNTER FOR LOOP WITHOUT CONDITION", j)
}
//FOR RANGE
//In Golang, the for range loop is a convenient and concise way to iterate over elements
//in various data structures.
//ARRAY (OR SLICE)
names := [3]string{"John", "Jane", "Joe"}
for index, value := range names {
fmt.Printf("FOR...RANGE ARRAY INDEX: %v, VALUE: %v\n", index, value)
}
//MAP
user := map[string]string{
"id": "1",
"name": "Daniel",
"Email": "[email protected]",
}
for key, value := range user {
fmt.Printf("FOR...RANGE MAP KEY: %v, VALUE: %v\n", key, value)
}
}