Current Affairs updated daily
KalviGavaniKalviGavani

Flow of Control Exercise Solutions | KalviGavani

TN Class 11 Computer Science Flow of Control exercise solutions — if-else, switch, for, while, do-while with worked examples.

Updated 15 June 202614 min read

Exercise Solutions Available

Step-by-step answers for all textbook questions in this chapter

View Solutions →

1. How to Use This Page

1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.


2. Key Concepts (Quick Recap)

Types of Control Structures

TypeDescriptionExamples
SequentialDefault flow — top to bottomAll statements execute in order
SelectionChoose between alternativesif, if-else, switch
IterationRepeat a block of codefor, while, do-while

Selection Statements

Simple if:

if (condition) {
    // executes only if condition is true
}

if-else:

if (condition) {
    // executes if condition is true
} else {
    // executes if condition is false
}

if-else if-else (ladder):

if (marks >= 90) {
    grade = 'A';
} else if (marks >= 75) {
    grade = 'B';
} else if (marks >= 60) {
    grade = 'C';
} else {
    grade = 'D';
}

switch statement:

switch (day) {
    case 1: cout << "Monday"; break;
    case 2: cout << "Tuesday"; break;
    case 3: cout << "Wednesday"; break;
    default: cout << "Invalid";
}

Iteration Statements (Loops)

Featureforwhiledo-while
Entry/Exit controlledEntryEntryExit
Minimum executions001
Best forKnown iteration countUnknown count, pre-checkUnknown count, post-check
Syntaxfor(init; cond; update)while(cond)do { } while(cond);

for loop:

for (int i = 1; i <= 10; i++) {
    cout << i << " ";
}
// Output: 1 2 3 4 5 6 7 8 9 10

while loop:

int i = 1;
while (i <= 10) {
    cout << i << " ";
    i++;
}
// Output: 1 2 3 4 5 6 7 8 9 10

do-while loop:

int i = 1;
do {
    cout << i << " ";
    i++;
} while (i <= 10);
// Output: 1 2 3 4 5 6 7 8 9 10

Jump Statements

StatementPurpose
breakExits the nearest enclosing loop or switch
continueSkips the rest of the current loop iteration
gotoJumps to a labelled statement (avoid in practice)

3. I. Choose the Best Answer (MCQs)

1. Which loop is guaranteed to execute at least once?
Answer

(a) for (b) while (c) do-while (d) None of the above

Answer(c) do-while The do-while loop checks the condition after executing the body, so the loop body runs at least once even if the condition is false initially. for and while check the condition before executing.
2. What is the output of the following code?
for (int i = 0; i < 5; i++) {
    if (i == 3) continue;
    cout << i << " ";
}
Answer

(a) 0 1 2 3 4 (b) 0 1 2 4 (c) 0 1 2 (d) 0 1 2 3

Answer(b) 0 1 2 4 When i == 3, continue skips the rest of the loop body, so 3 is not printed. All other values (0, 1, 2, 4) are printed.
3. The break statement is used to:
Answer

(a) Skip an iteration (b) Exit the current loop or switch (c) Restart the loop (d) Jump to a label

Answer(b) Exit the current loop or switch break immediately terminates the nearest enclosing loop or switch block. continue is used to skip an iteration, not break.
4. In a switch statement, what happens if break is missing after a case?
Answer

(a) Compiler error (b) Only that case executes (c) Fall-through occurs — execution continues to the next case (d) The switch exits

Answer(c) Fall-through occurs Without break, execution "falls through" to subsequent cases until a break or the end of the switch is reached. This is a common source of bugs.
5. Which of the following is an entry-controlled loop?
Answer

(a) do-while (b) for (c) Both for and while (d) Only do-while

Answer(c) Both for and while Both for and while check the condition before entering the loop body (entry-controlled). The do-while loop checks the condition after executing the body (exit-controlled).
6. What will be the output?
int x = 10;
if (x > 5)
    if (x > 15)
        cout << "A";
    else
        cout << "B";
Answer

(a) A (b) B (c) No output (d) Error

Answer(b) B This is a dangling-else situation. The else associates with the nearest if (x > 15). Since x = 10 is not greater than 15, the else branch executes, printing "B".

4. II. Fill in the Blanks

