Skip to Content
Course content

10: Control Flow: If/Else

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.