C# Conditional Statements – Fun and Easy Coding Examples
Time to Get Your Hands Dirty! ⌨️🚀
Hey there, coding champ! 😃 You’ve learned if-else statements like a pro. You also got the hang of switch cases and even tackled some advanced conditional statements. That’s amazing! 🎉
But here’s the thing… learning without practice is like having a car but never driving it. 🚗💨 Now, it’s time to put your knowledge to the test with some fun coding exercises!
Don’t worry, I’ve got your back. 😉 These examples are super easy to understand and will help you gain confidence in writing conditional statements like a boss. Let’s dive in! 💪🔥
1️⃣ If-Else Statement Example – “Can You Ride the Roller Coaster?” 🎢
Ever been to an amusement park and seen the “You must be this tall to ride” sign? Well, let’s write a C# program to check if a person is tall enough to ride a roller coaster! Sounds fun, right? Let’s do this!
Question:
A roller coaster has a height requirement. A person must be at least 120 cm tall to ride. Write a program that asks for the person’s height and prints:
✅ “Enjoy your ride!” if they meet the height requirement.
❌ “Sorry, you are not tall enough to ride.” if they don’t.
Hint:
Use an if-else
statement. If the height is 120 or more, print “Enjoy your ride!” Otherwise, print “Sorry, you are not tall enough to ride.”
Solution:
using System;
class Program
{
static void Main()
{
Console.Write("Enter your height in cm: ");
int height = Convert.ToInt32(Console.ReadLine());
if (height >= 120)
{
Console.WriteLine("Enjoy your ride!");
}
else
{
Console.WriteLine("Sorry, you are not tall enough to ride.");
}
}
}
Output Example:
Enter your height in cm: 130
Enjoy your ride!
or
Enter your height in cm: 110
Sorry, you are not tall enough to ride.
Explanation:
- The program asks for the person’s height in cm.
- If the height is 120 cm or more, the program prints “Enjoy your ride!” 🎢
- Otherwise, it prints “Sorry, you are not tall enough to ride.” 😔
2️⃣ Switch Case Example – “What’s the Day?” 📅
Question:
You need to write a program that takes a number (1-7) from the user and prints the corresponding day of the week. For example, 1 = Monday, 2 = Tuesday, etc.
Hint:
- Use a
switch
statement. If the user enters1
- Print “Monday”. If
2
- Print “Tuesday”, and so on.
- If they enter a number outside
1-7
, print “Invalid day number!”
Solution:
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number (1-7): ");
int day = Convert.ToInt32(Console.ReadLine());
switch (day)
{
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
case 3: Console.WriteLine("Wednesday"); break;
case 4: Console.WriteLine("Thursday"); break;
case 5: Console.WriteLine("Friday"); break;
case 6: Console.WriteLine("Saturday"); break;
case 7: Console.WriteLine("Sunday"); break;
default: Console.WriteLine("Invalid day number!"); break;
}
}
}
Output Example:
Enter a number (1-7): 3
Wednesday
or
Enter a number (1-7): 8
Invalid day number!
Explanation:
- The program asks for a number.
- The
switch
statement matches the number with the correct day. - If the number is not between 1-7, it prints “Invalid day number!”
3️⃣ Advanced Conditional Statement Example – “Should You Take an Umbrella?” ☔
Question:
Imagine you are planning to go out. You need a program that asks two things:
- Is it raining? (Yes/No)
- Do you have an umbrella? (Yes/No) If it’s raining and you don’t have an umbrella, print “Stay inside!”. Otherwise, print “You can go out!”
Hint:
Use&&
(Logical AND) to check if both conditions are true.
Solution:
using System;
class Program
{
static void Main()
{
Console.Write("Is it raining? (yes/no): ");
string raining = Console.ReadLine().ToLower();
Console.Write("Do you have an umbrella? (yes/no): ");
string umbrella = Console.ReadLine().ToLower();
if (raining == "yes" && umbrella == "no")
{
Console.WriteLine("Stay inside!");
}
else
{
Console.WriteLine("You can go out!");
}
}
}
Output Example:
Is it raining? (yes/no): yes
Do you have an umbrella? (yes/no): no
Stay inside!
Or
Is it raining? (yes/no): yes
Do you have an umbrella? (yes/no): yes
You can go out!
Explanation:
- The program asks if it’s raining and if you have an umbrella.
- If both answers are “yes” and “no” (meaning it’s raining and you don’t have an umbrella), it prints “Stay inside!”
- Otherwise, it prints “You can go out!”
Next What? 🚀
You’ve got the basics down! Now, it’s time to test your skills. In the next lesson, you’ll get some fun programming exercises on if-else statements, switch cases, and advanced conditions. So, get ready to challenge yourself! 💪🔥