Golang For Loop beginner guide with examples

This blog post covers the For Loop with a break, continue, goto keywords with examples in Go Language.

Golang For loop example

For loop is used to execute the code block a specific number of times. it is one more control structure type in the go language. Following is a syntax of the for loop.

for initialization; conditional Boolean expression; increment|decrement {
 // Code block
}

Following are notes for loop syntax

  • Initialization variables that can be declared and initialized with a starting value.
  • The conditional Boolean expression is that results true or false always, if it is true, a code block or one iteration executes. Otherwise, the controller will terminate from loop execution. These expressions are created using Comparing operators like ==, !=, =, etc. The expression can be single or multiple. Multiple expressions can be combined using logical operators - &&, ||,!
  • Increment/decrement is to change the variable value after the code block is executed. Parenthesis is not required for this loop.

Here is an example for Loop. This loop executes 10 times and prints each iteration variable value.

package main
import "fmt"
func main() {
 for i := 0; i < 10; i++ {
  fmt.Print(i)
 }

}

The output of the above code is

0123456789

Initialization and increment/decrement are not required of a for loop. Here is an example without an initialization variable

func main() {
 i := 0
 for ; i <= 10; i++ {
  fmt.Print(i)
 }
}

Here is an example without increment/decrement variable

func main() {
 i := 0
 for i <= 10 {
  fmt.Print(i)
  i++
 }
}

The output of all the above codes is

0123456789

Break statement in Golang

break is a keyword in golang. It is used to terminate the for loop execution, The Controller will be delegated to the next statement after for loop code.

This can also be used in a nested loop to terminate the nested loop execution and the controller executes outside loop normal Syntax is

break;

Here is an example of a for loop with a break statement.

In this example, the Loop executes from 1 to 10 iterations, During the 5th iteration, the break keyword terminates from For loop execution and executes the statements after the loop code.

func main() {
 i := 0
 for i <= 10 {
  fmt.Print(i)
  if i == 5 {
   break
  }
  i++
 }
 fmt.Print("End")
}

The output of the above program is

012345End

break keyword can also be used with label statements that break the termination Here is an example of break label statements

package main

import "fmt"

func main() {
LOOPSECTION:
 for i := 1; i <= 10; i++ {
  if i%2 == 0 {
   break LOOPSECTION
  }
  fmt.Print(i)
 }
 fmt.Print("End")

}

Output is

1End

Continue statement in Golang

Continue is a keyword that is used to continue the next loop execution after stopping the current execution.

Here is the syntax

continue;

Here is an example of for loop with continued keyword usage

func main() {
 for i := 1; i <= 10; i++ {
  if i%2 != 0 {
   continue
  }
  fmt.Print(i)
 }
 fmt.Print("End")

}

Output the code is

246810End

The continue keyword can also be used with label statements that jump the execution from the current execution

func main() {
LOOPSECTION:
 for i := 1; i <= 10; i++ {
  if i%2 == 0 {
   continue LOOPSECTION
  }
  fmt.Print(i)
 }
 fmt.Print("End")
}

Output is

13579End

goto label Statement

goto is a keyword in the go language. it is used to jump the iteration to the label statements without checking conditions. goto is not recommended for use in any language.

The syntax is as follows

goto LabelName;

LabelName: code;

Here is an example for the goto label statement of for loop iterates from 1 to 10, When 5 iteration is encountered, It skips the 5th value to console.

func main() {
 i := 1
LOOPSECTION:
 for ; i <= 10; i++ {
  if i == 5 {
   i++
   goto LOOPSECTION
  }
  fmt.Print(i)
 }
 fmt.Print("End")

}

The output of the above code is

1234678910End