control statements in Golang

Control Statements in Go language

March 25th, 2026
1201
7:00 Minutes

Go control statements are basically programming constructs that determine the flow of execution in a program. These permit us to make decisions, loop via code and jump to particular points within a function. In this blog, we will discuss in depth Go control statements, what are control statements in Golang, why they're important, types of statements, best practices, what not to do and so much more. So let's start!

What are Control Statements in Golang?

So what exactly are Go control statements? They basically dictate how a program executes and involve conditional statements such as 'if', 'if-else', 'switch', looping statements (for'), loop control statements (break, continue) and error handling statements.

Go control statements are put to use for making decisions, repeat blocks of code, control loop behavior and handle errors. They give flexibility and structure to your programs.

Why are Control Statements Important for Flow Control?

Golang control statements are important for flow control as they dictate the sequence of code execution, permitting programs to make decisions, repeat actions and manage errors dynamically. Through constructs like 'if/else', 'switch' and 'for' loops, developers can make intelligent, adaptable and readable programs. These programs respond to particular conditions and user inputs, instead of executing in a stiff linear sequence. Let us take a look at the main reasons why Go control statements are important for flow control.

1. Making Decisions (Conditionals)

The 'if, else if, else and switch' statements let your program execute different blocks of code according to the condition, whether it is true or false. It permits branching logic, creating programs that are responsive to different kinds of data or states.

2. Iteration of Actions (Loops)

Here, the 'for' loop is Golang's primary loop construct, permitting a block of code to be executed various times without repeating the same instructions. It is important for tasks such as iteration over data, performing calculations repeatedly and applying algorithms.

3. Managing Errors and Exceptions

The 'if' statements are specially used for checking errors or exceptional situations and executing particular handling logic when they appear like checking if an error is not 'nil'.

4. Structure and Code Readability

The well-structured control statements enhance the clarity and organization of code. This makes it easier for other developers and your future self too, for understanding the program's logic and flow.

Read Also: Golang Tutorial- Learn Go Programming Language

Types of Statements

Go control statements have two major types of statements-

  • Conditional Statements

These are for making decisions like 'if', 'if-else' and 'switch'.

  • Looping statements

These are for repeated execution

  • Jump Statements

These statements are for altering the normal flow of the loops like 'break' and 'continue'.

Now, we are going to read about the types of statements and in detail and with examples to make things easier for you to understand.

Decision-Making/Conditional Statements

The conditional/decision-making statements control the flow through executing different blocks of code according to the condition, whether it is true or false.

if Statement

This statement executes a block of code only if its condition evaluates to 'true'. Here is an example to make it easier for you.

package main

import "fmt"

func main() {

num := 10

if num > 5 {

fmt.Println("Number is greater than 5")

}

}

Here is the Output:

Number is greater than 5

if-else Statement

This statement executes one block of code if the condition is 'true' and a different block if its false. Let's take a look at an example.

package main

import "fmt"

func main() {

num := 3

if num%2 == 0 {

fmt.Println("Even number")

} else {

fmt.Println("Odd number")

}

}

The output to the above example is:

Odd number

if-else if-else

This statement permits a sequence of checks, executing the first block whose condition is 'true', here is an example.

package main

import "fmt"

func main() {

marks := 72

if marks >= 90 {

fmt.Println("Grade: A")

} else if marks >= 75 {

fmt.Println("Grade: B")

} else {

fmt.Println("Grade: C")

}

}

The output of this example:

Grade: B

switch Statement

This statement evaluates an expression and executes a block of code related to the matching value. Take a look at the example.

package main

import "fmt"

func main() {

day := 3

switch day {

case 1:

fmt.Println("Monday")

case 2:

fmt.Println("Tuesday")

case 3:

fmt.Println("Wednesday")

default:

fmt.Println("Invalid day")

}

}

Here is the output:

Wednesday

Looping Statements

The looping statements permit us for the repetition of a block code various times.

for Loop

The main looping construct in Go is used for iterating a block of code a particular number of times, take a look at an example.

package main

import "fmt"

func main() {

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

fmt.Println("Count:", i)

}

}

The output:

Count: 1

Count: 2

Count: 3

Count: 4

Count: 5

For range Loop

It is a special form of the 'for' loop put to use for iterating over elements of a slice, map, array or string, here is an example given below.

package main

import "fmt"

func main() {

fruits := []string{"Apple", "Banana", "Cherry"}

for index, fruit := range fruits {

fmt.Println(index, fruit)

}

}

The Output:

0 Apple

1 Banana

2 Cherry

Branching Statements

These break statements are used for altering the flow of execution by transferring control from one part of the program to the other.

break

This terminates the nearest enclosing breakable control flow block like a loop or 'switch', here is an example.

package main

import "fmt"

func main() {

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

if i == 3 {

break

}

fmt.Println("Value:", i)

}

}

The output is:

Value: 1

Value: 2

Continue

This statement skips the rest of the current repetition of a loop and proceeds to the next iteration, let us take a look at an example.

package main

import "fmt"

