Skip to content

Commit 1831b8e

Browse files
committed
Adds for & while egs
1 parent df7efe4 commit 1831b8e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

codes/ch4_looping/for.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt";
4+
5+
func main() {
6+
7+
/**
8+
* `for` loop
9+
*
10+
* for [assignment]; [conditions]; [increment/decrement] { ... }
11+
*/
12+
for i := 0; i < 10; i++ {
13+
fmt.Println("i =", i)
14+
}
15+
16+
}

codes/ch4_looping/while.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt";
4+
5+
func main() {
6+
7+
/**
8+
* There is no while loop in Go-lang;
9+
* but we can use `for` loop to behave like `while` loop.
10+
*/
11+
i := 0;
12+
for i < 10 {
13+
fmt.Println("i =", i)
14+
15+
i++
16+
}
17+
18+
}

0 commit comments

Comments
 (0)