1. The if statement is a selection control structure. 2. The for loop is an entry-controlled loop because it checks the condition before executing the body. 3. The do-while loop is an exit-controlled loop because it checks the condition after executing the body. 4. The break statement is used to exit a loop or switch block immediately. 5. The continue statement skips the remaining statements in the current iteration and jumps to the next iteration. 6. In a switch statement, the default case handles values that do not match any case label. 7. A loop that never terminates is called an infinite loop. 8. The while loop is best used when the number of iterations is not known in advance. 9. In a for loop, the three components are initialisation, condition, and update. 10. The switch expression must be of type integer or character, not float or double. 11. Fall-through occurs in a switch when a case does not end with a break. 12. The nested if is an if statement placed inside another if or else block. 13. In a for(int i=0; i<5; i++), the loop body executes 5 times.


5. III. True or False (with corrections)

1. The do-while loop may execute zero times.

False. The do-while loop is exit-controlled and always executes the body at least once before checking the condition. Only for and while can execute zero times.

2. The break statement can be used inside an if block that is not part of a loop or switch.

False. The break statement can only be used inside loops (for, while, do-while) or switch statements. Using it inside a standalone if block causes a compilation error.

3. A switch statement can use floating-point values in its case labels.

False. The switch expression and case labels must be integer or character types. Floating-point values are not allowed in switch statements.

4. The continue statement can be used in a switch statement.

False. The continue statement can only be used inside loops. In a switch, use break to exit. continue has no meaning inside a switch that is not enclosed by a loop.

5. An if statement can be nested inside another if statement.

True. Nesting of if statements is allowed. However, deep nesting should be avoided as it reduces readability.

6. The for loop is the best choice when the number of iterations is known beforehand.

True. The for loop is ideal when the number of iterations is known in advance because the initialisation, condition, and update are all specified in one line.


6. IV. Match the Following

Column AColumn B
1. for loopa. Exit-controlled loop
2. while loopb. Entry-controlled with known count
3. do-while loopc. Entry-controlled with unknown count
4. breakd. Skips current iteration
5. continuee. Exits loop or switch
6. switchf. Multi-way selection based on a value
7. Nested ifg. Selection within selection
Answer1-b, 2-c, 3-a, 4-e, 5-d, 6-f, 7-g

7. V. Answer Briefly (2-mark questions)

Q1. Differentiate between break and continue with examples.
Answer
Featurebreakcontinue
PurposeExits the loop/switch entirelySkips current iteration, continues loop
Used inLoops and switchLoops only
Examplefor(i=0;i<10;i++){if(i==5)break;} — stops at 5for(i=0;i<10;i++){if(i==5)continue;} — skips 5 only
Q2. What is a nested if statement? Give an example.
Answer

A nested if is an if statement placed inside another if or else block. It allows checking multiple conditions in a hierarchical manner.

if (marks >= 60) {
    if (marks >= 90)
        cout << "Grade A";
    else
        cout << "Grade B";
} else {
    cout << "Grade C";
}
Q3. When is the do-while loop preferred over the while loop?
Answer

The do-while loop is preferred when the loop body must execute at least once regardless of the condition. A common example is a menu-driven program where the menu is displayed at least once before checking if the user wants to continue.

int choice;
do {
    cout << "1. Add\n2. Exit\nEnter choice: ";
    cin >> choice;
} while (choice != 2);
Q4. What is fall-through in a switch statement? How do you prevent it?
Answer

Fall-through occurs when a case in a switch does not end with a break, causing execution to continue into the next case. To prevent it, always place a break at the end of each case.

switch (day) {
    case 1: cout << "Monday"; break;   // without break, falls to Tuesday
    case 2: cout << "Tuesday"; break;
}
Q5. Write the syntax of a for loop and explain each part.
for (initialisation; condition; update) {
    // loop body
}
Answer
  • Initialisation — Sets the starting value of the loop variable (e.g., int i = 0).
  • Condition — Checked before each iteration; the loop continues while it is true (e.g., i < 10).
  • Update — Modifies the loop variable after each iteration (e.g., i++).

8. VI. Answer in Detail (5-mark questions)

Q1. Explain the working of if, if-else, and if-else if-else with suitable C++ examples.
Answer

Simple if: Executes a block only when the condition is true.

int age = 18;
if (age >= 18) {
    cout << "Eligible to vote";
}
// Output: Eligible to vote

if-else: Chooses between two alternatives — one block if the condition is true, another if it is false.

int marks = 30;
if (marks >= 35) {
    cout << "Pass";
} else {
    cout << "Fail";
}
// Output: Fail

if-else if-else (ladder): Chooses among multiple alternatives by checking conditions in sequence. The first true condition's block executes; the rest are skipped. The final else is the default.

