Recap - Advanced Loop Control
Part of the Logic & Flow section of Coddy's GO journey — lesson 5 of 68.
Challenge
EasyIn this challenge, you'll combine labeled break statements with switch and fallthrough to create a security monitoring system. The system searches through multiple security zones for threats and categorizes them with cascading alert levels.
You will receive two inputs:
- A string representing the threat level to search for (e.g.,
"high") - A string representing the maximum zones to search (e.g.,
"3")
You are provided with the following security zones data:
zones := [][]string{
{"low", "medium", "low"},
{"medium", "high", "low"},
{"critical", "medium", "high"},
{"low", "critical", "medium"}
}Your task is to:
- Parse the threat level from the first input
- Parse the maximum zones to search from the second input
- Use nested loops with a labeled
breakto search through the zones - When you find the target threat level, print
"Threat found at zone [zone_number] position [position]"and immediately exit both loops using labeled break - After finding the threat (or if no threat is found), use a
switchstatement withfallthroughto display security alerts based on the threat level you were searching for
The switch statement should work as follows:
- For
"critical": Print "CRITICAL: Lockdown initiated!" and fall through to print the high alert as well - For
"high": Print "HIGH: Security breach detected!" and fall through to print the medium alert as well - For
"medium": Print "MEDIUM: Increased monitoring active" - For
"low": Print "LOW: Standard security protocols"
If the threat is not found after searching the specified number of zones, print "Threat not found in searched zones" before displaying the security alerts. Use 0-based indexing for zone numbers and positions within zones.
Try it yourself
package main
import (
"fmt"
"strconv"
)
func main() {
// Read input
var threatLevel string
var maxZonesStr string
fmt.Scanln(&threatLevel)
fmt.Scanln(&maxZonesStr)
// Convert max zones to integer
maxZones, _ := strconv.Atoi(maxZonesStr)
// Security zones data
zones := [][]string{
{"low", "medium", "low"},
{"medium", "high", "low"},
{"critical", "medium", "high"},
{"low", "critical", "medium"},
}
// TODO: Write your code below
// Use nested loops with labeled break to search for the threat
// Print threat location if found, or "Threat not found in searched zones" if not found
// Then use switch with fallthrough for security alerts
}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