Learn 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 at a specific number of times. it is one more control structure type in 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 are 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.. Expression can be single or multiple. Multiple expression can be combined using logical operators -  &&,||,!
  • Increment/decrement is to change the variable value after 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 the all the above code is

0123456789  

Break statement 

the break is a keyword in golang. It is used to terminates 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 terminates the nested loop execution and the controller executes outside loop normal Syntax is

break;  

Here is an example of for loop with break statement In this example, Loop executes from 1 to 10 iterations, During 5th iteration, break keyword terminates from For loop execution, and executes the statements after 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 which is used to continue the next loop execution after stop the current execution here is syntax

continue;  

Here is an example of for loop with continue 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 go language. it is used to jump the iteration to the label statements without checking condition. goto is not recommended to use in any language. The syntax is as follows

goto LabelName;  
  
LabelName: code;  

Here is an example for goto label statement of for loop 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  

Similar Posts