Easy Tutorial
❮ Home Go Goto Statement ❯

Go Language continue Statement

Go Language Loop Statements

The continue statement in Go is somewhat similar to the break statement. However, instead of exiting the loop, continue skips the current iteration and proceeds to the next iteration of the loop.

In a for loop, executing the continue statement triggers the execution of the for loop increment statement.

In nested loops, you can use a label to specify which loop to continue.

Syntax

The syntax for the continue statement is as follows:

continue;

The flowchart for the continue statement is as follows:

Example

Skip the current iteration when the variable a equals 15 and proceed to the next iteration:

Example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 10

   /* for loop */
   for a < 20 {
      if a == 15 {
         /* Skip this iteration */
         a = a + 1;
         continue;
      }
      fmt.Printf("The value of a is: %d\n", a);
      a++;      
   }   
}

The output of the above example is:

The value of a is: 10
The value of a is: 11
The value of a is: 12
The value of a is: 13
The value of a is: 14
The value of a is: 16
The value of a is: 17
The value of a is: 18
The value of a is: 19

The following example demonstrates nested loops and the use of labels with and without continue:

Example

package main

import "fmt"

func main() {

    // Without label
    fmt.Println("---- continue ---- ")
    for i := 1; i <= 3; i++ {
        fmt.Printf("i: %d\n", i)
            for i2 := 11; i2 <= 13; i2++ {
                fmt.Printf("i2: %d\n", i2)
                continue
            }
    }

    // With label
    fmt.Println("---- continue label ----")
    re:
        for i := 1; i <= 3; i++ {
            fmt.Printf("i: %d\n", i)
                for i2 := 11; i2 <= 13; i2++ {
                    fmt.Printf("i2: %d\n", i2)
                    continue re
                }
        }
}

The output of the above example is:

---- continue ---- 
i: 1
i2: 11
i2: 12
i2: 13
i: 2
i2: 11
i2: 12
i2: 13
i: 3
i2: 11
i2: 12
i2: 13
---- continue label ----
i: 1
i2: 11
i: 2
i2: 11
i: 3
i2: 11

Go Language Loop Statements

❮ Home Go Goto Statement ❯