Chapter 6 - `if`, `else`, and `else if` Statements
One of the most fundamental concepts in programming is decision-making, which allows programs to execute certain parts of the code based on specific conditions. This is where the constructs if
, else
, and else if
(sometimes called elif
in certain languages) come into play.
These constructs help programmers write code that responds dynamically to different inputs or situations. By setting conditions, you can control which code runs and when. Let's break them down in detail:
What Are Conditional Statements?
Conditional statements (if
, else
, and else if
) are decision-making tools in programming. They enable the program to evaluate expressions and execute specific blocks of code based on whether these expressions are true or false.
if
checks a condition. If it's true, the code inside the block executes.
else
provides a fallback when the if
condition is false.
else if
(or elif
) is used to handle multiple conditions, offering additional checks when the initial if
condition fails.
Below, we explore their individual uses and how to combine them for complex scenarios.
The if
Statement: Checking a Single Condition
The if
statement is the simplest form of a conditional structure. It checks a single condition, and if the condition is true, the block of code associated with the if
statement runs.
General Syntax for if
Statements
Python:
if condition:
# Code runs if the condition is true
PHP:
if ($condition) {
// Code runs if the condition is true
}
C++:
if (condition) {
// Code runs if the condition is true
}
Go:
if condition {
// Code runs if the condition is true
}
Zig:
if (condition) {
// Code runs if the condition is true
}
Example: Using if
to Make Decisions
Python:
x = 8
if x > 5:
print("x is greater than 5")
PHP:
$x = 8;
if ($x > 5) {
echo "x is greater than 5";
}
C++:
int x = 8;
if (x > 5) {
std::cout << "x is greater than 5" << std::endl;
}
Go:
package main
import "fmt"
func main() {
x := 8
if x > 5 {
fmt.Println("x is greater than 5")
}
}
Zig:
const std = @import("std");
pub fn main() void {
const x = 8;
if (x > 5) {
std.debug.print("x is greater than 5\n", .{});
}
}
Adding Fallback Logic with else
The else
statement provides a block of code that executes when the if
condition evaluates to false. This is useful for handling cases where the primary condition does not apply.
Syntax for Using else
Python:
if condition:
# Runs if true
else:
# Runs if false
PHP:
if ($condition) {
// Runs if true
} else {
// Runs if false
}
C++:
if (condition) {
// Runs if true
} else {
// Runs if false
}
Go:
if condition {
// Runs if true
} else {
// Runs if false
}
Zig:
if (condition) {
// Runs if true
} else {
// Runs if false
}
Example: Implementing else
for Alternative Outcomes
Python:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
PHP:
$x = 3;
if ($x > 5) {
echo "x is greater than 5";
} else {
echo "x is not greater than 5";
}
C++:
int x = 3;
if (x > 5) {
std::cout << "x is greater than 5" << std::endl;
} else {
std::cout << "x is not greater than 5" << std::endl;
}
Go:
package main
import "fmt"
func main() {
x := 3
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is not greater than 5")
}
}
Zig:
const std = @import("std");
pub fn main() void {
const x = 3;
if (x > 5) {
std.debug.print("x is greater than 5\n", .{});
} else {
std.debug.print("x is not greater than 5\n", .{});
}
}
Handling Multiple Scenarios with else if
When you need to evaluate multiple conditions, else if
(or elif
in Python) statements come into play. They allow you to check additional conditions in sequence.
Structure of else if
Across Languages
Python:
if condition1:
# Runs if condition1 is true
elif condition2:
# Runs if condition2 is true
else:
# Runs if none of the conditions are true
PHP:
if ($condition1) {
// Runs if condition1 is true
} elseif ($condition2) {
// Runs if condition2 is true
} else {
// Runs if none of the conditions are true
}
C++:
if (condition1) {
// Runs if condition1 is true
} else if (condition2) {
// Runs if condition2 is true
} else {
// Runs if none of the conditions are true
}
Go:
if condition1 {
// Runs if condition1 is true
} else if condition2 {
// Runs if condition2 is true
} else {
// Runs if none of the conditions are true
}
Zig:
if (condition1) {
// Runs if condition1 is true
} else if (condition2) {
// Runs if condition2 is true
} else {
// Runs if none of the conditions are true
}
Example: Using else if
for Sequential Conditions
Python:
x = 7
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but less than or equal to 10")
else:
print("x is 5 or less")
PHP:
$x = 7;
if ($x > 10) {
echo "x is greater than 10";
} elseif ($x > 5) {
echo "x is greater than 5 but less than or equal to 10";
} else {
echo "x is 5 or less";
}
C++:
int x = 7;
if (x > 10) {
std::cout << "x is greater than 10" << std::endl;
} else if (x > 5) {
std::cout << "x is greater than 5 but less than or equal to 10" << std::endl;
} else {
std::cout << "x is 5 or less" << std::endl;
}
Go:
package main
import "fmt"
func main() {
x := 7
if x > 10 {
fmt.Println("x is greater than 10")
} else if x > 5 {
fmt.Println("x is greater than 5 but less than or equal to 10")
} else {
fmt.Println("x is 5 or less")
}
}
Zig:
const std = @import("std");
pub fn main() void {
const x = 7;
if (x > 10) {
std.debug.print("x is greater than 10\n", .{});
} else if (x > 5) {
std.debug.print("x is greater than 5 but less than or equal to 10\n", .{});
} else {
std.debug.print("x is 5 or less\n", .{});
}
}
Best Practices for Writing Conditional Logic
Avoid Deep Nesting: Nested if
-else
structures can quickly become hard to read. Simplify logic by using functions or other constructs where possible.
Use Else Sparingly: If all possible conditions are covered by if
or else if
, you may not need an else
.
Logical Short-Circuiting: When using multiple conditions, prioritize the most likely or least expensive checks.
Readable Conditions: Write conditions that are easy to understand and maintain, even if they require breaking into multiple lines.
By mastering if
, else
, and else if
statements, you'll gain the tools to make your programs more dynamic and flexible. These constructs are crucial for creating logic that adapts to various inputs and scenarios.
Last modified: 26 January 2025