flow statement golang for if switch

Go is a statically typed, compiled programming language designed at Google. It is a popular language for web development and is known for its simplicity, performance, and reliability. In this article, we will be exploring Go’s built-in control structures: for, if, and switch.

If you’re new to Go programming, be sure to check out our guide on Go’s syntax and data types to get started.

The IF statement

golang if statement

The if statement is used to execute a block of code if a condition is true. If the condition is false, the code block will be skipped. Here is an example of an if statement that checks if a number is even or odd:

package main

import "fmt"

func main() {
  i := 5
  if i%2 == 0 {
      fmt.Println(i, "is even")
  } else {
      fmt.Println(i, "is odd")
  }
}

The output of this if statement will be:

5 is odd

An if statement can also include an optional else if clause, which allows for multiple conditions to be checked. For example:

package main

import "fmt"

func main() {
  i := 5
  if i < 0 {
      fmt.Println(i, "is negative")
  } else if i > 0 {
      fmt.Println(i, "is positive")
  } else {
      fmt.Println(i, "is zero")
  }
}

In this case, the output will be:

5 is positive

The FOR loop

The for loop is the most widely used repetition control structure in Go. It comes in three forms: the traditional form with an init statement, a condition, and a post statement, the form with no condition, and the form with a single condition. The for loop can also be used as a range loop to iterate over data structures like arrays and maps.

golang for loop statement

The traditional FOR loop

The traditional for loop is similar to the for loops found in other programming languages. It consists of three parts: an initialization statement, a condition, and an afterthought. The loop will continue to execute as long as the condition is true. Here is an example of a traditional for loop that counts from 0 to 9:

package main

import "fmt"

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

The output of this loop will be:

0
1
2
3
4
5
6
7
8
9

The FOR loop with no condition

A for loop can also be used without a condition. This will create an infinite loop. To exit an infinite loop, we can use the break keyword. Here is an example of an infinite loop that counts from 0 to 9, then breaks:

package main

import "fmt"

func main() {
  for {
      fmt.Println(i)
      i++
      if i == 10 {
          break
      }
  }
}

The output of this infinite loop will be the same as the previous example.

The FOR loop with a single condition

A for loop can also be used with a single condition. In this case, the loop will execute as long as the condition is true. Here is an example of a loop that counts from 0 to 9 using a single condition:

package main

import "fmt"

func main() {
  i := 0
  for i < 10 {
      fmt.Println(i)
      i++
  }
}

The output of this loop will again be the same as the previous examples.

The SWITCH statement

The switch statement is used to execute a block of code based on a value. It is similar to an `if` statement but often more concise and easier to read.

Here is an example of a switch statement that checks a variable x and prints a message based on its value:

package main

import "fmt"

func main() {
  x := 5
  switch x {
    case 1:
      fmt.Println("x is 1")
    case 2:
      fmt.Println("x is 2")
    case 3:
      fmt.Println("x is 3")
    default:
      fmt.Println("x is something else")
  }
}

In this case, the output will be:

x is 5

A switch statement can also include multiple values for a case. For example:

package main

import "fmt"

func main() {
  x := 5
  switch x {
  case 1, 3, 5:
      fmt.Println("x is odd")
  case 2, 4, 6:
      fmt.Println("x is even")
  default:
      fmt.Println("x is something else")
  }
}

The output in this case will be:

x is odd

A switch statement can also include an optional switch expression. This allows the switch statement to be used to evaluate more complex expressions. For example:

package main

import "fmt"

func main() {
 x := 5
 y := 10
 switch {
  case x > y:
      fmt.Println("x is greater than y")
  case x < y:
      fmt.Println("x is less than y")
  default:
      fmt.Println("x is equal to y")
  }
}

The output in this case will be:

x is less than y

That’s it for Go’s built-in control structures! With for loops, if statements, and switch statements, you should be able to control the flow of your Go programs effectively.

Conclusion

The for loop, if statement, and switch statement are essential control structures in Go. These structures allow you to control the flow of your program and manipulate data structures. The for loop is the most commonly used repetition control structure in Go and comes in different forms, including the traditional form with an init statement, a condition, and a post statement, the form with no condition, and the form with a single condition. The for loop can also be used as a range loop to iterate over arrays and maps. The if statement is used to make decisions in your program and can include an optional else if clause and an optional else clause. The switch statement allows you to perform different actions based on different conditions and can include an optional switch expression.

If you’re interested in learning more about Go programming, be sure to check out our guides on Golang arrays, slices, and maps, as well as working with functions in Go. These resources will provide you with a deeper understanding of how to work with data structures and functions in Go, enabling you to write more efficient and powerful code.

Similar Posts

Leave a Reply

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