This lesson covers control flow in Go using if-else and else-if statements to evaluate conditions and execute code accordingly.
This lesson covers control flow in programming with a focus on conditional statements in Go. We will use if-else and else-if statements to evaluate conditions and execute code accordingly. The examples in this guide validate conditions—such as checking if a user is eligible to vote based on age—and demonstrate proper syntax and structure.
Control flow determines the order in which instructions are executed. Instead of following a strict top-to-bottom order, the program’s execution flow is guided by conditions. Consider the following flowchart that illustrates the evaluation process for determining if a user is eligible to vote:
The diagram emphasizes that control flow in programming involves branching based on evaluated conditions.
The if statement is used to execute code only when a specified condition is true. If the condition is false, the code block is skipped. Note that parentheses around the condition are optional in Go.
The if-else statement extends the if statement by introducing an alternative block of code that executes when the condition is false. It is crucial to place the else statement on the same line as the closing brace of the if block to avoid syntax errors.
Avoid formatting the else block on a new line after the closing brace:
Copy
Ask AI
package mainimport "fmt"func main() { var fruit string = "grapes" if fruit == "apples" { fmt.Println("Fruit is apple") } else { fmt.Println("Fruit is not apple") }}
Console output:
Copy
Ask AI
>>> go run main.gosyntax error: unexpected else, expecting }
By placing else on the same line as the closing brace, the program executes without errors:
Copy
Ask AI
package mainimport "fmt"func main() { var fruit string = "grapes" if fruit == "apples" { fmt.Println("Fruit is apple") } else { fmt.Println("Fruit is not apple") }}
Console output:
Copy
Ask AI
>>> go run main.goFruit is not apple
Remember to always place the else on the same line as the closing brace to prevent syntax errors in Go.
When there are multiple conditions to evaluate, the else-if statement provides an elegant solution. It allows you to test additional conditions if the prior ones evaluate to false. Once a condition is met, the control flow exits the conditional statement.
if condition_1 { // Executes when condition_1 is true} else if condition_2 { // Executes when condition_1 is false and condition_2 is true} else if condition_3 { // Executes when previous conditions are false and condition_3 is true} else { // Executes when none of the above conditions are true}
In the following example, the program checks the value of a variable named “fruit”. Depending on the value, it prints a specific message. The corresponding flowchart visually represents this decision-making process:
Here is the example code:
Copy
Ask AI
package mainimport "fmt"func main() { fruit := "grapes" if fruit == "apple" { fmt.Println("I love apples") } else if fruit == "orange" { fmt.Println("Oranges are not apples") } else { fmt.Println("no appetite") }}
Console output:
Copy
Ask AI
>>> go run main.gono appetite
Since the value “grapes” does not match “apple” or “orange”, the else block is executed.
In this lesson, you learned how to implement control flow in Go using if, if-else, and else-if statements. These structures allow your programs to make decisions and execute specific code based on given conditions. Practice applying these techniques to build more dynamic and responsive applications.Keep exploring and practicing to improve your understanding of Go and control flow concepts. Happy coding!