Learn Go: Golang Control Flow Statements: for loops #9

In this tutorial you will learn how to repeat a block of code execution using loops in Golang.

Gopher

Lets go!!

Published · Jan 15 2020


A loop is used to run a block of code repeatedly. Golang has only one looping statement - the for loop.

Following is the generic syntax of for loop in Go -
for initialization; condition; increment { // loop body }

The initialization statement is executed exactly once before the first iteration of the loop. In each iteration, the condition is checked. If the condition evaluates to true then the body of the loop is executed, otherwise, the loop terminates. The increment statement is executed at the end of every iteration.

Here is a simple example of a for loop -
package main import "fmt" func main() { for i := 0; i < 10; i++ { fmt.Printf("%d ", i) } }


Unlike other languages like C, C++, and Java, Go’s for loop doesn’t contain parentheses, and the curly braces are mandatory.

Note that, both initialization and increment statements in the for loop are optional and can be omitted
  • Omitting the initialization statement
package main

import "fmt"

func main() {
i := 2
for ; i <= 10; i += 2 {
fmt.Printf("%d ", i)
}
}
  • Omitting the increment statement
package main

import "fmt"

func main() {
i := 2
for i <= 20 {
fmt.Printf("%d ", i)
i *= 2
}
}


Note that, you can also omit the semicolons from the for loop in the above example and write it like this -
package main import "fmt" func main() { i := 2 for i <= 20 { fmt.Printf("%d ", i) i *= 2 } }


The above for loop is similar to a while loop in other languages. Go doesn’t have a while loop because we can easily represent a while loop using for.

Finally, You can also omit the condition from the for loop in Golang. This will give you an infinite loop -
package main func main() { // Infinite Loop for { } }

break statement
You can use break statement to break out of a loop before its normal termination. Here is an example -
package main

import "fmt"

func main() {
for num := 1; num <= 100; num++ {
if num%3 == 0 && num%5 == 0 {
fmt.Printf("First positive number divisible by both 3 and 5 is %d\n", num)
break
}
}
}


continue statement

The continue statement is used to stop running the loop body midway and continue to the next iteration of the loop.
package main import "fmt" func main() { for num := 1; num <= 10; num++ { if num%2 == 0 { continue; } fmt.Printf("%d ", num) } }
. . .

infinite loop

The syntax for creating an infinite loop is,
for { ...
}
The following program will keep printing Hello World continuously without terminating.
package main import "fmt" func main() { for { fmt.Println("Hello World") } }


If you try to run the above program in the go playground you will get error "process took too long". Please try running it in your local system to print "Hello World" infinitely.

. . .

Conclusion

That’s all folks. In this article, you learned how to work with control flow statements like for loop in Golang. In the next article, You’ll learn how to define functions in Go. See you in the next post!
. . .

. . .

On a mission to build Next-Gen Community Platform for Developers