-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgad-go-loops.go
92 lines (78 loc) · 2.14 KB
/
gad-go-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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import "fmt"
func main() {
fmt.Println("Hold on!")
for i := 0; i < 3; i++ {
fmt.Printf("Numbers %d \n", i)
}
/*
Use of BREAK statement
It is used to terminate the for loop abruptly b4 it finishes its normal
execution and move the control to the line of code just after the for loop.
*/
for b := 1; b <= 8; b++ {
if b > 4 {
// Here, Loop is terminated if b > 2
break
}
fmt.Printf("%d ", b)
}
fmt.Printf("\n Break line after looping.\n")
/*
The continue statement:
It is used to skip the current iteration of the for loop. All code
present in a for loop after the continue statement will not be executed
for the current iteration. The loop will move on to the next iteration.
*/
for k := 1; k <= 20; k++ {
if k%2 == 0 { // U can use if k%2 != 0 for even numbers
continue
}
fmt.Printf("%d ", k)
}
/*
There, if i%2 == 0 checks if the remainder of dividing i by 2 is 0.
If it is zero, then the number is even and continue statement is executed
and the control moves to the next iteration of the loop. Hence the print
statement after the "continue" will not be called and the loop proceeds
to the next iteration. The output: 1 3 5 7 9 11 13 15 17 19
Nested for loops:
=================
Nested loop is a for loop which has another for loop inside it.
*/
fmt.Print("\n")
var n = 5
for m := 0; m < n; m++ {
for p := 0; p <= m; p++ {
fmt.Print("* ")
}
fmt.Println()
}
/*
Labels:
-------
Labels can be used to break the outer loop from inside the inner for loop.
*/
for g := 0; g < 4; g++ {
for h := 1; h < 5; h++ {
fmt.Printf("g = %d , h = %d \n", g, h)
}
}
fmt.Printf("**************************************\n")
/*
Let's rewrite the program above using labels
--------------------------------------------
What if we want to stop printing when i and j are equal. To do this we
need to break from the outer for loop. Adding a break in the inner for
loop when i and j are equal will only break from the inner for loop.
*/
outer:
for q := 0; q < 4; q++ {
for t := 1; t < 5; t++ {
fmt.Printf("q = %d , t = %d \n", q, t)
if t == q {
break outer
}
}
}
}