Skip to content

Commit

Permalink
Merge pull request #71 from tsingbx/for-each
Browse files Browse the repository at this point in the history
add doc for foreach
  • Loading branch information
xushiwei authored Dec 21, 2023
2 parents a343cfb + e8e76d0 commit c9459d4
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 32 deletions.
2 changes: 2 additions & 0 deletions 114-For/for-0.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# For loop
// Go+ has only one looping keyword: for, with several forms.
26 changes: 26 additions & 0 deletions 114-For/for-1.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 1、for/<-
// This is the most common form. You can use it with a slice, map, numeric range or custom iterators.
//
// The for value <- arr/map form is used for going through elements of a slice or a map.
//
// If an index is required, an alternative form for index, value <- arr can be used.

names := ["Sam", "Peter"]
for i, name <- names {
println i, name
}

m := {"one": 1, "two": 2}
for key, val <- m {
println key, val
}
for key, _ <- m {
println key
}

for val <- names {
println val
}
for val <- m {
println val
}
14 changes: 14 additions & 0 deletions 114-For/for-2.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 2、Range for
// You can use range expression (start:end:step) in for loop.

for i <- :5 {
println i
}

for i <- 1:5 {
println i
}

for i <- 1:5:2 {
println i
}
11 changes: 11 additions & 0 deletions 114-For/for-3.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 3、for/<-/if
// All loops of for/<- form can have an optional if condition.

numbers := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for num <- numbers if num%3 == 0 {
println num
}

for num <- :10 if num%3 == 0 {
println num
}
16 changes: 16 additions & 0 deletions 114-For/for-4.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 4、Condition for
// The condition can be omitted, resulting in an infinite loop. You can use break or return to end the loop.

sum := 0
i := 1
for i <= 100 {
sum += i
i++
}
println sum

for {
if sum > 0 {
break
}
}
9 changes: 9 additions & 0 deletions 114-For/for-5.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 5、C for
// 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.

for i := 0; i < 10; i += 2 {
if i == 6 {
continue
}
println i
}
25 changes: 25 additions & 0 deletions 114-For/for-6.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# break and continue
// How do you get out of an infinite for loop without using the keyboard or
// turning off your computer? That’s the job of the break statement. It exits the
// loop immediately, just like the break statement in other languages. Of course,
// you can use break with any for statement, not just the infinite for statement.
//
// Go+ also includes the continue keyword, which skips over rest of the body of a
// for loop and proceeds directly to the next iteration. Technically, you don’t need
// a continue statement.

for i := 1; i <= 100; i++ {
if i%3 == 0 && i%5 == 0 {
println("FizzBuzz")
continue
}
if i%3 == 0 {
println("Fizz")
continue
}
if i%5 == 0 {
println("Buzz")
break
}
println(i)
}
18 changes: 18 additions & 0 deletions 114-For/for-7.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Labeling Your “for” Statements
// By default, the break and continue keywords apply to the for loop that
// directly contains them. What if you have nested for loops and you want to exit
// or skip over an iterator of an outer loop? Let’s look at an example. We’re going
// to modify our string iterating program to stop iterating through a string as
// soon as it hits a letter “l”

samples := []string{"hello", "apple_π!"}
outer:
for _, sample := range samples {
for i, r := range sample {
println(i, r, string(r))
if r == 'l' {
continue outer
}
}
println()
}
31 changes: 0 additions & 31 deletions 114-For/for.gop

This file was deleted.

1 change: 1 addition & 0 deletions 121-For-Each/for-each-1.gop
Original file line number Diff line number Diff line change
@@ -0,0 +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.
18 changes: 18 additions & 0 deletions 121-For-Each/for-each-2.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# The for-range Statement
// What makes a for-range loop interesting is that you get two loop variables.
// The first variable is the position in the data structure being iterated, while the
// second is value at that position. The idiomatic names for the two loop variables
// depend on what is being looped over. When looping over an array, slice, or
// string, an i for index is commonly used. When iterating through a map, k (for
// key) is used instead.
//
// The second variable is frequently called v for value, but is sometimes given a
// name based on the type of the values being iterated.
//
// If you don’t need to access the key, use an underscore (_) as the variable’s name. This tells Go+ to ignore the
// value.

evenVals := []int{2, 4, 6, 8, 10, 12}
for _, v := range evenVals {
println(v)
}
15 changes: 15 additions & 0 deletions 121-For-Each/for-each-3.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Iterating Over Maps
// There’s something interesting about how a for-range loop iterates over a
// map.

m := map[string]int{
"a": 1,
"c": 3,
"b": 2,
}
for i := 0; i < 3; i++ {
println("Loop", i)
for k, v := range m {
println(k, v)
}
}
1 change: 0 additions & 1 deletion 121-For-Each/for-each.gop

This file was deleted.

0 comments on commit c9459d4

Please sign in to comment.