Go Tutorials

Go Programs

Go Continue Statement


Go Continue Statement

In this tutorial, we will learn about the continue statement in Go. We will cover the basics of using the continue statement to skip the current iteration of a loop and proceed with the next iteration.


What is a Continue Statement

A continue statement is used to skip the current iteration of a loop and proceed with the next iteration. When a continue statement is encountered, the remaining code inside the loop for the current iteration is skipped, and the loop continues with the next iteration.


Syntax

The syntax for the continue statement in Go is:

continue

The continue statement can be used in for, range, and switch statements to skip the current iteration and proceed with the next iteration.



Skipping Even Numbers in a For Loop

  1. Use a for loop to iterate from 1 to 10.
  2. Inside the loop, use an if statement to check if the current iteration is even.
  3. If the condition is true, use a continue statement to skip the current iteration.

Go Program

package main
import "fmt"
func main() {
    for i := 1; i <= 10; i++ {
        if i % 2 == 0 {
            continue
        }
        fmt.Printf("%d ", i)
    }
}

Output

1 3 5 7 9 


Skipping Odd Numbers in a Range Loop

  1. Use a range loop to iterate over a slice of numbers from 1 to 10.
  2. Inside the loop, use an if statement to check if the current number is odd.
  3. If the condition is true, use a continue statement to skip the current iteration.

Go Program

package main
import "fmt"
func main() {
    numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    for _, num := range numbers {
        if num % 2 != 0 {
            continue
        }
        fmt.Printf("%d ", num)
    }
}

Output

2 4 6 8 10