[Golang] for statement

2021-10-13 hit count image

Let's see how to use the for statement in Golang.

Outline

In this blog post, I will show you how to use the for statement in Golang. You can see the full source code of this blog post on the link below.

for statement

In Golang, there is only one loop statement, and that is the for statement. There is no loop statement like the while statement, but we can make it by using the for statement.

You can define the for statement like the below in Golang.

for INITIALIZATION_STATEMENT; CONDITION; AFTER_TRETMENT {
  // if the CONDITION is true, the CODE_BLOCK is executed.
  CODE_BLOCK
}

Also, you can omit the INITIALIZATION_STATEMENT like the below.

for ; CONDITION; AFTER_TRETMENT {
  ...
}

And, you can also omit the AFTER_TRETMENT like the below.

for INITIALIZATION_STATEMENT ; CONDITION;  {
  ...
}

Of course, you can omit both.

for ; CONDITION;  {
  ...
}

You can omit both more simply like the below.

for CONDITION {
  ...
}

To check these, create the main.go file and modify it like the below.

package main

import "fmt"

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

  i := 0
  for ; i < 10; i++ {
    fmt.Println(i)
  }

  for i := 0; i < 10; {
    fmt.Println(i)
    i++
  }

  i = 0
  for i < 10 {
    fmt.Println(i)
    i++
  }

  i = 0
  for i < 10 {
    fmt.Println(i)
    i++
  }
}

continue and break

In Golang, you can control the loop by using the continue and break.

  • continue: Go to AFTER_TRETMENT.
  • break: Exit the loop.

To check these, modify the main.go file like the below.

package main

import "fmt"

func main() {
  for i := 0; i < 10; i++ {
    if i == 3 {
      continue
    }
    if i == 6 {
      break
    }
    fmt.Println(i)
  }
}

When you execute the program, it goes to the AFTER_TREATMENT when i is 3, so i is not shown on the screen. Also, when i is 6, the loop is exited.

# go run main.go
0
1
2
4
5

Infinite loop

You can implement the infinite loop by using the for statement in Golang. When you omit the INITIALIZATION_STATEMENT, AFTER_TREATMENT, and set the CONDITION to true, the loop is executed forever.

for true {
  CODE_BLOCK
}

You can implement it more simply like the below.

for {
  CODE_BLOCK
}

To check the infinite loop, modify the main.go file like the below.

package main

import (
  "fmt"
  "time"
)

func main() {
  i := 1
  for {
    time.Sleep(time.Second)
    fmt.Println(i)
    i ++
  }
}

When you execute the code, you can see that the i is printed every second, and the program is not terminated.

# go run main.go
1
2
3
4
...

If the program is not terminated, you can use the ctrl + c to terminate the program.

Completed

Done! we’ve seen how to define and use the for statement in Golang. Golang just has one loop statement, so it’s simple to understand. Also, we’ve seen how to omit the INITIALIZATION_STATEMENT and AFTER_TREATMENT, and how to make the infinite loop.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts