{

Golang tutorials - 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, break,continue,goto label keywords examples

Golang For loop

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 for loop.

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

following are notes of for loop syntax

  • initialization variables that can be declared and initialized with starting value.
  • the conditional Boolean expression is that results true or false always, if it is true, 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 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 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 code is

0123456789  

Break statement

the break is a keyword in golang. It is used to terminate the for loop execution, 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 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 which break the termination Here is an example 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

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  

continue keyword can also be used with label statements which 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 to 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  
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.