Go Part 4: Control Flow Statements In Go

GO PART 4: CONTROL FLOW STATEMENTS IN GO


Please Subscribe Youtube| Like Facebook | Follow Twitter

Control Flow Statements In Go

In this article, we will provide a detailed overview of control flow statements in Go and provide examples of how they are used in Go programming.

Control flow statements are a fundamental aspect of any programming language, and Go is no exception. In Go, there are several control flow statements that allow developers to control the flow of their code execution. In this article, we will explore the different types of control flow statements available in Go, including if-else statements, switch statements, for loops, and while loops (implemented using for loops).

If-Else Statement

The if-else statement is a common control flow statement that is used to execute different blocks of code based on a specified condition. In Go, the syntax for an if-else statement is as follows:

if condition {
    // execute this block of code if the condition is true
} else {
    // execute this block of code if the condition is false
}

In some cases, you may need to evaluate multiple conditions. In these cases, you can use the else if statement. It allows developers to specify multiple conditions and corresponding actions to be taken based on those conditions. Here is the syntax for multiple if-else statement:

if condition1 {
   // code to execute if condition1 is true
} else if condition2 {
   // code to execute if condition1 is false and condition2 is true
} else {
   // code to execute if both condition1 and condition2 are false
}

Here’s an example of an if-else statement in Go:

package main

import "fmt"

func main() {
   x := 20

   if x < 10 {
      fmt.Println("x is less than 10")
   } else if x < 20 {
      fmt.Println("x is between 10 and 20")
   } else {
      fmt.Println("x is greater than or equal to 20")
   }
}

Output

x is greater than or equal to 20

In this example, the variable x is compared to different values using if-else if-else statements. If x is less than 10, the first block of code is executed. If x is between 10 and 20 (but not equal to 20), the second block of code is executed. Finally, if x is greater than or equal to 20, the third block of code is executed.

In our case value of x is 20. Therefore “x is greater than or equal to 20” is printed.

Switch Statement

The switch statement is another common control flow statement that is used to execute different blocks of code based on a specified condition. In Go, the syntax for a switch statement is as follows:

switch expression {
case value1:
    // execute this block of code if expression is equal to value1
case value2:
    // execute this block of code if expression is equal to value2
default:
    // execute this block of code if expression is not equal to any of the specified values
}

Here, expression is the value that is being compared to the different cases, and value1 and value2 are the specific values that the expression is being compared against.

If expression matches any of the specified values, the block of code inside that case statement is executed. If none of the cases match, then the default block of code is executed.

It’s important to note that in Go, there is no need to explicitly add a break statement after each case block, as the switch statement automatically breaks after executing the corresponding block of code.

Here’s an example of a switch statement in Go:

package main

import "fmt"

func main() {
    day := "Wednesday"

    switch day {
    case "Monday":
        fmt.Println("Today is Monday")
    case "Tuesday":
        fmt.Println("Today is Tuesday")
    case "Wednesday":
        fmt.Println("Today is Wednesday")
    default:
        fmt.Println("Today is not Monday, Tuesday or Wednesday")
    }
}

Output

Today is Wednesday

In this code snippet, a string variable named day is initialized with the value “Wednesday”.

Then a switch statement is used to compare the value of day with different cases.

In the first case, the program checks whether day is equal to the string “Monday”. If it is, then the code inside that case is executed, which simply prints “Today is Monday” to the console.

In the second case, the program checks whether day is equal to the string “Tuesday”. If it is, then the code inside that case is executed, which prints “Today is Tuesday” to the console.

In the third case, the program checks whether day is equal to the string “Wednesday”. If it is, then the code inside that case is executed, which prints “Today is Wednesday” to the console.

If none of the cases match, the program executes the default block of code, which simply prints “Today is not Monday, Tuesday or Wednesday” to the console.

Since the value of day is “Wednesday”, the third case is matched and the message “Today is Wednesday” is printed to the console.

For Loop

The for loop is a common control flow statement that is used to repeat a block of code a specified number of times. In Go, the syntax for a for loop is as follows:

for initialization; condition; increment {
    // execute this block of code while the condition is true
}

The initialization statement is executed once at the beginning of the loop and is used to initialize any variables that will be used in the loop.

The condition statement is checked at the beginning of each iteration of the loop. If the condition is true, the loop will execute the block of code inside the curly braces.

The increment statement is executed at the end of each iteration of the loop and is used to modify the condition or any variables that are being used in the loop.

The loop will continue to execute until the condition becomes false. When the condition becomes false, the program will exit the loop and continue with the rest of the code outside of the loop.

Here’s an example of a for loop in Go:

package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Println(i)
    }
}

Output

1
2
3
4
5

The for loop in this program is used to print the numbers from 1 to 5 to the console.

The loop starts with the initialization statement i := 1, which sets the loop variable i to the value 1. Then, the loop condition i <= 5 is checked. As long as the condition is true, the loop body will execute.

In this case, the loop body contains the fmt.Println(i) statement, which prints the current value of i to the console. After executing the loop body, the loop increments the loop variable i by 1 with the i++ statement.

This process repeats until the loop condition i <= 5 is no longer true.

Break and Continue Statements Inside a For Loop

break and continue statements are also used inside loops to alter the flow of control.

The break statement is used to exit a loop prematurely. When encountered inside a loop, it immediately terminates the loop and control is passed to the statement following the loop.
For example, consider the following code snippet:

for i := 1; i <= 10; i++ {
    if i == 6 {
        break
    }
    fmt.Println(i)
}

Output

1
2
3
4
5

In this code, the loop will iterate from 1 to 10. When i equals 6, the break statement is executed, causing the loop to terminate prematurely.

The continue statement, on the other hand, is used to skip the current iteration of a loop and move to the next iteration.
For example, consider the following code snippet:

for i := 1; i <= 10; i++ {
    if i == 6 {
        continue
    }
    fmt.Println(i)
}

Output

1
2
3
4
5
7
8
9
10

In this code, the loop will iterate from 1 to 10. When i equals 6, the continue statement is executed, causing the loop to skip the current iteration and move to the next iteration.

While Loop (implemented using for loops)

In Go, while loops are not available as a separate loop structure like in some other programming languages such as C# or Java. However, the same behavior can be achieved using the for loop in Go.

Here is the syntax for a for loop that behaves like a while loop:

for condition {
    // code to be executed
}

In this syntax, the loop will execute the code block repeatedly as long as the condition is true. The loop will exit once the condition becomes false.

Here’s an example of a while loop using a for loop in Go:

package main

import "fmt"

func main() {
    i := 1
    for i <= 5 {
        fmt.Println(i)
        i++
    }
}

Output

1
2
3
4
5

In this example, the for loop will continue to execute as long as the i variable is less than or equal to 5. The fmt.Println(i) statement prints the value of i to the console, and the i++ statement increments the value of i by 1 in each iteration.

Conclusion

In conclusion, control flow statements are an essential part of any programming language, including Go. They enable developers to control the flow of execution in their programs, making them more efficient and effective. In this article, we have covered the different types of control flow statements in Go, including if-else statements, switch statements, for loops, and while loops (implemented using for loops).

Go Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *