Lab Exercise: Mastering Control Flow in
C++
Objective
By the end of this lab, students will be able to:
• Use conditional statements (if, if-else, if-else ladder, nested if, switch)
• Implement loops (for, while, do-while)
• Apply jump statements (break, continue, goto)
Part 1: Conditional Statements
Task 1: Simple If
Write a program that asks the user to enter a number and checks if it is positive.
Task 2: If-Else
Modify the program to check whether the number is positive or negative.
Task 3: If-Else Ladder
Write a program that takes a student's mark (0–100) and displays the grade:
• A (≥ 80)
• B (70–79)
• C (60–69)
• D (50–59)
• F (< 50)
Task 4: Nested If
Write a program that takes age and citizenship status to determine if a person is
eligible to vote (Must be ≥ 18 and a citizen).
Task 5: Switch Statement
Create a menu-driven calculator. Input two numbers and an operator (+, -, *, /).
Use a switch statement to perform the calculation.
Part 2: Loop Statements
Task 6: For Loop
Print numbers from 1 to 10 and display their sum.
Task 7: While Loop
Ask the user to enter numbers until they enter 0, then display the total sum.
Task 8: Do-While Loop
Create a simple password checker: Ask the user to enter a password and repeat
until the correct password is entered.
Part 3: Jump Statements
Task 9: Break
Write a program that loops from 1 to 20 but stops the loop using break when a
number is divisible by 7.
Task 10: Continue
Write a program that prints numbers from 1 to 10 but skips numbers divisible by 3
using continue.
Task 11: Goto
Write a program that uses goto to repeat a menu until the user selects “Exit”.
Warning: Note that goto is generally discouraged in modern programming
because it can lead to "spaghetti code," making programs hard to follow and
debug.
Bonus Challenge (Integration Task)
Create a number guessing game that incorporates multiple control flow
elements:
• Uses a loop to allow repeated attempts.
• Uses if-else to give hints (too high / too low).
• Uses break when the correct number is guessed.
• Uses continue to skip invalid inputs.