Skip to main content

Command Palette

Search for a command to run...

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

Published
5 min read
Lab Sheet 3: Switch Statement & Loop Structures in C#
A
I co-founded🫆digizen (fighting internet chaos 🧠), building an AI app to tackle misinformation, and running ideaGeek to turn “I have an idea” into real startups. I also share tech + travel on YouTube (TechNomad & Rz Omar). I touch grass too 🌱… but mostly to debug life 💻

🧩 Section A – Switch Statement Exercises

Exercise 1: Basic Switch

Write a program that:

  1. Prompts the user to enter a number between 1–3.

  2. 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 switch to handle operations

  • Loop 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

  1. What is the key difference between for and while loops?

  2. Why must we use break in a switch block?

  3. What happens if you forget to update the loop variable in a while loop?

  4. What is the difference between entry-controlled and exit-controlled loops?

More from this blog

T

TechNomad

46 posts

TechNomad is mostly coding exercises, not long boring explanations 😌. I share what I teach and learn through hands-on problems—mainly for my students, but anyone can jump in. If you learn by doing (and breaking things), you’ll fit right in. Less theory, more “try this and see what happens.”