Go Decision Making
In Go, decision-making is accomplished using conditional statements that allow you to control the flow of your program based on certain conditions. The main conditional statements in Go are:
1. if statement: The if
statement is used to execute a block of code if a specified condition is true.
if condition {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
2. else if statement: The else if
statement is used to specify additional conditions to be tested if the initial if
condition is false.
if condition1 {
// Code to be executed if condition1 is true
} else if condition2 {
// Code to be executed if condition2 is true
} else {
// Code to be executed if all conditions are false
}
3. switch statement: The switch
statement is used when you have multiple possible values or cases to be evaluated.
switch expression {
case value1:
// Code to be executed if expression equals value1
case value2:
// Code to be executed if expression equals value2
default:
// Code to be executed if expression does not match any case
}
4. Ternary Operator (Conditional Operator): Go doesn’t have a traditional ternary operator like some other languages, but you can achieve a similar effect using a short if-else statement.
variable = (condition) ? value1 : value2
// Equivalent to:
if condition {
variable = value1
} else {
variable = value2
}
Here’s an example that demonstrates the use of decision-making in Go:
package main
import "fmt"
func main() {
age := 25
// if statement
if age >= 18 {
fmt.Println("You are an adult.")
} else {
fmt.Println("You are a minor.")
}
// switch statement
day := "Monday"
switch day {
case "Monday":
fmt.Println("It's Monday.")
case "Tuesday":
fmt.Println("It's Tuesday.")
default:
fmt.Println("It's another day.")
}
// Ternary operator (short if-else)
result := ""
result = func() string {
if age >= 18 {
return "You are an adult."
}
return "You are a minor."
}()
fmt.Println(result)
}
Output :
You are an adult.
It's Monday.
You are an adult.
In this example, we use if
, switch
, and a short if-else statement to make decisions based on the value of the age
variable and the day
variable. These conditional statements help control the program’s behavior and allow it to respond to different situations based on the conditions evaluated.
Nested if Statement
In Go, a nested if
statement is an if
statement that is placed inside another if
statement. It allows you to perform more complex condition checking and decision-making by evaluating multiple conditions sequentially. The inner if
statement is executed only if the condition of the outer if
statement is true.
Here’s the syntax of a nested if
statement in Go:
if outerCondition {
// Code executed when outerCondition is true
if innerCondition {
// Code executed when both outerCondition and innerCondition are true
} else {
// Code executed when outerCondition is true but innerCondition is false
}
} else {
// Code executed when outerCondition is false
}
Example of a nested if
statement:
package main
import "fmt"
func main() {
age := 25
money := 200
if age >= 18 {
fmt.Println("You are an adult.")
if money > 100 {
fmt.Println("You can afford a movie ticket.")
} else {
fmt.Println("You cannot afford a movie ticket.")
}
} else {
fmt.Println("You are a minor.")
}
}
Output:
You are an adult.
You can afford a movie ticket.
In this example, we have a nested if
statement inside the outer if
statement. The outer condition checks if the age
is greater than or equal to 18. If it’s true, it prints “You are an adult.” Then, the inner condition checks if the money
is greater than 100. If the inner condition is true, it prints “You can afford a movie ticket.” Otherwise, it prints “You cannot afford a movie ticket.”
Nested if
statements are useful when you need to consider multiple conditions and perform different actions based on various combinations of those conditions. Just be mindful of code readability and indentation to avoid making the code too complex and hard to understand.
if..else..if ladder
In Go, an “if…else…if ladder” is a series of if
and else if
statements chained together to handle multiple conditions in a hierarchical manner. It allows you to evaluate and execute different blocks of code based on various conditions.
The syntax of an “if…else…if ladder” in Go is as follows:
if condition1 {
// Code executed if condition1 is true
} else if condition2 {
// Code executed if condition2 is true (and condition1 is false)
} else if condition3 {
// Code executed if condition3 is true (and both condition1 and condition2 are false)
} else {
// Code executed if none of the above conditions are true
}
Example of an “if…else…if ladder” in Go:
package main
import "fmt"
func main() {
score := 85
if score >= 90 {
fmt.Println("Your grade is A.")
} else if score >= 80 {
fmt.Println("Your grade is B.")
} else if score >= 70 {
fmt.Println("Your grade is C.")
} else if score >= 60 {
fmt.Println("Your grade is D.")
} else {
fmt.Println("Your grade is F.")
}
}
Output:
Your grade is B.
In this example, we use an “if…else…if ladder” to determine the grade based on the score
value. The program first checks if the score is greater than or equal to 90, and if true, it prints “Your grade is A.” If not, it moves to the next condition and checks if the score is greater than or equal to 80, and so on. The first condition that evaluates to true will execute its corresponding block of code, and the rest of the conditions will be skipped.
The “if…else…if ladder” is useful when you have multiple conditions with distinct outcomes, and you want to handle them in a specific order. It allows you to provide different responses based on the hierarchy of conditions, making it a powerful tool for decision-making in Go programs.