Lab Sheet 3: Switch Statement & Loop Structures in C#

🧩 Section A – Switch Statement Exercises
Exercise 1: Basic Switch
Write a program that:
Prompts the user to enter a number between 1–3.
Prints:
“You selected One” for 1
“You selected Two” for 2
“You selected Three” for 3
“Invalid selection” for any other number.
Exercise 2: Switch with Strings
Write a program that asks the user to type a day name (e.g., “Monday”).
Display whether it’s a weekday or a weekend using a switch statement.
Hint: Convert input to lowercase using ToLower() before comparing.
Example Output:
Enter a day: Saturday
It's a weekend!
Exercise 3: Simple Menu with Switch
Create a simple text-based menu system:
1. Start Game
2. Load Game
3. Exit
Ask the user to choose an option and display a message accordingly.
If an invalid number is entered, show “Invalid choice.”
Extension: Keep displaying the menu until the user chooses Exit.
Exercise 4: Grading System
Ask the user to enter a grade character (‘A’, ‘B’, ‘C’, ‘D’, or ‘F’).
Use a switch to print feedback:
A → Excellent!
B → Good job!
C → Fair
D → Needs improvement
F → Failed
Use default for invalid entries.
🧩 Section B – For Loop Exercises
Exercise 5: Counting Up
Write a program that prints numbers from 1 to 10 using a for loop.
Exercise 6: Counting Down
Write a program that prints numbers from 10 down to 1.
Hint: Use for (int i = 10; i >= 1; i--)
Exercise 7: Odd and Even Numbers
Ask the user for a number n.
Use a for loop to print all even numbers between 1 and n.
Extension: Add another loop to print odd numbers separately.
Exercise 8: Multiplication Table Generator
Write a program that displays the multiplication table of a number entered by the user.
Example Output (for 5):
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Exercise 9: Sum of Numbers
Ask the user to enter a positive integer n.
Use a for loop to calculate and print the sum of all numbers from 1 to n.
Example:
Enter n: 5
Sum = 15
Exercise 10: Factorial Calculation
Use a for loop to calculate the factorial of a given number.
Example:
Enter a number: 4
Factorial = 24
Exercise 11: Nested For Loop – Patterns
Write a program that prints the following pattern:
*
**
***
****
*****
Extension: Try to reverse the pattern:
*****
****
***
**
*
Exercise 12: Skip and Stop
Use a for loop to display numbers from 1 to 10.
Skip printing 5 (
continue)Stop when you reach 8 (
break)
Exercise 13: Dual Counter
Write a loop with two variables:
for (int i = 0, j = 10; i < j; i++, j--)
{
Console.WriteLine($"i = {i}, j = {j}");
}
Observe and explain when the loop stops and how i and j change.
Exercise 14: Infinite Loop (Demo)
Write a for loop without a condition:
for( ; ; )
{
Console.WriteLine("This is an infinite loop!");
}
Stop the execution manually and discuss why it never ends.
🧩 Section C – While Loop Exercises
Exercise 15: Basic While Loop
Print numbers from 1 to 5 using a while loop.
Exercise 16: Countdown
Ask the user for a number n and print numbers from n down to 1 using a while loop.
Exercise 17: Sum Until Limit
Keep asking the user to enter numbers until the total sum exceeds 100.
Display the total at the end.
Exercise 18: Number Guessing Game
Generate a random number between 1–10.
Ask the user to guess until they get it correct.
Use a while loop for repetition.
🧩 Section D – Do...While Loop Exercises
Exercise 19: Basic Do While
Write a program that prints “Hello World! The counter is X”
until X becomes 5. Start from 0.
Exercise 20: User Confirmation
Ask the user:
Do you want to continue? (y/n)
Use a do...while loop to repeat until the user types ‘n’.
Exercise 21: Menu Repetition
Display this menu inside a do...while loop:
1. Add
2. Subtract
3. Multiply
4. Exit
Perform the operation and continue showing the menu until user selects Exit.
🧩 Section E – Integrated Challenge
Exercise 22: Simple Calculator
Build a menu-driven calculator using a switch and do...while loop:
Menu Options: Add, Subtract, Multiply, Divide, Exit
Use
switchto handle operationsLoop until user selects Exit
Hint: Validate division by zero.
Exercise 23: Pattern Generator (Advanced)
Ask the user to enter a number and print:
1
12
123
1234
Use nested for loops.
Exercise 24: Prime Numbers
Ask for a number n and print all prime numbers up to n using loops.
🧠 Reflection Questions
What is the key difference between
forandwhileloops?Why must we use
breakin aswitchblock?What happens if you forget to update the loop variable in a
whileloop?What is the difference between entry-controlled and exit-controlled loops?