int marks = 78;
if (marks >= 90) {
    cout << "Grade A";
} else if (marks >= 75) {
    cout << "Grade B";
} else if (marks >= 60) {
    cout << "Grade C";
} else {
    cout << "Grade D";
}
// Output: Grade B

Key points:

  • Conditions are evaluated top to bottom; only the first true block runs.
  • The else block is optional but recommended as a default case.
  • Use braces {} even for single statements to avoid ambiguity.
Q2. Compare for, while, and do-while loops with examples.
// Print 1 to 5 using for loop
for (int i = 1; i <= 5; i++) {
    cout << i << " ";
}
// Output: 1 2 3 4 5
// Print 1 to 5 using while loop
int i = 1;
while (i <= 5) {
    cout << i << " ";
    i++;
}
// Output: 1 2 3 4 5
// Print 1 to 5 using do-while loop
int i = 1;
do {
    cout << i << " ";
    i++;
} while (i <= 5);
// Output: 1 2 3 4 5
Answer
Featureforwhiledo-while
Control typeEntry-controlledEntry-controlledExit-controlled
Min executions001
InitialisationIn syntaxBefore loopBefore loop
Condition checkBefore each iterationBefore each iterationAfter each iteration
UpdateIn syntaxInside bodyInside body
Best forKnown countUnknown count, pre-checkUnknown count, post-check
Q3. Explain the switch statement with an example. When is it preferred over if-else?
Answer

The switch statement is a multi-way selection structure that compares a single variable against multiple constant values (integer or character).

#include <iostream>
using namespace std;
int main() {
    char operator_char;
    int a = 10, b = 5, result;
    cout << "Enter operator (+, -, *, /): ";
    cin >> operator_char;
    switch (operator_char) {
        case '+':
            result = a + b;
            cout << a << " + " << b << " = " << result;
            break;
        case '-':
            result = a - b;
            cout << a << " - " << b << " = " << result;
            break;
        case '*':
            result = a * b;
            cout << a << " * " << b << " = " << result;
            break;
        case '/':
            result = a / b;
            cout << a << " / " << b << " = " << result;
            break;
        default:
            cout << "Invalid operator";
    }
    return 0;
}

When switch is preferred over if-else:

  • The variable is compared against discrete constant values (not ranges).
  • The expression type is integer or character (not float or double).
  • There are many alternatives (3 or more), making switch more readable than a long if-else ladder.

When if-else is preferred:

  • Conditions involve relational expressions (e.g., x > 5 && x < 10).
  • Floating-point comparisons are needed.
  • Complex logical conditions (&&, ||) are involved.

9. Common Mistakes to Avoid

1. Using = instead of == in conditions — if (x = 5) assigns 5 to x and evaluates as true. Use if (x == 5) for comparison.

2. Missing break in switch — Without break, fall-through occurs and unintended cases execute.

3. Forgetting the semicolon after do-while — do { ... } while(cond); needs a semicolon after the closing parenthesis. This is the only loop that requires one.

4. Off-by-one errors in loops — for (i = 0; i < 5; i++) runs 5 times (0-4). for (i = 1; i <= 5; i++) also runs 5 times (1-5). Be clear about your range.

5. Infinite loops — Forgetting to update the loop variable (e.g., while(i<10){ cout<<i; } — i never changes) causes the loop to run forever.

6. Confusing break and continue — break exits the entire loop; continue only skips the current iteration. They are not interchangeable.

7. Dangling else — In nested if-else, the else associates with the nearest if. Use braces to make the association clear.

8. Using float/double in switch — The switch expression must be integer or character type. Using float or double causes a compilation error.


10. Exam Checklist

  • [ ] I can explain the three types of control structures (sequential, selection, iteration)
  • [ ] I can write if, if-else, and if-else if-else statements with correct syntax
  • [ ] I can write and trace for, while, and do-while loops
  • [ ] I know the difference between entry-controlled and exit-controlled loops
  • [ ] I can use break and continue correctly inside loops
  • [ ] I can write a switch statement with break and default
  • [ ] I understand fall-through and how to prevent it
  • [ ] I can explain when to use switch vs if-else
  • [ ] I can identify and fix common errors: = vs ==, infinite loops, off-by-one
  • [ ] I can trace the output of nested if and loop programs

Frequently Asked Questions

Sequential (default, top-to-bottom), selection (if, if-else, switch), and iteration (for, while, do-while).