Conditional Statements
Learn how to use conditional statements in Go, including if, else if, and switch.
Conditional statements are essential for controlling the flow of a program based on certain conditions. In Go, you can use if, else if, and switch statements to implement conditional logic.
If Statement
The simplest form of a conditional statement is the if statement. It allows you to execute a block of code if a specified condition is true.
Example 1: Basic If Statement
x := 10
if x > 5 {
fmt.Println("x is greater than 5")
}This will print:
x is greater than 5
Else If Statement
You can chain multiple conditions using else if. This allows you to check additional conditions if the previous ones are false.
Example 2: Else If Statement
x := 3
if x > 5 {
fmt.Println("x is greater than 5")
} else if x > 2 {
fmt.Println("x is greater than 2 but less than or equal to 5")
}This will print:
x is greater than 2 but less than or equal to 5
Else Statement
The else statement can be used to execute a block of code when all previous conditions are false.
Example 3: Else Statement
x := 1
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is less than or equal to 5")
}This will print:
x is less than or equal to 5
Switch Statement
The switch statement provides an alternative way to handle multiple conditions. It evaluates an expression and executes the corresponding case block.
Example 4: Switch Statement
year := "2024"
switch year {
case "2023":
fmt.Println("Year is 2023")
default:
fmt.Println("Year is not recognized")
}This will print:
year is not recognized
Conclusion
Conditional statements are fundamental in programming, allowing you to control the flow of your program based on specific conditions. In Go, you can use if, else if, and switch statements to implement complex logic in a clean and readable manner. Understanding how to use these statements effectively is crucial for writing robust Go applications.