Understanding {++ For Loops++}.
Objective
To understand {++ for++} loops.
Go offers only "for" loops for doing repetitive tasks, if you come from some other languages there are no "while" or "do-while" loops in Go, instead "for" loops have a flexible syntax and offers all the looping functions.
General syntax is:
for condition {
do something
}
Structure
Navigate to our code folder
code/basic/
For our program create a new folder '08_for_loops'
code/basic/08_for_loops
And lets create a file 'for_loops.go' in it, finally the structure would look like this:
code/basic/08_for_loops/for_loops.go
Code
The code will be divided into four parts:
1.)
for loops
Review
Line 9 declared a for loop along with condition
for i := 0; i < 11; i++ {
We initial the variable "i" and set it to "0", then we say loop till "i is less than 11" and after each pass do "i++", i.e increment the value of "i" by 1.
Line 10 prints the value after each pass and on line 11 we increment the value of num
fmt.Println("Num =", num1)
num1++
2.)
for loops
Review
On line 17 we start a infinite loop, the for loop will keep on executing till it encounters a break condition or runs out of memory
for {
On line 19 we check for a break condition
if num2 < 10
3.)
for loops
Review
On line 29 we start a loop with operational statements, the loop will break when it fulfills the condition.
for num3 <= 30
Note we have declared and initialized "num3" on line 27
num3 := 20
4.)
for loops
Review
We declare a boolean condition on line 35, and start the loop on line 38
for cond
In order to break the loop we set the condition on line 39
if num4 >= 40 {
cond = false
}
Full Code
Complete For-Loop Code
package main
import "fmt"
func main() {
// Variation 1 - conditional loop
num1 := 0
fmt.Println("Starting for loop...")
for i := 0; i < 11; i++ {
fmt.Println("Num =", num1)
num1++
}
fmt.Println()
// Variation 2 - Infinite loop
num2 := 20
fmt.Println("Entering infinite loop...")
for {
// break condition
if num2 < 10 {
break
}
fmt.Println("Num =", num2)
num2--
}
fmt.Println()
// Variation 3 - Optional statements
num3 := 20
fmt.Println("Loop with optional statements...")
for num3 <= 30 {
fmt.Println("Num =", num3)
num3++
}
fmt.Println()
// Variation 4 - Boolean operators
cond := true
num4 := 30
fmt.Println("Loops with boolean operator...")
for cond {
if num4 >= 40 {
cond = false
}
fmt.Println("Num =", num4)
num4++
}
}
Run Code
Open your terminal and navigate to our folder
code/basic/08_for_loops
Once in the folder type the following command
go run for_loops.go
Output
Output
Starting for loop...
Num = 0
Num = 1
Num = 2
Num = 3
Num = 4
Num = 5
Num = 6
Num = 7
Num = 8
Num = 9
Num = 10
Entering infinite loop...
Num = 20
Num = 19
Num = 18
Num = 17
Num = 16
Num = 15
Num = 14
Num = 13
Num = 12
Num = 11
Num = 10
Loop with optional statements...
Num = 20
Num = 21
Num = 22
Num = 23
Num = 24
Num = 25
Num = 26
Num = 27
Num = 28
Num = 29
Num = 30
Loops with boolean operator...
Num = 30
Num = 31
Num = 32
Num = 33
Num = 34
Num = 35
Num = 36
Num = 37
Num = 38
Num = 39
Num = 40
Note
Go ships with one more variant know as "for - range", we will study it in the coming chapters.
Github
Just in case you have some errors with your code, you can check out the code at github repo
Golang Playground
You can also run the code at playground
Next
In the next chapter we will learn about {++ function++} declaration.