C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
10: Control Flow: If/Else
Think about how you handle a simple decision in your day-to-day life, like deciding whether to take an umbrella when you leave the house. You don't just blindly grab it every single day; you check a condition first. In your head, the logic goes: "If it is raining outside, I will take the umbrella. Otherwise, I'll leave it at home."
In C#, that's exactly what we're doing with control flow. We're telling the computer to stop blindly executing lines of code from top to bottom and instead take a detour based on whether a specific condition is true or false. It's the "fork in the road" for your program.
Teaching the Computer to Decide
The most basic tool for this is the if statement. It requires a condition inside parentheses that evaluates to a boolean (either true or false). If that condition is true, the code inside the curly braces runs. If it's false, the computer simply skips over that entire block as if it didn't exist.
int accountBalance = 100;
int withdrawalAmount = 50;
if (accountBalance >= withdrawalAmount)
{
Console.WriteLine("Dispensing cash...");
accountBalance -= withdrawalAmount;
}
In this example, I'm checking if the balance is greater than or equal to the amount being requested. If you had \$100 and asked for \$50, the condition is true, and the cash is dispensed. But if you tried to withdraw \$150, the computer would see that 100 >= 150 is false and just jump straight to whatever code comes after the closing brace.
The Safety Net
Usually, you don't want your program to just stay silent when a condition isn't met. You want to provide an alternative. This is where the else statement comes in. Think of it as the "catch-all" or the safety net. It only runs if the if condition failed.
if (accountBalance >= withdrawalAmount)
{
Console.WriteLine("Dispensing cash...");
}
else
{
Console.WriteLine("Insufficient funds. Please check your balance.");
}
I like to think of else as the "otherwise" part of the conversation. There is no condition attached to an else block because it doesn't need one—it's simply the default path taken when the first door is locked.
Handling Multiple Possibilities
Real-world logic is rarely just "yes or no." Often, you have a handful of different options. For instance, imagine you're building a system to categorize a user's membership level based on how much they spend. You can't do that with a single if/else. Instead, we use else if to chain multiple checks together.
int totalSpent = 500;
if (totalSpent >= 1000)
{
Console.WriteLine("Platinum Member");
}
else if (totalSpent >= 500)
{
Console.WriteLine("Gold Member");
}
else if (totalSpent >= 100)
{
Console.WriteLine("Silver Member");
}
else
{
Console.WriteLine("Bronze Member");
}
Here is the crucial part: the computer evaluates these from top to bottom. As soon as it finds one condition that is true, it executes that block and skips everything else in the chain. If I had totalSpent = 1200, it would hit the first if, print "Platinum Member," and completely ignore the Gold, Silver, and Bronze checks. This is why the order of your conditions matters immensely—always check for your most specific or restrictive conditions first.
📋 Practical Task
The Movie Ticket Price Calculator
You are writing a small module for a cinema's kiosk. Your task is to determine the ticket price based on the customer's age. Use an if / else if / else structure to implement the following logic:
- If the age is 65 or older, the price is $7 (Senior discount).
- If the age is 13 or older (but under 65), the price is $12 (Standard adult).
- If the age is under 13, the price is $8 (Child price).
Create an integer variable named customerAge, assign it a value, and print the final price to the console. Test your code by changing the age to see if the price updates correctly for each category.
There are no comments for now.