2.3 KiB
2.3 KiB
Which one of these is a valid loop statement in Go?
- while
- forever
- until
- for CORRECT
4: Correct. There is only one loop statement in Go.
What does this code print?
for i := 3; i > 0; i-- {
fmt.Println(i)
}
- 3 2 1 CORRECT
- 1 2 3
- 0 1 2
- 2 1 0
What does this code print?
for i := 3; i > 0; {
i--
fmt.Println(i)
}
- 3 2 1
- 1 2 3
- 0 1 2
- 2 1 0 CORRECT
What does this code print?
for i := 3; ; {
if i <= 0 {
break
}
i--
fmt.Println(i)
}
- 3 2 1
- 1 2 3
- 0 1 2
- 2 1 0 CORRECT
What does this code print?
for i := 2; i <= 9; i++ {
if i % 3 != 0 {
continue
}
fmt.Println(i)
}
- 3 6 9 CORRECT
- 9 6 3
- 2 3 6 9
- 2 3 4 5 6 7 8 9
How can you simplify this code?
for ; true ; {
// ...
}
-
for true { }
-
for true; { }
-
CORRECT
for { }
-
for ; true { }
What does this code print?
Let's say that you run the program like this:
go run main.go go is awesome
for i, v := range os.Args[1:] {
fmt.Println(i+1, v)
}
-
CORRECT
1 go 2 is 3 awesome
-
go is awesome
-
0 go 1 is 2 awesome
-
1 2 3
What does this code print?
Let's say that you run the program like this:
go run main.go go is awesome
for i := range os.Args[1:] {
fmt.Println(i+1)
}
-
1 go 2 is 3 awesome
-
go is awesome
-
0 go 1 is 2 awesome
-
CORRECT
1 2 3
What does this code print?
Let's say that you run the program like this:
go run main.go go is awesome
for _, v := range os.Args[1:] {
fmt.Println(v)
}
-
1 go 2 is 3 awesome
-
CORRECT
go is awesome
-
0 go 1 is 2 awesome
-
1 2 3
What does this code print?
Let's say that you run the program like this:
go run main.go go is awesome
var i int
for range os.Args {
i++
}
fmt.Println(i)
- go is awesome
- 1 2 3
- 2
- 4 CORRECT
4: As you can see, you can also use a for range statement for counting things.