Go Conditional Statements
Conditional statements in Go allow your program to make decisions and execute different blocks of code based on specific conditions. They are a fundamental part of control flow and help create dynamic, responsive applications.
Understanding Conditions in Go
A condition is an expression that evaluates to either true or
false. Go uses standard comparison and logical operators to
build these expressions.
Comparison Operators
These operators are used to compare two values:
| Operator | Description | Example |
|---|---|---|
<
|
Less than |
a < b
|
<=
|
Less than or equal to |
a <= b
|
>
|
Greater than |
a > b
|
>=
|
Greater than or equal to |
a >= b
|
==
|
Equal to |
a == b
|
!=
|
Not equal to |
a != b
|
Logical Operators
Logical operators combine multiple conditions:
| Operator | Description | Example |
|---|---|---|
&&
|
Logical AND |
(a > b) && (b < c)
|
||
|
Logical OR |
(a > b) || (b < c)
|
!
|
Logical NOT |
!(a == b)
|
Types of Conditional Statements in Go
Go provides several ways to handle decision-making:
1. The if Statement
Use the if statement to execute a block of code only when a
condition is true.
2. The if...else Statement
Use else to define an alternative block of code when the
condition is false.
3. The if...else if...else Ladder
Use else if to check multiple conditions sequentially.
4. The switch Statement
The switch statement is useful when you need to select one of
many code blocks to execute.
Go if Statement
The if statement in Go is used to execute a block of code only
when a specified condition evaluates to true. It is one of the
most essential control flow structures and is widely used for
decision-making in programs.
Syntax of the if Statement
Key Rules:
-
The keyword must always be written in lowercase. Using
IforIFwill result in a compilation error. -
The condition must evaluate to a boolean value (
trueorfalse). -
Curly braces
{}are mandatory, even for a single line of code. -
Unlike some other languages, parentheses
()around the condition are not required.
Example 1: Using if with a Direct Condition
In this example, we check whether a number is positive:
Example 2: Using if with Variables
You can also compare variables inside an if statement:
Example 3: Using if with Expressions
The condition can be a more complex expression involving calculations:
Go else Statement
The else statement in Go is used alongside the
if statement to define an alternative block of code that
executes when the specified condition evaluates to false. It
ensures your program can handle both outcomes of a decision effectively.
Syntax of if...else
Important Notes:
-
The
elseblock is optional but useful when you need a fallback action. -
The condition must return a boolean value (
trueorfalse). -
Parentheses
()around the condition are not required in Go. -
Curly braces
{}are mandatory for bothifandelseblocks.
Example 1: Checking Time of Day
Explanation: Since the value of hour is 21,
the condition hour < 18 evaluates to false.
Therefore, the else block is executed.
Example 2: Comparing Temperature
Explanation: The condition
temperature >= 25 is false, so the program
executes the else block.
Example 3: Even or Odd Number Check
Go else if Statement
The else if statement in Go allows you to evaluate multiple
conditions sequentially. It is used when you need to test additional
conditions after the initial if condition evaluates to
false.
Syntax of if...else if...else
How It Works:
- Conditions are evaluated from top to bottom.
-
As soon as a condition evaluates to
true, its corresponding block executes. - Once a match is found, the remaining conditions are not evaluated.
-
The
elseblock is optional and acts as a fallback.
Example 1: Greeting Based on Time
Example 2: Comparing Two Numbers
Example 3: Order of Conditions Matters
Explanation: Even though value >= 20 is
also true, the first condition (value >= 10) is matched
first. Therefore, only "Value is at least 10" is printed.
The Nested if Statement
You can have if statements inside other
if statements. This is called a nested if.
