Condition (If Statement)
A condition (if statement) is a control structure that executes code only if a specified condition is true. It allows a program to make decisions.
Why conditions are important
Programs often need to choose between different actions.
For example:
- If the user is logged in → show dashboard
- If the password is incorrect → show error
- If temperature is below 0 → display warning
Without conditions, programs would always execute the same instructions.
How an if statement works
A condition checks whether an expression is true or false.
Basic structure:
Example:
If the condition is true, the code inside runs.
If false, it is skipped.
If–Else Structure
Sometimes a program must choose between two options.
Example:
Else If
Used when checking multiple conditions.
This allows more complex decision-making.
Conditions and Boolean Values
Conditions always evaluate to a Boolean value:
- true
- false
Comparison and logical operators are commonly used inside conditions.
Example:
Why learning conditions matters
Conditions allow programs to:
- Make decisions
- React to user input
- Control logic
- Create dynamic behavior
Decision-making is a core part of programming.
Simple example
Think of a traffic light:
- If the light is green → go
- If the light is green → go
That is a real-life conditional structure.
Related terms
- What is Operator?
- What is Expression?
- What is Assignment?
Source
Simplified from general programming documentation and Wikipedia.