Introduction
A conditional statement lets a program choose between different actions depending on whether some condition is true or false at the moment it runs. Without conditionals, a program could only execute the same fixed sequence of steps every time, regardless of circumstances, which would make it impossible to respond differently to different inputs, such as checking whether a user is old enough to proceed or whether a bank balance is sufficient for a withdrawal.
Cricket analogy: An umpire doesn't signal a boundary the same way every single delivery regardless of what happens; the signal depends on whether the ball actually crosses the rope, mirroring how a conditional only takes an action when its condition is true.
Explanation
Most languages express a basic conditional with an if statement, optionally followed by one or more else if branches for additional conditions, and a final else branch that runs only if none of the earlier conditions were true. The condition itself is an expression that evaluates to a boolean value, true or false, often built from comparison operators like equals or greater-than, combined using logical operators such as and, or, and not to express more complex rules.
Cricket analogy: A third umpire's review process checks the primary condition first, then a secondary condition if the first is inconclusive, and falls back to the on-field call if neither resolves it, mirroring an if, else if, else chain evaluated top to bottom.
Conditionals can be nested, meaning an if statement can contain another if statement inside its branch, which lets a program express rules that depend on more than one factor at once, such as checking both age and whether a form of ID was provided before allowing a purchase.
A frequent source of confusion for newcomers is the difference between assignment and comparison: a single equals sign typically assigns a value to a variable, while a double equals sign (or a dedicated comparison operator) checks whether two values are equal, and mixing these up can silently change a program's behavior instead of raising an obvious error. Another subtlety is that once one branch of an if or else if chain is taken, the remaining branches in that same chain are skipped entirely, even if their conditions would also have been true.
Cricket analogy: Confusing a coach's private note to a player with a public announcement to the whole team would cause chaos, similar to how confusing assignment with comparison can silently change a program's meaning instead of raising an obvious error.
Example
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
elif age >= 18:
print("Bring ID next time")
else:
print("Entry denied")Writing a single equals sign inside an if statement's condition, when a comparison was intended, is one of the most common beginner mistakes. In languages that allow it, this can silently assign a value instead of comparing one, causing the condition to evaluate in an unexpected way without any error message.
Summary
- A conditional statement lets a program choose between actions based on whether a condition is true or false.
- An if, else if, else chain evaluates conditions in order and runs only the first branch whose condition is true.
- Conditions are boolean expressions, often built from comparison and logical operators.
- Conditionals can be nested to express rules that depend on more than one factor at once.
- Confusing assignment with comparison, and forgetting that later branches are skipped once one matches, are common sources of bugs.
Practice what you learned
1. What is the primary purpose of a conditional statement in a program?
2. In an if, else if, else chain, what happens once one branch's condition is found to be true?
3. What type of value does a condition in an if statement ultimately evaluate to?
4. Why is confusing a single equals sign with a double equals sign a common source of bugs?
5. What does a nested conditional allow a program to express?
Was this page helpful?
You May Also Like
What Is Programming
An introduction to programming as the process of writing precise instructions that a computer can execute step by step to solve a problem.
Compiled vs Interpreted
A comparison of how compiled and interpreted languages turn source code into running instructions, and the tradeoffs each approach makes.
Types of Programming Languages
An overview of how programming languages are categorized by execution model, abstraction level, and paradigm, and why the categories matter.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics