C# If Else Statement with Examples

  Keywords : C# if-else condition will execute either if block or else block of statements based on the value of Boolean expression with examples, C# if-else statement is having an optional else block

Here we will learn if else statement in c# with examples and how to use if-else condition in c# to execute if statements when the Boolean expression is true otherwise to execute the else block of statements with examples.

C# If-Else Statement

In c#, if-else statement or condition is having an optional else statements and these else statements will be executed whenever the if condition is fails to execute.

 

Generally, in c# if-else statement, whenever the boolean expression returns true, then if statements will be executed otherwise else block of statements will be executed.

Syntax of C# if-else Statement

Following is the syntax of defining if else statement in c# programming language.

 

if (boolean_expression)

{

// Statements to Execute if boolean expression is True

}

else {

// Statements to Execute if boolean expression is False

}

If you observe above if-else statement syntax, the statements inside of if condition will be executed only when the “bool_expression” returns true otherwise the statements inside of else condition will be executed.

 

Following is the simple example of using if-else statement in c# programming language.

 

int x = 20;

if (x >= 10)

{

Console.WriteLine("x is Greater than or equals 10");

}

else

{

Console.WriteLine("x is less than or equals to 10");

}

If you observe above example, whenever the defined condition (x >= 10) returns true, then if condition will be executed otherwise the else block of code will be executed.

C# If-Else Statement Flow Chart Diagram

Following is the flow chart diagram which will represent the process flow of if-else statement in c# programming language.

 

C# If Else Statement Flow Chart Diagram

If you observe above c# if-else statement flow chart, if the defined condition is true, then the statements within the if condition will be executed otherwise the statements within the else block will be executed.

C# If-Else Statement Example

Following is the example of defining if-else statement in c# programming language to execute the block of code or statements based on Boolean expression.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 20;

            if (x >= 10)

            {

                Console.WriteLine("x is Greater than or Equals to 10");

            }

            else

            {

                Console.WriteLine("x is Less than 10");

            }

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we defined an if-else conditions to execute the statements based on defined condition status either true or false.

Output of C# If Else Statement Example

When we execute above c# program, we will get the result like as shown below.

 

C# If Else Statement Example Result

 

If you observe above result, the Boolean expression returns true that’s the reason only if block of statements has been executed and printed the statements in console window.

 

This is how we can use if-else statement in c# programming language to execute the block of code or statements based on our requirements.