Conditional & Iterative Statements in C++: Loops & Switch-Case
1. What are Control Flow Statements? (In Simple Words)
Normally, a C++ program runs line-by-line from top to bottom.
But in real programs, you often need to make decisions (like “If marks >= 40, Print PASS, else FAIL”) or repeat actions (like “Print roll numbers from 1 to 60”).
To control the flow of execution, C++ provides three types of statements:
if, if-else, switch).for, while, do-while).break, continue, return).2. Decision-Making Statements (Conditionals)
A. Simple if and if-else
Used when you have two choices:
if (marks >= 40) {
cout << "Result: PASS" << endl;
} else {
cout << "Result: FAIL" << endl;
}
B. The else-if Ladder (Multiple Choices)
Used when you have many conditions to check in sequence:
if (percentage >= 75) {
cout << "Grade: Distinction" << endl;
} else if (percentage >= 60) {
cout << "Grade: First Division" << endl;
} else if (percentage >= 50) {
cout << "Grade: Second Division" << endl;
} else {
cout << "Grade: Third Division / Fail" << endl;
}
C. The switch-case Statement
When you have a menu with multiple options (like a calculator), using many if-else blocks gets messy. A switch statement makes your code clean and easy to read.
#include <iostream>
using namespace std;
int main() {
char op;
int a = 20, b = 10;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
switch (op) {
case '+':
cout << "Sum: " << (a + b) << endl;
break; // Stops and exits the switch
case '-':
cout << "Difference: " << (a - b) << endl;
break;
case '*':
cout << "Product: " << (a * b) << endl;
break;
case '/':
cout << "Quotient: " << (a / b) << endl;
break;
default:
cout << "Error: Invalid operator entered!" << endl;
}
return 0;
}
Why is
breakso important in switch?
If you forget to writebreak;, C++ doesn’t stop after the matching case! It continues executing the next cases below it automatically. This bug is known as fall-through.
Exam Tip: In C++,
switchonly works with integers (int) and characters (char). You cannot use floating-point numbers (float) or strings inswitch(x).
3. Iterative Statements (Loops in C++)
Instead of writing cout << "Hello" << endl; 100 times, you can put it inside a loop!
Loops in C++ are divided into two main categories:
•
while loop•
for loop•
do-while loop4. Comparison: while vs. do-while Loop
Look at this simple example to see the exact difference:
// 1. While Loop (Entry-Controlled)
int i = 10;
while (i < 5) { // Condition is FALSE right at the gate!
cout << "This will NEVER print" << endl;
i++;
}
// 2. Do-While Loop (Exit-Controlled)
int j = 10;
do {
cout << "This line prints ONCE!" << endl; // Runs FIRST before check!
j++;
} while (j < 5); // Condition is checked here at the exit door
| Feature | while Loop |
do-while Loop |
|---|---|---|
| Where is condition checked? | At the entry (beginning) of the loop | At the exit (end) of the loop |
| Minimum number of executions | 0 times (if condition is false initially) | At least 1 time (body runs before checking) |
| Trailing Semicolon | No semicolon at the end: while (cond) { } |
Must end with a semicolon: do { } while (cond); |
| Best Used For | General tasks where condition must be met first | Menu-driven programs (e.g., “Do you want to continue? (Y/N)”) |
5. Jump Statements: break vs. continue
Both break and continue are used inside loops to alter execution:
┌─── 'break' exits the entire loop immediately!
│
[ Loop Begins ] ──▶ [ Loop Body ] ──▶ [ Next Iteration ]
│
└─── 'continue' skips only the REST of this round!
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 4) {
continue; // Skips printing 4, jumps straight to i = 5
}
if (i == 8) {
break; // Stops the loop completely when i reaches 8!
}
cout << i << " ";
}
// Output: 1 2 3 5 6 7
return 0;
}
break;: Stops the entire loop immediately and jumps out to the next statement outside the loop.continue;: Skips only the remaining lines of the current iteration and jumps directly to the next loop cycle.
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- Differentiate between Entry-Controlled and Exit-Controlled loops with code examples.
Model Answer:- Entry-Controlled (
while,for): Condition is checked at the start. If false, executes 0 times. - Exit-Controlled (
do-while): Condition is checked at the end. Executes at least once even if the condition is false. - Provide the small 4-line code example for both.
- Entry-Controlled (
- Explain the difference between
breakandcontinuestatements.
Model Answer:breakexits the entire loop or switch statement immediately.continueskips only the current iteration and moves to the next turn of the loop.- Show a short
forloop example highlighting the output difference.
Frequently Asked 10-Mark Questions:
- Explain the
switch-casestatement in C++. What are its rules, advantages overif-else, and why isbreaknecessary? Write a menu-driven program for a basic calculator.
Model Answer:- Define
switchsyntax withcase,break, anddefault. - Mention key rules: only
intandcharexpressions are allowed; no duplicatecasevalues allowed. - Explain the concept of fall-through (what happens if
breakis omitted). - Write the full C++ calculator program for addition, subtraction, multiplication, and division.
- Define