CodeGym /Courses /C# SELF /Nested ifs, multi-level logic

Nested ifs, multi-level logic

C# SELF
Level 3 , Lesson 2
Available

1. Basic syntax of a nested if

In real life, it's rare to make a decision based on just one thing. Usually, you have to check a bunch of conditions, and they often depend on each other. For example, to get into a concert, it's not enough to just be an adult — sometimes you also need to be on the guest list. That's where so-called multi-level logic comes in, and nested if statements help us out.

Nested conditions are when one if lives inside another. Imagine a box inside a box: if the first box opens (the condition is true), then you look inside and maybe open the second one. In terms of code and logic, this is a super powerful tool.

Let's start simple:

if (conditionA)
{
    // actions if conditionA is true
    if (conditionB)
    {
        // actions if both conditions are true
    }
}
A nested if inside another if

If this reminds you of a matryoshka doll — you totally get it! The nested if inside the outer one will only run if the outer one worked.

Example: getting into a club

Let's level up our learning app, where the user previously entered their name and age. Now let's add a second level of checking: say, to get into a private club, you need to be over 21 and have an invite code.

Console.Write("Enter your name: ");
string name = Console.ReadLine();

Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());

if (age >= 21)
{
    Console.Write("Enter your invite code: ");
    string code = Console.ReadLine();

    if (code == "VIP2024")
    {
        Console.WriteLine("Welcome, " + name + ", to the VIP club!");
    }
    else
    {
        Console.WriteLine("Sorry, wrong code. Access denied.");
    }
}
else
{
    Console.WriteLine("Sorry, entry is 21+ only!");
}

Heads up: the inner if checks the code only if the age check already passed. This saves resources (and doesn't annoy the bouncer).

2. How many levels of nesting are okay?

Technically, as many as you want. But honestly — the more nested levels, the harder it is to read and maintain your code. In practice, people rarely use more than 2-3 levels of nesting. If you end up with a "ladder" of 4 or more nested ifs, maybe it's time to rethink your logic or move some checks into separate functions (but that's a topic for another lecture!).

3. Nested if and else: gotchas and quirks

Beginner mistake: sloppy curly braces.

When you write nested conditions, especially with else, C# is super strict about syntax rules. If you skip the curly braces {}, only the first line after if or else belongs to it. This is a classic source of annoying bugs.

Example with a possible bug:

if (age >= 21)
    if (code == "VIP2024")
        Console.WriteLine("Welcome!");
else
    Console.WriteLine("Wrong code.");

A lot of folks expect the else to go with the age check, but actually it goes with the nearest if before it (the invite code check). Because of this, your messages might show up in weird places. It's better to always use braces — even if there's just one line inside!

Just to be clear, let me show you again. The compiler sees the code above like this:

if (age >= 21)
{    
    if (code == "VIP2024")
        Console.WriteLine("Welcome!");
else
    Console.WriteLine("Wrong code.");
}    

Important! Again:
If you don't put curly braces in an if-else block, the else goes with the previous (closest) if.

4. Alternatives: else if in nested conditions

Sometimes inside an outer if you want to not just check one more thing, but also have an alternative. For example, if the invite code didn't work, you could offer to buy a ticket. You do this with a nested else if/else:

if (age >= 21)
{
    Console.Write("Enter your invite code: ");
    string code = Console.ReadLine();

    if (code == "VIP2024")
    {
        Console.WriteLine("Welcome to the club!");
    }
    else if (code == "GUEST")
    {
        Console.WriteLine("Guest mode: access only until 11:00 PM.");
    }
    else
    {
        Console.WriteLine("Sorry, access not possible.");
    }
}
else
{
    Console.WriteLine("Entry is 21+ only.");
}

In this example, we made a nested choice between three scenarios: VIP code, guest code, and denial.

5. Practice: discount calculator with multi-level logic

Let's write an app like this: the user gets a store discount based on their age and whether they have a club card.

  • If the user's age is greater than or equal to 60 — discount is 15%.
  • If the age is less, but the user has a club card — discount is 7%.
  • Otherwise, no discount.

How do we code this?

Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());

double discount = 0.0;

if (age >= 60)
{
    discount = 0.15;
}
else
{
    Console.Write("Do you have a club card (yes/no)? ");
    string hasCard = Console.ReadLine();

    if (hasCard == "yes")
    {
        discount = 0.07;
    }
}

Console.WriteLine("Your discount: " + (discount * 100) + "%");

Here we used a nested if inside the else branch. If the age didn't qualify, only then do we ask about the card.

2
Task
C# SELF, level 3, lesson 2
Locked
Age Check for the Ride
Age Check for the Ride
2
Task
C# SELF, level 3, lesson 2
Locked
Ticket Price Calculation
Ticket Price Calculation
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION