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
| Type | Description | Examples |
|---|---|---|
| Sequential | Default flow — top to bottom | All statements execute in order |
| Selection | Choose between alternatives | if, if-else, switch |
| Iteration | Repeat a block of code | for, 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)
| Feature | for | while | do-while |
|---|---|---|---|
| Entry/Exit controlled | Entry | Entry | Exit |
| Minimum executions | 0 | 0 | 1 |
| Best for | Known iteration count | Unknown count, pre-check | Unknown count, post-check |
| Syntax | for(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
| Statement | Purpose |
|---|---|
break | Exits the nearest enclosing loop or switch |
continue | Skips the rest of the current loop iteration |
goto | Jumps to a labelled statement (avoid in practice) |
3. I. Choose the Best Answer (MCQs)
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
cout << i << " ";
}
break statement is used to:switch statement, what happens if break is missing after a case?int x = 10;
if (x > 5)
if (x > 15)
cout << "A";
else
cout << "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 A | Column B |
|---|---|
1. for loop | a. Exit-controlled loop |
2. while loop | b. Entry-controlled with known count |
3. do-while loop | c. Entry-controlled with unknown count |
4. break | d. Skips current iteration |
5. continue | e. Exits loop or switch |
6. switch | f. Multi-way selection based on a value |
7. Nested if | g. Selection within selection |
7. V. Answer Briefly (2-mark questions)
break and continue with examples.if statement? Give an example.do-while loop preferred over the while loop?switch statement? How do you prevent it?for loop and explain each part.for (initialisation; condition; update) {
// loop body
}
8. VI. Answer in Detail (5-mark questions)
if, if-else, and if-else if-else with suitable C++ examples.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
switch statement with an example. When is it preferred over if-else?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, andif-else if-elsestatements with correct syntax - [ ] I can write and trace
for,while, anddo-whileloops - [ ] I know the difference between entry-controlled and exit-controlled loops
- [ ] I can use
breakandcontinuecorrectly inside loops - [ ] I can write a
switchstatement withbreakanddefault - [ ] I understand fall-through and how to prevent it
- [ ] I can explain when to use
switchvsif-else - [ ] I can identify and fix common errors:
=vs==, infinite loops, off-by-one - [ ] I can trace the output of nested
ifand loop programs
