← Back to C# Course | Chapter 4: Control Flow | Lesson 1 of 7

if / else

In this page:

  1. if / else

if / else

An if statement runs a block only when its condition is true; an optional else runs when it's false. else if chains let you test multiple conditions in order, stopping at the first match. Braces are optional for single statements but recommended for clarity.

Note: Chain conditions with else if instead of nesting separate if blocks.

Example: if / else

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        int score = 72;

        if (score >= 90)
            Console.WriteLine("A");
        else if (score >= 70)
            Console.WriteLine("B");
        else
            Console.WriteLine("C");
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.