codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

7 / 72 topics
1Introduction to Go (Golang)2Installation and Setup3Hello World Program4Variables and Constants5Data Types6Operators7Control Structures (if, else, switch)8Functions9Defer Statement10Panic and Recover
Tutorials/Go (Golang)/Control Structures
🐹Go (Golang)

Control Structures

Updated 2026-05-15
10 min read

Control Structures

Introduction

In programming, control structures are essential for managing the flow of execution in your code. They allow you to make decisions and repeat actions based on certain conditions. In this tutorial, we will explore three fundamental control structures in Go: if statements, switch cases, and loops (for).

Concept

If Statements

An if statement is used to execute a block of code only if a specified condition is true. It can also include an optional else clause to handle the case where the condition is false.

Syntax

if condition {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

### Switch Cases

A `switch` statement provides a more concise way to check multiple conditions. It compares a variable against several possible values and executes the code block corresponding to the first matching case.

#### Syntax
```go
switch expression {
case value1:
    // Code to execute if expression == value1
case value2:
    // Code to execute if expression == value2
default:
    // Code to execute if no cases match
}

### Loops

Go has a single looping construct, the `for` loop. It can be used in three different ways: basic for loop, while-like loop, and infinite loop.

#### Basic For Loop
```go
for initialization; condition; post {
    // Code to execute repeatedly
}

#### While-Like Loop
```go
for condition {
    // Code to execute repeatedly as long as condition is true
}

Infinite Loop

for {
    // Code to execute indefinitely until a break statement is encountered
}

Examples

If Statements

Here's an example of using an if statement to check if a number is positive, negative, or zero.

Go
1package main
2
3import "fmt"
4
5func main() {
6 num := 10
7
8 if num > 0 {
9 fmt.Println("Number is positive")
10 } else if num < 0 {
11 fmt.Println("Number is negative")
12 } else {
13 fmt.Println("Number is zero")
14 }
15}
Output
Number is positive

Switch Cases

In this example, we use a switch statement to determine the day of the week based on an integer input.

Go
1package main
2
3import "fmt"
4
5func main() {
6 day := 3
7
8 switch day {
9 case 1:
10 fmt.Println("Monday")
11 case 2:
12 fmt.Println("Tuesday")
13 case 3:
14 fmt.Println("Wednesday")
15 case 4:
16 fmt.Println("Thursday")
17 case 5:
18 fmt.Println("Friday")
19 case 6:
20 fmt.Println("Saturday")
21 case 7:
22 fmt.Println("Sunday")
23 default:
24 fmt.Println("Invalid day")
25 }
26}
Output
Wednesday

Loops

Let's look at different types of loops in Go.

Basic For Loop

This example prints numbers from 1 to 5 using a basic for loop.

Go
1package main
2
3import "fmt"
4
5func main() {
6 for i := 1; i <= 5; i++ {
7 fmt.Println(i)
8 }
9}
Output
1
2
3
4
5

While-Like Loop

This example uses a for loop to print numbers from 1 to 5, similar to a while loop in other languages.

Go
1package main
2
3import "fmt"
4
5func main() {
6 i := 1
7 for i <= 5 {
8 fmt.Println(i)
9 i++
10 }
11}
Output
1
2
3
4
5

Infinite Loop

This example demonstrates an infinite loop with a break statement to exit the loop.

Go
1package main
2
3import "fmt"
4
5func main() {
6 i := 0
7 for {
8 if i >= 5 {
9 break
10 }
11 fmt.Println(i)
12 i++
13 }
14}
Output
0
1
2
3
4

What's Next?

In the next section, we will delve deeper into if statements and explore more complex conditions and operators.


PreviousOperatorsNext Functions

Recommended Gear

OperatorsFunctions