C# Academy · Lesson

Control Flow: If-Else Statements

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

Lesson 1 of 510 steps

Control Flow: If-Else Statements is a free C# Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 5 lessons in total.

Introduction to If-Else Statements

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

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.

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.");
        }
    }
}

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.");
        }
    }
}

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.

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.");
        }
    }
}

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.");
        }
    }
}

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.");
        }
    }
}

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
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

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. 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. The C# Academy course includes 5 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 5.

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