Conditional Statements

Conditional Statements
Conditional statements are used to make decisions in a program. They allow the program to execute different blocks of code based on whether a condition is TRUE or FALSE. Mastering conditional statements is the first step to writing logical, intelligent, and dynamic programs.
What are Conditional Statements?
Conditional statements are used to control the flow of execution in a program. Based on the result of a condition (TRUE or FALSE), the program decides which block of code to execute and which to skip.
Every programming language supports conditional statements. They allow programs to be flexible, dynamic, and adaptive — capable of responding differently based on user input, data, or external events.
Real-Life Analogy
Conditional statements are like the decisions we make every day. "If it's raining, take an umbrella." "If the light is green, go." "If I have enough money, buy this book." Every decision is a conditional — checking a condition and choosing an action.
Key Features of Conditional Statements
Evaluate One or More Conditions
They check one or more conditions and determine whether they are TRUE or FALSE.
Execute Code When Condition is TRUE
When a condition evaluates to TRUE, the associated block of code is executed.
Skip Code When Condition is FALSE
When a condition is FALSE, the corresponding block is skipped or the alternative block runs.
Provide Decision-Making Capability
Conditional statements enable programs to think, decide, and respond intelligently.
Make Programs Dynamic and Intelligent
They add flexibility, adaptability, and real-world logic to programs.
Types of Conditional Statements
There are four main types of conditional statements used in programming. Each is used based on how many conditions need to be checked and how the decision flow is structured.
| Type | Description |
|---|---|
| if Statement | Executes a block only when condition is TRUE. |
| if...else Statement | Executes one block when TRUE and another when FALSE. |
| if...else if...else Ladder | Checks multiple conditions and executes the first TRUE block. |
| Nested if | An if inside another if for layered decisions. |
Flowcharts of Conditional Statements
Each type of conditional statement has its own flowchart representation. Understanding these visual flows makes it easier to write and debug code.
A. if Statement
Start
↓
Is Condition TRUE?
├── Yes → Execute Statements
└── No → Skip (Do Nothing)
↓
Stop
B. if...else Statement
Start
↓
Is Condition TRUE?
├── Yes → Execute if Block
└── No → Execute else Block
↓
Stop
C. if...else if...else Ladder
Start
↓
Is Condition 1 TRUE?
├── Yes → Block 1
└── No → Is Condition 2 TRUE?
├── Yes → Block 2
└── No → Is Condition n TRUE?
├── Yes → Block n
└── No → Else Block (Default)
↓
Stop
D. Nested if Statement
Start
↓
Is Condition 1 TRUE?
├── Yes → Is Condition 2 TRUE?
│ ├── Yes → Inner if Block
│ └── No → Else Part of Inner if
└── No → Else Part of Outer if
↓
Stop
if statement inside another if statement for
handling dependent conditions.
Example — Find Largest of Three Numbers
Let's understand how conditional statements work using a classic example — finding the largest of three numbers using Nested If.
Problem Statement
Given three numbers A, B, and C, find and print the largest number.
Algorithm
Step 1: Start
Step 2: Read three numbers A, B, C
Step 3: If A > B then
If A > C then print A
Else print C
Else
If B > C then print B
Else print C
Step 4: Stop
Step Process
Compare A and B
First, determine if A is greater than B to know which branch to enter.
If A > B then Compare A and C
If A is greater, compare A with C to see if A is also greater than C.
If A <= B then Compare B and C
If A is not the largest, compare B with C to check whether B or C is the largest.
Print the Largest Number
Based on the comparisons, print the largest of the three numbers.
Flow Summary
| Condition | Result |
|---|---|
| A > B and A > C | A is Largest |
| A > B and A <= C | C is Largest |
| A <= B and B > C | B is Largest |
| A <= B and B <= C | C is Largest |
Code Examples in Different Languages
The same logic implemented in three popular programming languages.
Example in C
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
printf("%d is Largest", a);
else
printf("%d is Largest", c);
}
else {
if (b > c)
printf("%d is Largest", b);
else
printf("%d is Largest", c);
}
return 0;
}
Example in Java
import java.util.*;
public class Largest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.print("Enter three numbers: ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if (a > b) {
if (a > c)
System.out.println(a + " is Largest");
else
System.out.println(c + " is Largest");
} else {
if (b > c)
System.out.println(b + " is Largest");
else
System.out.println(c + " is Largest");
}
}
}
Example in Python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b:
if a > c:
print(f"{a} is Largest")
else:
print(f"{c} is Largest")
else:
if b > c:
print(f"{b} is Largest")
else:
print(f"{c} is Largest")
Condition Results
| Condition | Meaning | Action |
|---|---|---|
| TRUE (Yes) | Condition satisfied | Execute the block |
| FALSE (No) | Condition not satisfied | Skip block (or else block) |
Real-Life Example — Traffic Signal
Traffic signals are a perfect real-life example of conditional statements in action.
if (light == "RED") {
printf("Stop the vehicle");
} else if (light == "YELLOW") {
printf("Get Ready");
} else if (light == "GREEN") {
printf("Go");
} else {
printf("Invalid Signal");
}
Common Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
Logical Operators
Multiple conditions can be combined using logical operators for more complex decisions.
| Operator | Meaning | Example |
|---|---|---|
| && (AND) | Both conditions TRUE | (a > 5 && b < 10) |
| || (OR) | At least one TRUE | (a == 5 || b == 10) |
| ! (NOT) | Reverses the condition | !(a > b) |
Comparison of Conditional Statements
| Type | Branches | Best For | Example Use |
|---|---|---|---|
| if | One | Single condition check | Check if user is logged in |
| if-else | Two | Two-way decision | Pass or Fail |
| if-else if-else Ladder | Many | Multi-way decisions | Grade calculation |
| Nested if | Dependent | Layered conditions | Find largest of three |
More Practical Examples
Example 1: Check Even or Odd (if-else)
if (num % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
Example 2: Grade Calculation (if-else if-else Ladder)
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")
Example 3: Login & Role Check (Nested if)
if (isLoggedIn) {
if (role == "admin") {
printf("Admin Panel");
} else {
printf("User Panel");
}
} else {
printf("Please Log In");
}
Example 4: Voting Eligibility (Simple if)
if age >= 18:
print("Eligible to vote")
Advantages of Conditional Statements
Advantages
- Enable decision-making in programs
- Make programs flexible and dynamic
- Support real-world logic and behavior
- Foundation for loops, functions, and algorithms
- Supported by all programming languages
- Improve program interactivity
- Enable input validation and error handling
Limitations
- Overuse can lead to complex code
- Deep nesting reduces readability
- Hard to debug with many branches
- Requires careful ordering in ladder
- Not always the most efficient (use switch for many discrete values)
Tips for Using Conditional Statements
Best Practices
- Write conditions from specific to general.
- Use correct comparison operators.
- Always handle all possible cases.
- Keep conditions simple and readable.
- Use proper indentation and braces.
- Avoid deep nesting; use else-if ladder or refactor.
- Test with various inputs and edge cases.
- Use meaningful variable names.
- Combine multiple conditions using logical operators.
- Prefer
switchfor many discrete value comparisons.
Common Mistakes to Avoid
Mistake 1: Using = instead of ==
- Wrong:
if (a = 5) - Correct:
if (a == 5) =assigns;==compares
Mistake 2: Missing else
- FALSE case unhandled
- Unexpected input causes bugs
- Always include default logic
Mistake 3: Wrong Order in Ladder
- General condition blocks specific ones
- Order from most specific to least
- Wrong order → wrong result
Mistake 4: Deep Nesting
- Hard to read and maintain
- Increases risk of bugs
- Refactor with functions or logical operators
Did You Know?
Interesting Fact
The if...else if...else structure is the foundation of Artificial Intelligence, Automation, and Smart Systems. Every intelligent decision — from ATM PIN verification to autonomous driving — begins with a simple conditional statement.
Frequently Asked Questions
Q1. What are conditional statements?
Conditional statements are used to execute different blocks of code based on whether a condition is TRUE or FALSE.
Q2. How many types of conditional statements are there?
The four main types are: if, if-else, if-else if-else Ladder, and Nested if.
Q3. What is the difference between if and if-else?
An if executes code only when the condition is TRUE.
if-else executes one block if TRUE and another if FALSE.
Q4. When should I use nested if?
Use nested if when a decision depends on the result of another decision, such as multi-level authentication or classification.
Q5. Which is better — if-else ladder or nested if?
Use else-if ladder for independent conditions and nested if for dependent conditions.
Q6. Which is faster — if-else or switch?
switch is generally faster for comparing a single variable to many discrete values. Use if-else for range checks or complex conditions.
Key Takeaways
- Conditional statements enable decision-making in programs.
- They execute different code based on conditions.
- Four types: if, if-else, if-else if-else ladder, nested if.
- Use comparison and logical operators to evaluate conditions.
- Order of conditions matters, especially in ladders.
- Avoid deep nesting for better readability.
- Every programming language supports conditional statements.
- Foundation of intelligent, dynamic programs.
Key Takeaway
The if...else if...else ladder helps a program to
evaluate multiple conditions in order and execute the first
TRUE block. Combined with if, if-else, and nested
if, they form the complete toolkit for making decisions in
any programming language. It is simple, powerful, and
essential for real-world problems.
Best of Luck! Practice more examples, think logically,
and code confidently!
Flowchart = Visual Thinking → Smart Solutions → Better
Results! 🚀
Home & Online Tuition
Learn from an experienced tutor with personalized guidance.
Available Locations
Expert Home & Online Tuition
Personalized one-to-one tuition that focuses on concept building, practical learning, problem-solving skills, and excellent academic performance. Suitable for school students looking for structured, interactive, and result-oriented learning.
Subjects We Teach
Why Choose Our Tuition?
✅ Concept-Based Learning
✅ Practical Examples
✅ Weekly Tests
✅ Doubt Solving Sessions
✅ Practice Worksheets
✅ MCQ & Assignments
✅ Exam Preparation
✅ Flexible Class Timings