OneCompiler

For Statement in Go Programming Language

330

For statement

for loop is used to execute a block of code given number of times based on a condition.

Syntax

for initialization ;condition;increment{
//code statements
}

You can execute this program live here https://onecompiler.com/go/3ut9uuzcf

Example


package main
import "fmt"

func main() {
  
  // printing 1 to 5 numbers
	 i := 1
	for ;i <= 5;i++{
	  fmt.Printf("%d\t",i)
	}
	
	//writing another way of loop to print 1 to 5 numbers
	fmt.Println()
	i = 1
	for i<=5{
	  fmt.Printf("%d\t",i)
	  i++
	}
	
	//break the for loop using break statement if i=10
	fmt.Println()
	for i := 6;; i++{
	  fmt.Printf("%d\t",i)
	  if(i == 10){
	    break
	  }
	}

Output

1	2	3	4	5	
1	2	3	4	5	
6	7	8	9	10

For Range Statement

for statement supports one more functionality that uses range keyword to iterate an array, slice, map, string, or channel.

You can execute this program live here https://onecompiler.com/go/3ut9vmgxs

Example


package main
import "fmt"

func main() {
  
  //create a map
  fruitVsColour := map[string]string{"Apple":"red","Grapes":"green","Banana":"yellow","Guva":"green"}
  //Iterate the map using "range" keyword
  fmt.Println("----Map Keys and its values-----")
  for index,element := range fruitVsColour {
    fmt.Println(index,":",element)
  }
  
  fmt.Println("----Map Keys -----")
  //display only keys
  for key := range fruitVsColour{
    fmt.Println(key)
  }
  fmt.Println("----Map Key related values -----")
  //display only values
  for _,value := range fruitVsColour{
    fmt.Println(value)
  }

}

Output

----Map Keys and its values-----
Apple : red
Grapes : green
Banana : yellow
Guva : green
----Map Keys -----
Grapes
Banana
Guva
Apple
----Map Key related values -----
green
red
green
yellow

For - Range loop over string

for loop iterate over each character of a string

You can execute this program live here https://onecompiler.com/go/3ut9wumer

Example


package main
import "fmt"

func main() {
  
  for range "OneCompiler"{
    fmt.Println("OneCompiler")
  }
	
}

Output

OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler
OneCompiler

Infinite For Loop

If we omit the condition in Golang `for' loop, that becomes an infinite loop.


package main
import "fmt"

func main() {
	for;;{
      fmt.Println("This is Infinite loop");
  }
}

break Statement

break statement is used to break out a loop before its normal termination.

You can execute this program live here https://onecompiler.com/go/3ut9xcjpy

Example


package main
import "fmt"

func main() {
  //the loop will break if i%2 value is zero
	for i := 1; i < 40; i++{
      if i%2 ==0 {
        fmt.Println(i,":is an even number")
        break
      }
  }
}

Output

2 : is an even number

continue Statement

continue statement is used to stop running the loop in the mid and continue to the next iteration.

You can execute this program live here https://onecompiler.com/go/3ut9xyde8

Example

package main
import "fmt"

func main() {
  //print only odd numbers
  fmt.Println("--Odd Numbers--")
  for i:=1;i<=10;i++{
    if i%2==0{
      continue
    }
    fmt.Printf("%d\t", i)
  }
	
}

Output

--Odd Numbers--
1	3	5	7	9