Skip to main content
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.

Understanding Control Flow

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 image is a flowchart determining eligibility based on age. If age is 18 or older, the result is "eligible"; otherwise, "not eligible."
The diagram emphasizes that control flow in programming involves branching based on evaluated conditions.

The If Statement

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.

Syntax

Example: Using an If Statement

In the example below, we declare a string variable and check if its value equals “happy”. If the condition is true, it prints the value:
Console output:
Since the condition is met, the program prints “happy”.

The If-Else Statement

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.

Correct Syntax

Incorrect Syntax Example

Avoid formatting the else block on a new line after the closing brace:
Console output:

Correct Example

By placing else on the same line as the closing brace, the program executes without errors:
Console output:
Remember to always place the else on the same line as the closing brace to prevent syntax errors in Go.

The Else-If Statement

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.

Structure

Example: Using If, Else-If, and Else

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:
A flowchart evaluating a fruit variable, leading to different statements based on whether the fruit is "apple" or "orange," or neither.
Here is the example code:
Console output:
Since the value “grapes” does not match “apple” or “orange”, the else block is executed.

Summary

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!

Watch Video

Practice Lab