Module: Control Flow

If Statements

Go Programming: Control Flow - If Statements

If statements are fundamental building blocks for controlling the flow of execution in any programming language, and Go is no exception. They allow your program to make decisions based on conditions.

Basic if Statement

The simplest form of an if statement executes a block of code only if a specified condition is true.

package main

import "fmt"

func main() {
	number := 10

	if number > 5 {
		fmt.Println("Number is greater than 5")
	}

	fmt.Println("This line always executes")
}

Explanation:

  • if condition { ... }: The if keyword is followed by a boolean condition enclosed in parentheses (though parentheses are optional in Go). The code block within the curly braces {} is executed only if the condition evaluates to true.
  • number > 5: This is the condition being evaluated. It checks if the value of the number variable is greater than 5.
  • fmt.Println("Number is greater than 5"): This line is executed because number is indeed greater than 5.
  • fmt.Println("This line always executes"): This line is not part of the if block and will always be executed, regardless of the condition.

if-else Statement

The if-else statement provides an alternative block of code to execute if the if condition is false.

package main

import "fmt"

func main() {
	number := 3

	if number > 5 {
		fmt.Println("Number is greater than 5")
	} else {
		fmt.Println("Number is not greater than 5")
	}

	fmt.Println("This line always executes")
}

Explanation:

  • if condition { ... } else { ... }: The else keyword introduces a block of code that is executed only if the if condition is false.
  • In this example, number is 3, which is not greater than 5. Therefore, the code within the else block is executed.

if-else if-else Statement

You can chain multiple conditions together using else if statements. This allows you to check for a series of different conditions.

package main

import "fmt"

func main() {
	number := 5

	if number > 10 {
		fmt.Println("Number is greater than 10")
	} else if number > 5 {
		fmt.Println("Number is greater than 5 but not greater than 10")
	} else if number == 5 {
		fmt.Println("Number is equal to 5")
	} else {
		fmt.Println("Number is less than or equal to 5")
	}

	fmt.Println("This line always executes")
}

Explanation:

  • else if condition { ... }: The else if keyword introduces another condition to check if the previous if or else if conditions were false.
  • The conditions are evaluated in order. Once a condition is true, its corresponding block of code is executed, and the rest of the if-else if-else chain is skipped.
  • The final else block is optional and provides a default block of code to execute if none of the preceding conditions are true.

Short if Statement (Statement Before if)

Go allows a concise form of the if statement where the code to be executed is written before the if condition. This is often used for simple assignments or variable updates.

package main

import "fmt"

func main() {
	number := 7

	if number > 5 {
		fmt.Println("Number is greater than 5")
	}

	// Short if statement
	if x := number * 2; x > 10 {
		fmt.Println("x is greater than 10")
	} else {
		fmt.Println("x is not greater than 10")
	}

	// x is only accessible within the scope of the if statement.
	// fmt.Println(x) // This would cause a compile error.
}

Explanation:

  • if x := number * 2; condition { ... }: This declares a new variable x and assigns it the value of number * 2. The condition is then evaluated using the value of x.
  • The scope of the variable x is limited to the if statement. It cannot be accessed outside of the if block.
  • This form is useful for avoiding redundant calculations or variable declarations.

Important Considerations:

  • Boolean Conditions: The condition in an if statement must evaluate to a boolean value (true or false).
  • Curly Braces: While curly braces {} are optional for single-line if blocks, it's generally considered good practice to always include them for readability and to avoid potential errors when adding more code later.
  • Scope: Variables declared within an if statement (using the short if statement form) have a limited scope – they are only accessible within that if block.
  • No Switch Fallthrough: Unlike some other languages, Go does not have "fallthrough" behavior in if-else if chains. Once a condition is met, the remaining conditions are not evaluated.

These examples cover the core concepts of if statements in Go. Mastering these concepts is crucial for writing effective and flexible Go programs.