Easy Tutorial
❮ Csharp While Loop Csharp Exception Handling ❯

C# if Statement

C# Decision

An if statement consists of a Boolean expression followed by one or more statements.

Syntax

The syntax for an if statement in C#:

if(boolean_expression)
{
   /* Statements to execute if the Boolean expression is true */
}

If the Boolean expression evaluates to true, the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, the first set of code after the if statement (after the closing bracket) will be executed.

Flowchart

Example

Example

using System;

namespace DecisionMaking
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Local variable definition */
            int a = 10;

            /* Check the boolean condition using if statement */
            if (a < 20)
            {
                /* Print the following statement if the condition is true */
                Console.WriteLine("a is less than 20");
            }
            Console.WriteLine("The value of a is {0}", a);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

a is less than 20
The value of a is 10

C# Decision

❮ Csharp While Loop Csharp Exception Handling ❯