Lesson 11
Control Structures
Covers if, if-else, else-if, nested if, switch.
Lesson content
What are control structures?
Control structures set execution order.
- Make decisions based on conditions
- Choose between multiple paths
- Execute different code blocks
Without them, code just runs top down.
Traffic light analogy. Green go, red stop, yellow slow.
Why do control structures exist?
Real apps react to input differently each time.
- ATM checks PIN correctness
- Grading system assigns grades by marks
- Shopping site applies discounts by order value
Real-world use cases
- Login and authentication systems
- Banking applications
- Online shopping carts
- Student grading systems
- Traffic management systems
- Inventory management software
- Hospital management systems
- Access control systems
Core concepts
Category | Purpose | Examples
---------- | ---------------------------- | -------------------------------------
Sequential | Executes statements in order | Default program flow
Selection | Chooses between alternatives | if, if-else, else-if, switch1. if statement
Runs code block only if condition is true.
if (condition)
{
// Statements
}2. if-else statement
Runs one block or another based on condition.
if (condition)
{
// True block
}
else
{
// False block
}3. else-if ladder
Checks conditions in order till one is true.
if (condition1)
{
// Statements
}
else if (condition2)
{
// Statements
}
else
{
// Statements
}4. Nested if
One if placed inside another if.
if (condition1)
{
if (condition2)
{
// Statements
}
}5. switch statement
Picks a block based on an expression's value.
switch (expression)
{
case value1:
// Statements
break;
case value2:
// Statements
break;
default:
// Statements
}break stops fall-through into next case.
Jump statement: break
Break exits switch. Continues after it.
Example 1: Using if
#include <iostream>
using namespace std;
int main()
{
int age = 20;
if (age >= 18)
{
cout << "Eligible to vote";
}
return 0;
}Example 2: Using if-else
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0)
{
cout << "Even Number";
}
else
{
cout << "Odd Number";
}
return 0;
}Example 3: Using switch
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Enter a number (1-3): ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Apple";
break;
case 2:
cout << "Banana";
break;
case 3:
cout << "Orange";
break;
default:
cout << "Invalid Choice";
}
return 0;
}Example 4: Using else-if ladder
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90)
{
cout << "Grade A";
}
else if (marks >= 75)
{
cout << "Grade B";
}
else if (marks >= 50)
{
cout << "Grade C";
}
else
{
cout << "Fail";
}
return 0;
}Example 5: Nested if
#include <iostream>
using namespace std;
int main()
{
int age;
bool hasLicense;
cout << "Enter age: ";
cin >> age;
cout << "Do you have a license? (1 = Yes, 0 = No): ";
cin >> hasLicense;
if (age >= 18)
{
if (hasLicense)
{
cout << "You can drive.";
}
else
{
cout << "You need a driving license.";
}
}
else
{
cout << "You are too young to drive.";
}
return 0;
}Common mistakes and pitfalls
- = instead of ==. Fix: use == for comparing
- Missing break in switch. Fix: add break each case
- Switch with ranges. Fix: use if-else instead
- Missing braces. Fix: always add {}
- Too many nested ifs. Fix: simplify or use functions
// Wrong
if (marks = 50)
{
cout << "Pass";
}// Correct
if (marks == 50)
{
cout << "Pass";
}Best practices
- Pick the simplest structure that fits
- Use switch for fixed menus
- Use if-else for complex conditions
- Keep conditions simple and clear
- Always use braces, even for one line
- Avoid deep nested if statements
- Use meaningful variable names
When not to use this
- Switch for range-based checks
- Deep nested ifs, use helper functions
- Long if-else chains for object behavior
Summary
- Control structures set execution order
- Selection: if, if-else, else-if, nested if, switch
- Use if for simple conditions
- Use if-else for two alternatives
- Use else-if for multiple conditions
- Use nested if for dependent conditions
- Use switch for fixed choices
- break exits a switch case
Frequently asked questions
What are control structures in C++?
They control execution order and decisions.
What is the difference between if and switch?
If handles ranges and logic. Switch compares fixed values.
When should I use switch instead of if-else?
Use switch for one variable, many fixed values.
Can switch work with strings in C++?
No. Switch needs integral or enum types.
What is a nested if statement?
An if statement placed inside another if.