C# if else Example: Easy Guide with if, else-if & Nested if
Hey there, future C# pro! 👋 Ever wondered how to make your code smarter? Imagine telling your program, “Hey, if this happens, do that. Otherwise, do something else!” That’s exactly what if else in C# does. It’s like giving your code decision-making power! 💡
What is if else in C#?
The if-else
statement is like a crossroads. Your program checks a condition, and based on whether it’s true or false, it picks a different path. Let’s take a simple example before we jump into the real-world scenario.
Basic Example
int age = 18;
if (age >= 18)
{
Console.WriteLine("You can vote!");
}
else
{
Console.WriteLine("Sorry, you are too young to vote.");
}
Output:
You can vote!
Explanation:
- If
age
is 18 or more, the program prints “You can vote!” - Otherwise, it prints “Sorry, you are too young to vote.”
Simple, right? Now, let’s make it fun with a real-world example! 🎉
Real-World Example: Coffee Shop Discount ☕
Imagine you run a coffee shop. You want to give a 10% discount to customers who spend $50 or more. If they spend less, they pay the full price. Let’s write a program for that!
C# if else Example: Coffee Shop Discount
using System;
class CoffeeShop
{
static void Main()
{
Console.Write("Enter your bill amount: $");
double billAmount = Convert.ToDouble(Console.ReadLine());
double finalAmount;
if (billAmount >= 50)
{
finalAmount = billAmount * 0.9; // 10% discount
Console.WriteLine("Congrats! You got a 10% discount. Your final bill is: $" + finalAmount);
}
else
{
finalAmount = billAmount; // No discount
Console.WriteLine("No discount applied. Your final bill is: $" + finalAmount);
}
}
}
Sample Output 1 (When user enters $60)
Enter your bill amount: $60
Congrats! You got a 10% discount. Your final bill is: $54
Sample Output 2 (When user enters $40)
Enter your bill amount: $40
No discount applied. Your final bill is: $40
How Does It Work?
- The program asks for the bill amount.
- If it’s $50 or more, it applies a 10% discount.
- Otherwise, the user pays the full amount.
- The output changes dynamically based on what the user enters.
Cool, right? Now, imagine you could add a loyalty program or special discounts for birthdays. That’s how powerful if else C# can be! 🚀
Alright, buddy! Now, let’s check out the different flavors of C# If-Else statements. 🍕😃
- If Statement – A simple check ✅
- If-Else Statement – Choose between two options 🤔
- Else-if Statement – Handle multiple conditions 🏗️
- Nested If – When one condition depends on another 🔄
1. if Statement (The Simple Check ✅)
The if
statement checks one condition. If it’s true, the code inside runs. Otherwise, it’s skipped.
Example: Checking if a number is positive
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
Output:
The number is positive.
How It Works:
- The program checks if
number > 0
. - Since 10 is positive, it prints “The number is positive.”
If number
was -5
, nothing would happen! That’s where if-else
comes in.
2. if-else Statement (The Either-Or Decision 🤔)
Sometimes, you need two choices. If the condition is true, do one thing. Otherwise, do something else.
Example: Voting Eligibility
int age = 17;
if (age >= 18)
{
Console.WriteLine("You can vote!");
}
else
{
Console.WriteLine("Sorry, you're too young to vote.");
}
Output:
Sorry, you're too young to vote.
How It Works:
- If
age >= 18
, it prints “You can vote!” - Otherwise, it prints “Sorry, you’re too young to vote.”
3. else-if Ladder (Multiple Conditions 🏗️)
What if we need more than two choices? That’s where else-if
helps!
Example: Movie Ticket Pricing 🎬
int age = 12;
if (age < 5)
{
Console.WriteLine("Ticket is free!");
}
else if (age <= 12)
{
Console.WriteLine("Child ticket: $5");
}
else if (age <= 18)
{
Console.WriteLine("Teen ticket: $8");
}
else
{
Console.WriteLine("Adult ticket: $12");
}
Output:
Child ticket: $5
How It Works:
- If age < 5, the ticket is free.
- If age is 5-12, it’s $5.
- If age is 13-18, it’s $8.
- Otherwise, it’s $12.
4. Nested if (Conditions Inside Conditions 🔄)
A nested if means an if
inside another if
. This is useful when one condition depends on another.
Example: Restaurant Discount 🍽️
double billAmount = 60;
bool isMember = true;
if (billAmount > 50)
{
Console.WriteLine("You qualify for a discount!");
if (isMember)
{
Console.WriteLine("Since you're a member, you get 20% off!");
}
else
{
Console.WriteLine("You get 10% off.");
}
}
else
{
Console.WriteLine("No discount applied.");
}
Output:
You qualify for a discount!
Since you're a member, you get 20% off!
How It Works:
- If the bill is more than $50, the user qualifies for a discount.
- If they are a member, they get 20% off.
- If not, they get 10% off.
Conclusion 🎯
Boom! 🎉 You just mastered if, if-else, else-if, and nested if in C#! These are super useful in games, apps, and websites. Now, your programs can make decisions like a pro!
Next What? 🚀
Great job! You’re on fire! 🔥 But what if you have tons of conditions? Checking each one with if-else
could be messy. That’s where switch case comes in! In the next lesson, we’ll make decision-making even easier!
👉 Tell me—what part confused you the most? Let’s crack it together! 😊