0Pricing
C# Academy · Lesson

Control Flow: If-Else Statements

Learn how to make decisions in your programs using if-else statements.

Control Flow: If-Else Statements is a free C# Academy lesson on CoddyKit — lesson 1 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Control Flow: If-Else Statements

Welcome to the first lesson in C# Fundamentals! In this lesson, you’ll learn how to use if-else statements to make decisions in your programs. Let’s get started!

Control Flow: If-Else Statements — illustration 1

2

What Are If-Else Statements?

If-else statements allow your program to make decisions based on conditions. Depending on whether a condition is true or false, the program executes a specific block of code.

Key Point: Use if-else statements to control the flow of your program based on logic.

3

Syntax of If-Else Statements

The basic syntax of an if-else statement is:

if (condition) {

/* code to execute if condition is true */ }

else {

/* code to execute if condition is false */

}

Example:

using System;

class Program {
    static void Main() {
        int age = 20;

        if (age >= 18) {
            Console.WriteLine("You are an adult.");
        } else {
            Console.WriteLine("You are not an adult.");
        }
    }
}

4

Using Else-If

If you need to check multiple conditions, you can use else-if statements. Example:

Key Point: Use else-if to check additional conditions after the initial if statement.

using System;

class Program {
    static void Main() {
        int age = 20;

        if (age >= 18) {
            Console.WriteLine("You are an adult.");
        } else {
            Console.WriteLine("You are not an adult.");
        }
    }
}

5

Comparison Operators

If-else statements use comparison operators to evaluate conditions. Common operators include:

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Tip: Use the appropriate operator for your condition.

6

Logical Operators

You can combine conditions using logical operators:

  • &&: Logical AND (true if both conditions are true).
  • ||: Logical OR (true if at least one condition is true).
  • !: Logical NOT (inverts the truth value).

Example:

using System;

class Program {
    static void Main() {
        int age = 25;
        bool hasLicense = true;

        if (age >= 18 && hasLicense) {
            Console.WriteLine("You can drive.");
        } else {
            Console.WriteLine("You cannot drive.");
        }
    }
}

7

Nesting If Statements

You can nest if statements inside each other for more complex conditions. Example:

Tip: Avoid excessive nesting to keep your code readable.

using System;

class Program {
    static void Main() {
        int age = 20;
        bool hasPermission = true;

        if (age >= 18) {
            if (hasPermission) {
                Console.WriteLine("Access granted.");
            } else {
                Console.WriteLine("Access denied. Permission required.");
            }
        } else {
            Console.WriteLine("Access denied. You must be 18 or older.");
        }
    }
}

8

Practice Challenge

Write a program that asks the user for their age and checks if they are eligible to vote. Example:

using System;

class Program {
    static void Main() {
        Console.WriteLine("Enter your age:");
        int age = int.Parse(Console.ReadLine());

        if (age >= 18) {
            Console.WriteLine("You are eligible to vote.");
        } else {
            Console.WriteLine("You are not eligible to vote.");
        }
    }
}

9

10

Great Job!

Congratulations! You’ve learned how to use if-else statements to make decisions in your C# programs. In the next lesson, we’ll explore loops and learn how to repeat tasks efficiently. Let’s keep coding!

Control Flow: If-Else Statements — illustration 10

Frequently asked questions

Is the “Control Flow: If-Else Statements” lesson free?

Yes — the full text of “Control Flow: If-Else Statements” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Control Flow: If-Else Statements”?

Learn how to make decisions in your programs using if-else statements. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Control Flow: If-Else Statements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Control Flow: If-Else Statements
  2. Loops in C#: For, While, and Do-While
  3. Switch Statements
  4. Introduction to Arrays
  5. Working with Strings
← Back to C# Academy