Selection Structure

Selection Structure
The Selection Structure allows a program to make decisions and execute different statements based on conditions. It is also called Decision Structure. It's the second essential control structure that makes programs intelligent, dynamic, and interactive.
Introduction
After the sequence structure (executing steps in order), the next fundamental control structure in programming is the Selection Structure. Also known as Decision Structure, it allows programs to choose different paths of execution based on conditions.
Selection structure is what makes programs smart — able to evaluate situations and respond differently based on inputs. Every real-world program from login systems to games uses selection structures at multiple points.
Real-Life Analogy
A selection structure is like standing at a fork in the road. You look at a signboard (condition), and based on what it says, you choose to go left or right. Similarly, a program checks a condition and chooses a path to execute.
What is Selection Structure?
Control Structure for Decision Making
Selection structure is a control structure that allows programs to make decisions during execution.
Chooses Different Paths
The program chooses different paths based on whether a condition evaluates to True or False.
Enables Smart, Dynamic Programs
Helps in writing smart and dynamic programs that adapt to different situations.
Key Point
How It Works
- A condition (test expression) is evaluated.
- If the condition is True, one block of code is executed.
- If the condition is False, another block of code is executed.
- It helps the program to behave differently for different situations.
Where is Selection Structure Used?
Comparing & Checking
- Comparing numbers
- Checking conditions
- Range validation
- Data filtering
Validation & Security
- Validating user input
- Authentication (login)
- Password verification
- Access control
Academic & Business
- Grade calculation
- Salary computation
- Discount calculation
- Tax computation
Interactive Programs
- Menu-driven programs
- Game logic
- User choices
- Conditional workflows
Types of Selection Structure
There are three main types of selection structures used in programming.
(A) IF Structure
IF Structure
Executes a block of code if the condition is True. If False, the block is skipped entirely.
Syntax
if (condition) {
// statements
}
Example
if (A > B) then
Print "A is Greater"
(B) IF-ELSE Structure
IF-ELSE Structure
Executes one block if the condition is True, and another block if the condition is False.
Syntax
if (condition) {
// True block
} else {
// False block
}
Example
if (A > B) then
Print "A is Greater"
else
Print "B is Greater"
(C) IF-ELSE IF Ladder
IF-ELSE IF Ladder
Checks multiple conditions in sequence. The first True condition's block is executed. If none match, the else block runs.
Syntax
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// block N
}
Example — Grade Calculation
if (marks >= 90) Grade = "A"
else if (marks >= 75) Grade = "B"
else if (marks >= 60) Grade = "C"
else Grade = "Fail"
Flowchart Representation (IF-ELSE Structure)
Here's the flowchart of an IF-ELSE structure that finds the larger of two numbers.
Start
↓
Input A, B
↓
Is A > B? (Diamond)
├── Yes (True) → Print "A is Greater"
└── No (False) → Print "B is Greater"
↓
Stop
Complete Example — Find the Largest of Two Numbers
A. Problem Statement
Take two numbers as input and display the largest number.
B. Algorithm (IF-ELSE)
Step 1: Start
Step 2: Read two numbers A and B
Step 3: If A > B then
Print "A is Greater"
Else
Print "B is Greater"
Step 4: Stop
C. Pseudocode
START
READ A, B
IF A > B THEN
PRINT "A is Greater"
ELSE
PRINT "B is Greater"
ENDIF
STOP
D. C Program
#include <stdio.h>
int main() {
int A, B;
printf("Enter two numbers: ");
scanf("%d %d", &A, &B);
if (A > B) {
printf("A is Greater");
} else {
printf("B is Greater");
}
return 0;
}
E. Example Run
Input: A = 15, B = 25
Condition: Is 15 > 25? → False
Execution: Else part is executed
Output: B is Greater
5 Characteristics of Selection Structure
| No. | Characteristic | Description |
|---|---|---|
| 1 | Decision Making | Makes decisions based on conditions |
| 2 | Conditional Flow | Flow changes according to True or False |
| 3 | Multiple Paths | Different paths for different outcomes |
| 4 | Flexibility | Handles different situations easily |
| 5 | Widely Used | Used in almost every real-world program |
More Practical Examples
Example 1: Check Even or Odd
n = int(input("Enter a number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Example 2: Check Voting Eligibility
if (age >= 18) {
printf("Eligible to Vote");
} else {
printf("Not Eligible");
}
Example 3: Grade Calculation (Ladder)
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")
Example 4: Simple Login System
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "1234":
print("Login Successful")
else:
print("Invalid credentials")
Example 5: Find Largest of Three Numbers (Nested IF)
if (A > B) {
if (A > C)
printf("A is Largest");
else
printf("C is Largest");
} else {
if (B > C)
printf("B is Largest");
else
printf("C is Largest");
}
Example 6: Menu-Driven Program
print("1. Add\n2. Subtract\n3. Multiply\n4. Divide")
choice = int(input("Enter choice: "))
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if choice == 1:
print("Sum =", a + b)
elif choice == 2:
print("Difference =", a - b)
elif choice == 3:
print("Product =", a * b)
elif choice == 4:
print("Quotient =", a / b)
else:
print("Invalid choice")
Comparison Operators Used in Conditions
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater or equal | a >= b |
| <= | Less or equal | a <= b |
Logical Operators for Complex Conditions
| Operator | Meaning | Example |
|---|---|---|
| && (AND) | Both conditions must be true | (a > 5 && b < 10) |
| || (OR) | At least one must be true | (a == 0 || b == 0) |
| ! (NOT) | Reverses the condition | !(a == b) |
Selection Structure Types Comparison
| Type | Best For | Branches | Complexity |
|---|---|---|---|
| IF | Single condition check | One (True only) | Simple |
| IF-ELSE | Two-way decision | Two (True/False) | Simple |
| IF-ELSE IF Ladder | Multiple conditions | Many | Medium |
| Nested IF | Dependent decisions | Layered | Higher |
| Switch-Case | Value matching | Many discrete values | Medium |
Did You Know?
Interesting Fact
Every complex program you see (software, games, apps) uses selection structure to make decisions at multiple places! From an ATM checking your PIN to a game deciding your score — decisions are everywhere in code.
Tips for Using Selection Structure
Best Practices
- Write clear and simple conditions.
- Use correct comparison operators (== not =).
- Always handle all possible cases, including edge cases.
- Use parentheses to clarify complex conditions.
- Use meaningful variable names for readability.
- Test with both True and False scenarios.
- Avoid deep nesting — refactor when possible.
- Use curly braces even for single statements.
- Consider switch-case for value matching.
- Combine conditions with logical operators.
- Comment complex decision logic.
- Prefer positive conditions over negative when possible.
Common Mistakes to Avoid
Mistake 1: Using = instead of ==
- Wrong:
if (a = 5) - Correct:
if (a == 5) - = assigns; == compares
Mistake 2: Missing else Block
- Only True case handled
- False case unaddressed
- Always plan for both outcomes
Mistake 3: Wrong Order in Ladder
- General condition first blocks specific ones
- Order matters
- Specific to general
Mistake 4: Deep Nesting
- Hard to read
- Prone to bugs
- Refactor using else-if
Mistake 5: Missing Braces
- Only first line runs
- Dangling else problem
- Always use { }
Mistake 6: No Testing
- Not testing edge cases
- Missing boundary bugs
- Test comprehensively
Frequently Asked Questions
Q1. What is a selection structure?
A selection structure is a control structure that allows a program to choose different paths of execution based on conditions. It is also called Decision Structure.
Q2. What are the types of selection structure?
The main types are: IF, IF-ELSE, and IF-ELSE IF Ladder. Nested IF and Switch-Case are also selection structures.
Q3. Why is selection structure important?
It allows programs to make decisions and behave differently based on inputs, making them intelligent, dynamic, and interactive.
Q4. What symbol represents selection in flowcharts?
The diamond symbol represents a decision or condition in flowcharts.
Q5. What is the difference between IF and IF-ELSE?
IF handles only the True case. IF-ELSE handles both True and False cases with different code blocks.
Q6. When should I use IF-ELSE IF Ladder?
Use it when you have multiple conditions to check in order — like grade calculation, discount tiers, or menu options.
Q7. Can I use logical operators in conditions?
Yes! Use && (AND), || (OR), and ! (NOT) to combine multiple conditions.
Q8. What is nested IF?
Nested IF is an IF statement placed inside another IF statement, used when a decision depends on the result of another decision.
Q9. How is switch-case different from IF-ELSE Ladder?
Switch-case checks a variable against discrete values. IF-ELSE Ladder handles ranges and complex conditions.
Q10. What operators are used in selection structure?
Comparison operators (==, !=, >, <, >=, <=) and logical operators (&&, ||, !) are commonly used in selection structures.
Key Takeaways
- Selection structure enables decision-making in programs.
- Also called Decision Structure.
- Three main types: IF, IF-ELSE, IF-ELSE IF Ladder.
- Uses conditions that evaluate to True or False.
- Represented by a diamond in flowcharts.
- Uses comparison and logical operators.
- Makes programs smart and interactive.
- Used in almost every real-world program.
- Foundation of intelligent, dynamic software.
- Combined with sequence and repetition for complex programs.
Key Takeaway
Selection structure helps the program to think and
decide. It makes programs smart and interactive by
enabling different actions based on different conditions. Master
selection structures and you're ready to build truly intelligent
programs.
⭐ UNDERSTAND THE ALGORITHM, MASTER THE PROGRAM! ⭐
Best of Luck! Practice more examples, think
logically, code confidently. You can do it! 😊
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