C# if statement

Tutorials PanelPosted by

The ‘if’ Statement is one of the most used decision-making statement in programming. Whenever you need to test a condition; the ‘if’ statement can be used. The ‘if’ statement checks if a statement true or false and route the program based on the result of that statement.

‘if’ statement Flowchart

Syntax of ‘if statement’

  if (boolean expession)
{
// execute the code if the statement is true
}
// goes on with the code

The ‘if’ statement ‘boolean expression’ should be inside two parenthesis. The expression code is between two brackets. If the expression is true, then the code between the two brackets will be executed. Otherwise, the program will continue without executing the code inside the brackets.

Example of ‘if statement’

            if (true) // condition is true 
            {
                Console.WriteLine(" this is True");
            }

A logical expression can be used in the ‘if’ statement, such as in the following example:

            if (1 < 2) // expression is true 
            {
                Console.WriteLine("1 is less than 2");
            }

output:

1 is less than 2  

if-else statement

The ‘else’ statement is used if you want to have some code executed if the main ‘if’ statement expression is false.

Syntax of ‘if-else statement’

            if (expression)
            {
                // execute this code block if the expression is true 
            }

            else
            {
                // execute this code block if the expression is false 
            }

The ‘else’ statement can appear only one time in an ‘if-else’ statement.

The else statement cannot contain any expression. The code in the else block will be executed if the ‘if’ statement expression is false.

Example of ‘if-else statement’

            int i = 0;
            int j = 10;
            if (i > j)
            {
                Console.WriteLine("i is greater than j");
            }
            else
            {
                Console.WriteLine("i is either equal to or less than j");
            }

i is equal or less than j

else if statement

The ‘else if’ statement can be used to check another condition in the else expression.

Syntax of ‘else if statement’

            if (boolean expession)
            {
                // execute the code if the statement is true
            }
            else if (boolean expession)
            {
                // execute the code if the statement is true
            }
            else
            {
                // execute the code if all statement above are false
            }

The code will be executed if the expression in the main ‘if’ statement is false and the expression in the ‘if else ‘ statement is true.

Example of ‘else if statement’

            int i = 5;
            int j = 10;
            if (i > j)
            {
                Console.WriteLine("i is greater than j");
            }
            else if (i < j)
            {
                Console.WriteLine("i is less than j");
            }
            else
            {
                Console.WriteLine("i is equal to j");
            }

‘else if’ statement can be used multiple time in a single if statement chain.

Nested ‘if’ statement

In C#, ‘nested if’ statements are supported and widely used in programs. The ‘nested if’ statement is an ‘if’ statement inside another if statement.

        static void Main(string[] args)
        {
            int i = 5;
            if (i > 0)
            {
                if (i <= 10)
                {
                    Console.WriteLine("i is less than 100");
                }
                else
                {
                    Console.WriteLine("i is greater than 100");
                }
            }
            Console.ReadKey();
        }

Output

i is less than 100

Also, nested ‘if- else’ statement is supported in C#. The nested ‘if’ statements are used when you want to validate more than one expression, or when you have some cascading statements.


Example I

        static void Main(string[] args)
        {
            int number = 1;
            if (number < 5)
            {
                Console.WriteLine("{0} is less than 5", number);
            }

            Console.ReadKey();
        }

Output

2 is less than 5

Example II

        static void Main(string[] args)
        {
            int i = 10;
            if (i > 0)
            {
                Console.WriteLine("i is greater than 0");
                if (i > 7)
                {
                    Console.WriteLine("i is greate than 7");
                }
                else
                {
                    Console.WriteLine("i is less than 7");

                }
            }

            Console.ReadKey();
        }

Output:

i is greater than 0
i is greater than 7

Leave a Reply