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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyIn 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:
- Parse the number of rows from the first input to determine how many rows to process
- Parse the skip condition number from the second input
- Use nested loops with a labeled
continueto process the data - For each row, check each column value in that row
- If you find the skip condition number in any column of a row, use labeled
continueto skip the rest of that row's processing and move to the next row - For rows that don't contain the skip condition number, print
"Processing row [row_number]: [value1] [value2] [value3] [value4] [value5]" - 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
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors