Go from Beginner to Expert: A Complete Guide to Learn Golang PART-7 Step-by-Step Guide to understand Control Statements (break and continue) of Golang

Go from Beginner to Expert: A Complete Guide to Learn Golang PART-7

Step-by-Step Guide to understand Control Statements (break and continue) of Golang

Md. Faiyaj Zaman
3 min readJan 10, 2023

--

Welcome back to this tutorial on control statements in Golang. In the previous sections, we discussed if-else statements, switch statements, and for loops. In this section, we will be discussing break and continue statements in Golang.

To understand Control Statements(for loops) of Golang, please check the previous post here.

Break and continue statements

Break and continue statements are used to control the flow of execution within loops. The break statement is used to exit a loop early and the continue statement is used to skip an iteration of a loop.

1Syntax and examples of using break statement

Here is an example of using the break statement in a for loop:

package main

import "fmt"

func main() {

for i := 0; i < 5; i++ {

if i == 3 {

break

}

fmt.Println(i)

}

}

In this example, the for loop is iterating from 0 to 4. The if statement inside the loop checks if the current value of i is equal to 3. If it is, the break statement is executed and the loop is exited early. As a result, the value 3 is not printed by the loop.

2Syntax and examples of using continue statement

Here is an example of using the continue statement in a for loop:

package main

import "fmt"

func main() {

for i := 0; i < 5; i++ {

if i == 3 {

continue

}

fmt.Println(i)

}

}

In this example, the for loop is iterating from 0 to 4. The if statement inside the loop checks if the current value of i is equal to 3. If it is, the continue statement is executed and the current iteration is skipped. As a result, the value 3 is not printed by the loop.

It’s important to keep in mind that a break statement when called inside a nested loop, it only breaks the current loop it is in, whereas a continue statement will skip the current iteration of the innermost loop it is in.

In Conclusion, in this tutorial we’ve seen how to use control statements to control the flow of your code execution, including if-else statements, switch statements, for loops, and break and continue statements.

Each of these control statements serves a specific purpose and can be used in various ways to achieve different outcomes in your code. With a good understanding of these control statements, you can write better, more efficient code in Golang.

As always, practice is the key to mastering control statements. Try experimenting with different control statements and see how they can be used to solve different problems.

With time and practice, you’ll become proficient in using control statements in Golang and be able to write better code.

We will be discussing on Functions in Golang in the next section.

To stay up-to-date on our latest posts, please click on the Follow button and subscribe to our email notifications.

Kindly explore the complete series on Golang, which presents a step-by-step guide, available here.

Happy Coding :)

--

--