feat: add go lang for loop
This commit is contained in:
35
single_file_tests/forloop.go
Normal file
35
single_file_tests/forloop.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
sum := 0
|
||||
for i := 1; i <= 100; i++ {
|
||||
sum += i
|
||||
}
|
||||
fmt.Println("Sum from 1 to 100:", sum)
|
||||
|
||||
n := 1
|
||||
for n < 1000 {
|
||||
n *= 2
|
||||
}
|
||||
fmt.Println("n:", n)
|
||||
|
||||
s := 0
|
||||
j := 1
|
||||
for {
|
||||
if j > 100 {
|
||||
break
|
||||
}
|
||||
s += j
|
||||
j++
|
||||
}
|
||||
fmt.Println("s: ", s)
|
||||
|
||||
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
ss := 0
|
||||
for _, num := range nums {
|
||||
ss += num
|
||||
}
|
||||
fmt.Println("SS:", ss)
|
||||
}
|
||||
Reference in New Issue
Block a user