←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Conditional & Iterative Statements in C++: Loops & Switch-Case

Study material and explanations for Unit 1

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:

Control Flow Categories in C++
1. Selection (Decision-Making)
Runs a specific block of code based on whether a condition is true or false (if, if-else, switch).
2. Iteration (Loops)
Repeats a block of code multiple times until a condition becomes false (for, while, do-while).
3. Jump Statements
Jumps directly to another part of the program (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 break so important in switch?
If you forget to write break;, 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++, switch only works with integers (int) and characters (char). You cannot use floating-point numbers (float) or strings in switch(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:

Loop Categorization by Condition Check Point
Entry-Controlled (Pre-Test) Loops
The condition is checked BEFORE entering the loop body. If the condition is false at the start, the loop runs 0 times.
• while loop
• for loop
Exit-Controlled (Post-Test) Loops
The condition is checked AFTER the loop body runs. Even if the condition is false, the loop runs at least once!
• do-while loop

4. 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:

  1. 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.
  2. Explain the difference between break and continue statements.
    Model Answer:
    • break exits the entire loop or switch statement immediately.
    • continue skips only the current iteration and moves to the next turn of the loop.
    • Show a short for loop example highlighting the output difference.

Frequently Asked 10-Mark Questions:

  1. Explain the switch-case statement in C++. What are its rules, advantages over if-else, and why is break necessary? Write a menu-driven program for a basic calculator.
    Model Answer:
    • Define switch syntax with case, break, and default.
    • Mention key rules: only int and char expressions are allowed; no duplicate case values allowed.
    • Explain the concept of fall-through (what happens if break is omitted).
    • Write the full C++ calculator program for addition, subtraction, multiplication, and division.