func main() {

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

if i == 3 {

continue

}

fmt.Println("Value:", i)

}

}

The output is:

Value: 1

Value: 2

Value: 4

Value: 5

Goto

This statement lets us transfer control to a labeled statement in the same function. Whereas, in the present language, its use is usually discouraged because of its potential to make it less readable and tough to maintain code, take a look at its example.

package main

import "fmt"

func main() {

var num int = 1

START:

if num <= 3 {

fmt.Println("Number:", num)

num++

goto START

}

}

The output is:

Number: 1

Number: 2

Number: 3

Fallthrough

This is a unique Go statement which causes execution for continuing to the next 'case' in a 'switch' statement even without a match, let us take a look at an example.

package main

import "fmt"

func main() {

num := 2

switch num {

case 1:

fmt.Println("One")

case 2:

fmt.Println("Two")

fallthrough

case 3:

fmt.Println("Three")

default:

fmt.Println("Other")

}

}

Output:

Two

Three

Combining Control Statements

From time to time, you need to make use of decision making, looping and branching altogether for solving problems. Here in Golang, you can nest these control statements or make use of them in each other. We will go through an example on searching for an element in a Slice, where I will be combining a 'for loop', an 'if condition' and a 'break statement'.

package main

import "fmt"

func main() {

numbers := []int{2, 4, 6, 8, 10}

target := 6

found := false

for i, num := range numbers {

if num == target {

fmt.Printf("Found %d at index %d\n", target, i)

found = true

break

}

}

if !found {

fmt.Printf("%d not found in the slice\n", target)

}

}

How it Works:

  • Loop (for range) goes through each and every number in the slice.
  • Decision-making (if) checks if the number matches the target.
  • Branching (break) exits the loop once the number is found.
  • Another if outside the loop prints a message if the number wasn't found.

Here is the Output of the given example:

Found 6 at index 2

This example clearly shows how we can combine control statements to solve a real world problem such as searching data efficiently.

Read Also: Top 50+ Golang Interview Questions And Answers

Best Practices for Go Control Statements

The best practices for Go control statements highlight clarity, conciseness and neglecting unnecessary complexity. Here are some major best practices for Go control statements to keep in mind:

  • You must keep the code clear and concise and aim for the easiest and most readable way to express your logic. Do not write long, complicated 'if' conditions, break them into smaller checks for readability.
  • Should limit scope, meaning you should restrict the scope of variables as much as possible to improve the code's clarity and prevent unintended side effects.
  • You can consider 'switch' over multiple 'if-else' chains. As it makes code cleaner while checking multiple values.
  • Consider using 'for-range' for collections, as while looping over slices, arrays, maps or strings, 'for-range' is way more readable and safer than using indexes manually.
  • Handling errors is another one of the best practices, you can implement strong error handling, usually by returning errors and handling them as soon as possible.

Common Mistakes to Avoid in Go Control Statements

There are some common mistakes which can occur while using control statements in Golang. By avoiding these will lead you to more strong and readable code, so let us look at some common mistakes to avoid while using Go control statements.

  • Must not forget that 'switch' auto-breaks.
  • Should avoid creating infinite loops by mistake.
  • Avoid overusing 'goto', as it can make code messy and hard to follow.
  • Must avoid skipping the 'default' case in 'switch'.
  • Must avoid complicated 'if' conditions, as they are hard to read.

Wrapping Up

Go control statements are basically the building blocks for making decisions and handling the flow of a program. From easy 'if' checks to strong 'switch' cases and flexible 'for' loops, as they let us write clear and efficient logic. By obeying the best practices and avoiding common mistakes, you can keep your code clean, readable and easy to manage. Once you master these basics, you will also be prepared for tackling more advanced Go concepts such as concurrency and error handling.

FAQs- Control Statements in Golang

Q1. Does Go have a 'while' loop?

No, Go doesn't have a while keyword. But, you can use the 'for' loop to achieve the same behavior.

Q2. Do I need to write a break in Go switch cases?

No, in Go each case ends automatically. If you want to continue to the next case, you use the fallthrough keyword.

Q3.What's the best way to write clean control statements?

You must keep conditions simple, prefer switch for multiple cases, use for-range while looping collections and always handle the default case in switch.

Q4. Can control statements in Golang handle multiple conditions?

Yes. Using if-else if-else chains or switch statements, Golang can handle multiple conditions clearly and execute code based on different scenarios.

Goto is supported but generally discouraged. It can jump to labels, but overusing it can make code hard to read and maintain.

Course Schedule

Course NameBatch TypeDetails
Golang Training
Every WeekdayView Details
Golang Training
Every WeekendView Details
About the Author
Piyush Verma | igmGuru
About the Author

Piyush is a technical writer skilled in Golang, R, C, C#, C++, Ruby, and ERP systems. He simplifies complex coding concepts into clear, beginner-friendly content, helping readers build strong foundations. With a structured approach, he supports both beginners and professionals in mastering technologies and advancing their careers.

Drop Us a Query
Fields marked * are mandatory

Programming Certification Courses

×

Your Shopping Cart


Your shopping cart is empty.