find and solve || findandsolve.com
Please wait.....

Nested if else Statement in C#

Consider a situation where you want to execute a given set of statements based on a condition where a number is even or odd. In this situation, you can use the if statement to programmatically decide which of the given sets of statements is to be executed.

The if statement allows you to select a set of statements from two given sets of statements based on the value of a Boolean expression, which evaluates to true or false. When the if statement is executed the expression is evaluated.

If the expression evaluates to true then one of the given set of statements is executed and if the expression evaluates to false then the other set of statements is executed.

The syntax of the if statement is as follows:


 if(expression)
{
 //Statements1
}
else{
//statements 2
}


In the preceding syntax,

if: Refers to the if the keyword that indicates an if statement.

expression: Refers to the Boolean expression that acts as a condition for the if statement.

{}: Refers to a pair of curly braces, which is required when you want to execute a block of statements in the if statement. A block of statements refers to a group of multiple statements that are treated as a single statement.

statemtns1: Refers to the set of statements that is executed when an expression evaluates to true.

The set of statements can be a single statement or a block of statements.

statement2: Refers to the set of statements that is executed when the expression evaluates to false.

This set of statements is also optional and can be a single statement of the block of statements.


The preceding syntax is the syntax of a simple if statement that involves only one if clause and one else clause. However, C# allows you to nest one if statement within another if statement. You can nest multiple if statements in the if or else clause of the if statements. Note that in such cases, the else clause corresponds to the last or the most recent if statement. This is because the compiler associates the else clause with the last unmatched if statement (if statement without an else clause).

The syntax of nested if statements are as follows:

If(expression)
 {
       If(expression2)
        {
         //statemtns1
        }
     else
        {
        //statements2
         }
   }
else
{
  If(expression3)
    {
       //statements3
    }
  else
     {
       //statements4
    }
 
}

In the preceding syntax:

if : Refers to the if keyword that indicates an if statement

else: Refers to the else keyword that indicates the else clause of the if statement

expression1,expression2,expressioin3 :Refer to the Boolean expression of the if statements

statements1,statements2, statements3, statements4 : Refer to the sets of statements

For example 

Check odd or event number  by c# console

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number :");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number%2==0)
            {
                Console.WriteLine(number + "  is an even number");
            }
            else
            {
                Console.WriteLine(number + "is an odd number");
            }
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int enteredNumber;
            Console.WriteLine("Enter the number rang of 0 to 5");
            enteredNumber = Convert.ToInt32(Console.ReadLine());
            if (enteredNumber==0)
            {
                Console.WriteLine("you entered ZERO");
            }
           else if (enteredNumber==1)
            {
                Console.WriteLine("you entered ONE");
            }
            else if (enteredNumber == 2)
            {
                Console.WriteLine("you entered TWO");
            }
            else if (enteredNumber == 3)
            {
                Console.WriteLine("you entered THREE");
            }
            else if (enteredNumber == 4)
            {
                Console.WriteLine("you entered FOUR");
            }
            else if (enteredNumber ==5)
            {
                Console.WriteLine("you entered FIVE");
            }
            else
            {
                Console.WriteLine("Your number is grater then 5");
            }
            
            Console.ReadKey();
        }
    }
}

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response