In programming, control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. Without control flow statements, a program would execute linearly, one instruction after another. Control flow statements allow us to make decisions (conditional statements) and repeat actions (loops), making our programs much more powerful and flexible. This lesson will cover the fundamental control flow statements in C#: if/else for conditional execution and for/while for creating loops.

Conditional Statements: if/else

Conditional statements allow you to execute different blocks of code based on whether a certain condition is true or false. The most common conditional statement is the if statement, often used with an optional else clause.

The if Statement

The if statement evaluates a boolean expression (an expression that results in true or false). If the expression is true, the code block within the if statement is executed. If the expression is false, the code block is skipped.

int age = 20;

// Check if the age is greater than or equal to 18
if (age >= 18)
{
    // This code will be executed because age is 20, which is >= 18
    Console.WriteLine("You are an adult.");
}

Console.WriteLine("This line will always be executed.");

In this example, the condition age >= 18 is evaluated. Since age is 20, the condition is true, and the message "You are an adult." is printed to the console. The line "This line will always be executed." is outside the if block, so it will always be executed regardless of the condition.

The else Statement

The else statement provides an alternative code block to execute when the if condition is false.

int age = 16;

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else
{
    // This code will be executed because age is 16, which is not >= 18
    Console.WriteLine("You are a minor.");
}

Console.WriteLine("This line will always be executed.");

In this case, the condition age >= 18 is false because age is 16. Therefore, the code block within the else statement is executed, and the message "You are a minor." is printed.

The else if Statement

The else if statement allows you to check multiple conditions in a sequence. It's placed between the if and else statements. The else if condition is only evaluated if the preceding if or else if conditions are false.

int score = 75;

if (score >= 90)
{
    Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
    Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
    // This code will be executed because score is 75, which is >= 70
    Console.WriteLine("Grade: C");
}
else if (score >= 60)
{
    Console.WriteLine("Grade: D");
}
else
{
    Console.WriteLine("Grade: F");
}

Console.WriteLine("Grading complete.");

In this example, the code checks the score against different grade ranges. The first if condition (score >= 90) is false. The second else if condition (score >= 80) is also false. The third else if condition (score >= 70) is true, so "Grade: C" is printed. The remaining else if and else blocks are skipped.

Nested if Statements

You can also nest if statements inside other if or else statements to create more complex decision-making logic.

bool hasLicense = true;
int driverAge = 22;

if (hasLicense)
{
    Console.WriteLine("Driver has a license.");

    if (driverAge >= 18)
    {
        Console.WriteLine("Driver is eligible to drive.");
    }
    else
    {
        Console.WriteLine("Driver is underage but has a license.");
    }
}
else
{
    Console.WriteLine("Driver does not have a license.");
}

In this example, the outer if statement checks if the driver has a license. If they do, the inner if statement checks if they are old enough to drive. This demonstrates how you can create a hierarchy of conditions to handle different scenarios.

Practice Activities for if/else Statements