Skip to content

Commit c9459d4

Browse files
authored
Merge pull request #71 from tsingbx/for-each
add doc for foreach
2 parents a343cfb + e8e76d0 commit c9459d4

13 files changed

Lines changed: 155 additions & 32 deletions

File tree

114-For/for-0.gop

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# For loop
2+
// Go+ has only one looping keyword: for, with several forms.

114-For/for-1.gop

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 1、for/<-
2+
// This is the most common form. You can use it with a slice, map, numeric range or custom iterators.
3+
//
4+
// The for value <- arr/map form is used for going through elements of a slice or a map.
5+
//
6+
// If an index is required, an alternative form for index, value <- arr can be used.
7+
8+
names := ["Sam", "Peter"]
9+
for i, name <- names {
10+
println i, name
11+
}
12+
13+
m := {"one": 1, "two": 2}
14+
for key, val <- m {
15+
println key, val
16+
}
17+
for key, _ <- m {
18+
println key
19+
}
20+
21+
for val <- names {
22+
println val
23+
}
24+
for val <- m {
25+
println val
26+
}

114-For/for-2.gop

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 2、Range for
2+
// You can use range expression (start:end:step) in for loop.
3+
4+
for i <- :5 {
5+
println i
6+
}
7+
8+
for i <- 1:5 {
9+
println i
10+
}
11+
12+
for i <- 1:5:2 {
13+
println i
14+
}

114-For/for-3.gop

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 3、for/<-/if
2+
// All loops of for/<- form can have an optional if condition.
3+
4+
numbers := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5+
for num <- numbers if num%3 == 0 {
6+
println num
7+
}
8+
9+
for num <- :10 if num%3 == 0 {
10+
println num
11+
}

114-For/for-4.gop

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 4、Condition for
2+
// The condition can be omitted, resulting in an infinite loop. You can use break or return to end the loop.
3+
4+
sum := 0
5+
i := 1
6+
for i <= 100 {
7+
sum += i
8+
i++
9+
}
10+
println sum
11+
12+
for {
13+
if sum > 0 {
14+
break
15+
}
16+
}

114-For/for-5.gop

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 5、C for
2+
// Finally, there's the traditional C style for loop. It's safer than the while form because with the latter it's easy to forget to update the counter and get stuck in an infinite loop.
3+
4+
for i := 0; i < 10; i += 2 {
5+
if i == 6 {
6+
continue
7+
}
8+
println i
9+
}

114-For/for-6.gop

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# break and continue
2+
// How do you get out of an infinite for loop without using the keyboard or
3+
// turning off your computer? That’s the job of the break statement. It exits the
4+
// loop immediately, just like the break statement in other languages. Of course,
5+
// you can use break with any for statement, not just the infinite for statement.
6+
//
7+
// Go+ also includes the continue keyword, which skips over rest of the body of a
8+
// for loop and proceeds directly to the next iteration. Technically, you don’t need
9+
// a continue statement.
10+
11+
for i := 1; i <= 100; i++ {
12+
if i%3 == 0 && i%5 == 0 {
13+
println("FizzBuzz")
14+
continue
15+
}
16+
if i%3 == 0 {
17+
println("Fizz")
18+
continue
19+
}
20+
if i%5 == 0 {
21+
println("Buzz")
22+
break
23+
}
24+
println(i)
25+
}

114-For/for-7.gop

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Labeling Your “for” Statements
2+
// By default, the break and continue keywords apply to the for loop that
3+
// directly contains them. What if you have nested for loops and you want to exit
4+
// or skip over an iterator of an outer loop? Let’s look at an example. We’re going
5+
// to modify our string iterating program to stop iterating through a string as
6+
// soon as it hits a letter “l”
7+
8+
samples := []string{"hello", "apple_π!"}
9+
outer:
10+
for _, sample := range samples {
11+
for i, r := range sample {
12+
println(i, r, string(r))
13+
if r == 'l' {
14+
continue outer
15+
}
16+
}
17+
println()
18+
}

114-For/for.gop

Lines changed: 0 additions & 31 deletions
This file was deleted.

121-For-Each/for-each-1.gop

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// In Go+ there is no foreach loop instead, the for loop can be used as “foreach“. There is a keyword range, you can combine for and range together and have the choice of using the key or value within the loop.

0 commit comments

Comments
 (0)