1.
Introduction
2.
What is a Loop?
3.
Break Statement
3.1.
Syntax
3.2.
Flow Diagram
3.3.
Example
4.
Break with Nested Loops
4.1.
Example
5.
Continue Statement
5.1.
Syntax
5.2.
Flow Chart
5.3.
Example
6.
Continue with Nested Loops
6.1.
Example
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

# Go Loop Control Statement

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

In this article, we will be covering the Go Loop Control Statement. Therefore before moving on to the main topic, we shall be covering what loops are. So loops are used when we want to do a particular action for a specific amount of time. A loop is a set of instructions that can be used to perform a repetitive task until a condition is reached.

## What is a Loop?

A loop is a set of instructions that can perform a repetitive task until a condition is reached. You can learn more from here Go loops.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Break Statement

In Go, the break statement is used to terminate the execution of the current loop. A break statement is always paired with a conditional if statement.

1. When a break statement is encountered inside a loop, the loop gets immediately terminated, and the program control resumes.
2. It can be used to terminate a program in a switch statement.

If you are using nested loops, the break statement will stop the execution of the innermost loop and begin the execution of the following line of code after the block.

### Syntax

``break;``

### Example

``````package main

import "fmt"

func main() {

var a int = 10

for a < 20 {
fmt.Printf("value of a: %d\n", a);
a++;
if a > 15 {

break;
}
}
} ``````

Output

``````value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15``````

In this example, we have used the break statement. We printed specific values, and after checking the condition, we exited the code.

## Break with Nested Loops

If you have a nested set of loops, you will need a break for each loop if required. The break statement will only halt the execution of the innermost loop.

### Example

``````package main

import "fmt"

func main() {

for i := 1; i <= 3; i++ {

for j := 1; j <= 3; j++ {

if i==2 {
break
}
fmt.Println("i=", i, "j=",j )
}
}
}``````

Output

``````i= 1 j= 1
i= 1 j= 2
i= 1 j= 3
i= 3 j= 1
i= 3 j= 2
i= 3 j= 3``````

In this example, we have used the break statement with nested loops. We exited the loop after reaching a specific condition.

## Continue Statement

In Go, the continue statement skips the current iteration of the loop. It transfers the control flow of the program to the next iteration.

### Syntax

``````for initialization; condition, update {
if condition {
continue
}
}``````

### Example

``````package main
import ("fmt")

func main() {
for i := 1 ; i <= 5 ; i++ {

if i == 3 {
continue
}

fmt.Println(i)
}
} ``````

Output

``````1
2
4
5``````

In this example, we used the continue statement to print some numbers. We checked the condition, and after doing it, we printed the output.

## Continue with Nested Loops

In Go, when the continue statement is used with nested loops, it skips the inner loop's current iteration.

### Example

``````package main
import ("fmt")

func main() {
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {

if j==2 {
continue
}

fmt.Println("i=", i, "j=",j )
}
}
}``````

Output

``````i= 1 j= 1
i= 1 j= 3
i= 2 j= 1
i= 2 j= 3
i= 3 j= 1
i= 3 j= 3``````

In this example, we used the nested loops with the continue statement. We checked the condition, and after we printed the output.

## FAQs

1. What is a control statement in a loop?
A loop consists of a loop's body and a control statement. The control statement consists of specific conditions that direct the body of the loop to execute.

2. What is the use of loop control in the Go language?
The Go language comes with the support of these types of loop control statements. These are break statement which is used to terminate the loop. The continued statement skips the current iteration and shifts to the next iteration.

3. What is the function of the goto statement?
The goto statement transfers the control to the labeled statement in the program. The label can be defined as the valid identifier and placed just before the statement from where the control is transferred.

## Key Takeaways

In this blog, we have covered the Go Loop Control Statement and its implementation in GoLang. We gave a brief introduction to the topic. We also explained the different loop control statements, their syntax, and the example.We hope that this blog has helped you enhance your knowledge regarding go loop control statements and if you would like to learn more, check out our articles on Go Loops. Do upvote our blog to help other ninjas grow. Happy Coding!â€ť

Live masterclass