Conditional Statements and Loops
Learn how to make decisions and repetitions in Go.
Conditional statements
Conditional statements perform various actions based on specific conditions. The developer specifies one or more conditions that the program executes. Particular statements are returned based on the outcome. Go uses logical and comparison operators to create conditions for different decisions. We’re going to explore the following:
- The
ifstatement - The
if elsestatement - The
else ifstatement - The nested
ifstatement - The
switchstatement
The if statement
The if statement executes a code block only if the specified condition is true.
The if else statement
The if else statement allows us to run one block of code if the specified condition is true and another block of code if it’s false.
The else if statement
The else if statement allows us to combine multiple if else statements.
The nested if
The nested if statement is an if statement within another if statement.
Here’s what’s happening in the code above:
The first
if...