Table of contents
Conditional statements are a fundamental part of programming based on logic. They allow programs to make decisions based on certain conditions being true or false.
Variations
The general variations of conditional statements are:
if - executes a block of code if a condition is true
else - executes a different block of code if the condition is false
else if (or elif) - checks another condition if the first if the condition is false
switch/case - selects one of many code blocks to execute based on different conditions
ternary operator - a short-hand if-else expression
Conditions are based on logical expressions that evaluate true or false. This could be comparisons, logical operations, or any expression that resolves to a boolean value.
Importance
The importance of conditional statements in structured programming is that they allow programs to:
Make decisions based on data and conditions
Have different execution paths based on those decisions
Handle exceptions and edge cases
Simplify complex logic into readable chunks
Without conditional statements, programs would be linear with no ability to branch or make choices. This would severely limit what programs could accomplish.
In summary, conditional statements are an essential part of almost all programming languages, based on logical expressions that evaluate true or false. They allow programs to have different execution paths based on conditions, which is key for structured programming.
if Statement
The if statement allows us to execute commands conditionally:
if condition
then
command1
command2
fi
condition can be:
A test
[ .. ]
An arithmetic expression
$((..))
A string comparison
==
or!=
Example:
if [ $AGE -gt 18 ]
then
echo "You are an adult"
fi
else Clause
We can add an else clause to execute an alternative:
if condition; then
command1
else
command2
fi
Example:
if [ $AGE -gt 18 ]; then
echo "You can vote"
else
echo "You cannot vote"
fi
elif Clause
We can have multiple conditions using elif (else if):
if condition1; then
command1
elif condition2; then
command2
elif condition3;
command3
else
command4
fi
Example:
if [ $AGE -lt 16 ]; then
echo "You cannot drive"
elif [ $AGE -ge 16 ] && [ $AGE -lt 18 ]; then
echo "You can drive with restrictions"
elif [ $AGE -ge 18 ]; then
echo "You can drive"
else
echo "Invalid age!"
fi
So in summary, if, else if (elif) and else clauses allow us to execute code conditionally in Bash scripts. Sometimes, an "if statement" is called a "selection statement", because enables you to select the branch you want to execute depending on the conditional value.
Script Example
Here is a Bash script example to display the day of the week using if statements:
#!/bin/bash
echo "Enter the number of the day (1-7): "
read day
if [ $day -eq 1 ]
then
echo "Monday"
elif [ $day -eq 2 ]
then
echo "Tuesday"
elif [ $day -eq 3 ]
then
echo "Wednesday"
elif [ $day -eq 4 ]
then
echo "Thursday"
elif [ $day -eq 5 ]
then
echo "Friday"
elif [ $day -eq 6 ]
then
echo "Saturday"
elif [ $day -eq 7 ]
then
echo "Sunday"
else
echo "Invalid day"
fi
Breaking it down:
We use
#!/bin/bash
to indicate this is a Bash scriptWe prompt the user to enter a number from 1 to 7 representing the day
We read the input into the
$day
variableWe then have a series of if/elif statements checking the value of
$day
For each day number, we print the corresponding day of the week
We have an else clause to handle invalid input
When run, the output will be:
Enter the number of the day (1-7):
3
Wednesday
This shows a basic Bash script using if/elif statements to implement logic based on user input. We can expand this to handle exceptions, ask for more input, etc.
Disclaim: This article was created with AI (Rix)