Menu
Coddy logo textTech

Continuing a Specific Loop

Part of the Logic & Flow section of Coddy's GO journey — lesson 3 of 68.

Just as you can use labels with break to exit specific loops, Go also allows you to use labels with the continue statement. A labeled continue skips the current iteration of the specified loop and jumps to its next iteration.

This is particularly useful in nested loops when you want to skip the rest of the current iteration of an outer loop from within an inner loop, rather than just continuing the inner loop.

outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if i == 1 && j == 0 {
            continue outer
        }
        fmt.Printf("i=%d, j=%d\n", i, j)
    }
    fmt.Println("End of outer loop iteration")
}

In this example, when i == 1 && j == 0, the continue outer statement skips the rest of the outer loop's current iteration, including the inner loop and the "End of outer loop iteration" message. The program then moves directly to the next iteration of the outer loop where i becomes 2.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

In this challenge, you'll practice using labeled continue statements to control nested loop execution. You're building a data processing system that needs to skip certain outer loop iterations based on conditions found in inner loops.

You will receive two inputs:

  • A string representing the number of rows to process (e.g., "4")
  • A string representing a skip condition number (e.g., "3")

You are provided with the following 2D data array:

data := [][]int{
    {1, 2, 3, 4, 5},
    {6, 7, 8, 9, 10},
    {11, 12, 13, 14, 15},
    {16, 17, 18, 19, 20},
    {21, 22, 23, 24, 25}
}

Your task is to:

  1. Parse the number of rows from the first input to determine how many rows to process
  2. Parse the skip condition number from the second input
  3. Use nested loops with a labeled continue to process the data
  4. For each row, check each column value in that row
  5. If you find the skip condition number in any column of a row, use labeled continue to skip the rest of that row's processing and move to the next row
  6. For rows that don't contain the skip condition number, print "Processing row [row_number]: [value1] [value2] [value3] [value4] [value5]"
  7. For rows that do contain the skip condition number, print "Skipping row [row_number] due to condition"

The row numbers should be reported using 0-based indexing (first row is 0). Process only the number of rows specified in the first input, and only check the first 5 columns of each row.

Cheat sheet

Use labels with continue to skip the current iteration of a specific loop in nested loops:

outer:
for i := 0; i < 3; i++ {
    for j := 0; j < 3; j++ {
        if i == 1 && j == 0 {
            continue outer
        }
        fmt.Printf("i=%d, j=%d\n", i, j)
    }
    fmt.Println("End of outer loop iteration")
}

When continue outer is executed, it skips the rest of the outer loop's current iteration and jumps directly to the next iteration of the labeled loop.

Try it yourself

package main

import (
    "fmt"
    "strconv"
)

func main() {
    // Read input
    var rowsInput string
    var skipConditionInput string
    fmt.Scanln(&rowsInput)
    fmt.Scanln(&skipConditionInput)
    
    // Parse inputs
    numRows, _ := strconv.Atoi(rowsInput)
    skipCondition, _ := strconv.Atoi(skipConditionInput)
    
    // Provided 2D data array
    data := [][]int{
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20},
        {21, 22, 23, 24, 25},
    }
    
    // TODO: Write your code below
    // Use nested loops with labeled continue to process the data
    // Check each row for the skip condition number
    // Print appropriate messages based on whether the condition is found
    
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow