Breaking loops with labels

Report a typo

Below is a Go program with three nested for loops. Your task is to break the second nested loop when the condition if counter >= 2 is met inside the third loop.

To achieve this, determine the correct placement for the label and use a break statement followed by the label name to break the loop. You can choose any label name you prefer!

Remember to choose the right location for your label to ensure the second nested loop breaks as intended.

Sample Input 1:

First loop iteration,Second loop iteration,Third loop iteration

Sample Output 1:

First loop iteration
	Second loop iteration
		Third loop iteration
		Third loop iteration

Sample Input 2:

I'm blue,da ba dee,da ba di

Sample Output 2:

I'm blue
	da ba dee
		da ba di
		da ba di
Write a program in Go
package main

import (
"bufio"
"fmt"
"os"
"strings"
)

func main() {
// DO NOT delete or modify the variable declarations below!
counter := 0

scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()

input := strings.Split(scanner.Text(), ",")
msg1, msg2, msg3 := input[0], input[1], input[2]

// Determine the correct placement for the label and then use a `break` statement
// followed by the label name to break out of the second nested loop.
for {
fmt.Printf("%s\n", msg1)
for {
fmt.Printf("\t%s\n", msg2)
for {
fmt.Printf("\t\t%s\n", msg3)
counter++

if counter >= 2 {

}
}
}
return // DO NOT delete the `return` statement!
}
___

Create a free account to access the full topic