Skip to content

Latest commit

 

History

History
143 lines (118 loc) · 3.91 KB

README.md

File metadata and controls

143 lines (118 loc) · 3.91 KB

Loops in Golang

Description

Loops in Golang are control structures that allow repetitive execution of a block of code until a certain condition is met. They are used to iterate over collections, perform a specific task a certain number of times, or continuously execute code until a specific condition is satisfied.

In Golang, there are three types of loops:

  1. For loop: The for loop is the most commonly used loop in Golang. It allows you to repeatedly execute a block of code based on a condition or a fixed number of iterations.
  2. While loop: Golang does not have a separate while loop construct. Instead, the for loop can be used as a while loop by omitting the initialization and post statements.
  3. Infinite loop: An infinite loop is a loop that keeps executing indefinitely until an explicit break or return statement is encountered.

Why Use Loops?

Loops allow us to efficiently execute a piece of code multiple times without having to duplicate it manually. They make our code more concise, readable, and maintainable. By using loops, we can handle large sets of data, perform computations, and implement various algorithms.

Use Cases of Loops with Examples

1. For Loop

The for loop is versatile and can be used in various scenarios. Here are a few examples:

Syntax:

for initialization; condition; post {
    // code to be executed
}
  • initialization initializes a counter variable or sets up the loop.
  • condition is evaluated before each iteration. If it's true, the loop continues; otherwise, it terminates.
  • post is executed after each iteration, usually used for incrementing or updating the counter.

Example 1: Iterate over a collection

numbers := []int{1, 2, 3, 4, 5}
for i := 0; i < len(numbers); i++ {
    fmt.Println(numbers[i])
}

Output:

1
2
3
4
5

Example 2: Calculate the sum of numbers

numbers := []int{1, 2, 3, 4, 5}
sum := 0
for _, num := range numbers {
    sum += num
}
fmt.Println("Sum:", sum)

Output:

Sum: 15

Example 3: Iterate over a range

// Print numbers from 1 to 5
for i := 1; i <= 5; i++ {
    fmt.Println(i)
}

Output:

1
2
3
4
5

2. While Loop

While Golang does not have a specific while loop, you can achieve the same functionality using a for loop with only a condition.

Example: Countdown timer

count := 10
for count > 0 {
    fmt.Println(count)
    count--
}
fmt.Println("Liftoff!")

Output:

10
9
8
7
6
5
4
3
2
1
Liftoff!

3. Infinite Loop

An infinite loop continues indefinitely until a break or return statement is encountered. It is often used when you want a program to keep running until a specific condition is met. If break or return statement is omitted, then the loop runs forever.

Example: User input until 'exit' is entered

for {
    var input string
    fmt.Print("Enter a command: ")
    fmt.Scanln(&input)
    if input == "exit" {
        break
    }
    fmt.Println("Executing command:", input)
}

Output:

Enter a command: run
Executing command: run
Enter a command: stop
Executing command: stop
Enter a command: exit

These examples demonstrate how loops can be used to solve various problems efficiently by repeating code execution or iterating over collections until a specific condition is met.

Additional Resources

Remember, loops are powerful tools that help us automate repetitive tasks and handle data efficiently. Understanding how to use loops effectively is crucial for writing clean and efficient code in Go.