C# If Statement with Examples

  Keywords : C# if condition will execute the statements based on the value of Boolean expression with examples, C# if statement to execute the block of code when the given condition is true with example

Here we will learn if statement in c# with examples and how to use if condition in c# to execute the statements based on the value of Boolean expression with examples.

C# If Statement

In c#, if statement or condition is used to execute the block of code or statements when the defined condition is true.

 

Generally, in c# the statement that can be executed based on the condition is known as a “Conditional Statement” and the statement is more likely a block of code.

Syntax of C# if Statement

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

 

if (bool_expression) {

// Statements to Execute if condition is true

}

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

 

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

 

int x = 20;

if (x >= 10)

{

Console.WriteLine("Number Greater than 10");

}

If you observe above example, the Console statement will be executed only when the defined condition (x >= 10) returns true.

C# If Statement Flow Chart Diagram

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

 

C# If Statement Flow Chart Diagram

 

If you observe above c# if statement flow chart, if the defined condition is true, then the statements within the if condition will be executed otherwise the if condition will comes to an end without executing the statements.

C# If Statement Example

Following is the example of defining if 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, y = 10;

            if (x >= 10)

            {

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

            }

            if (y <= 5)

            {

                Console.WriteLine("y is less than or equals to 5");

            }

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

            Console.ReadLine();

        }

    }

}

If you observe above code, we defined a two if conditions to execute statements based on defined variables (x, y) values.

Output of C# If Statement Example

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

 

C# If Statement Example Result

 

If you observe above result, only one if statement condition is true that’s the reason only one statement printed on console window.

 

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