C# nesting if statement


Release date:2023-08-24 Update date:2023-10-13 Editor:admin View counts:444

Label:

C# nesting if statement

In C #, nesting if-else statement is legal, which means that you can use a if or else if use another within the statement if or else if statement.

Grammar

Nesting in C # if syntax of the statement:

if( boolean_expression 1)
{
   /* Execute when Boolean expression 1 is true */
   if(boolean_expression 2)
   {
      /* Execute when Boolean expression 2 is true */
   }
}

You can nest else if...else , mode and nesting if sentence is similar.

Example

using System;
namespace DecisionMaking
{

    class Program
    {
        static void Main(string[] args)
        {
            //* Definition of Local Variables */
            int a = 100;
            int b = 200;
            /* Check Boolean conditions */
            if (a == 100)
            {
                /* If the condition is true, check the following conditions */
                if (b == 200)
                {
                    /* If the condition is true, output the following statement */
                    Console.WriteLine("The value of a is 100, and the value of b is 200");
                }
            }
            Console.WriteLine("The exact value of a is {0}", a);
            Console.WriteLine("The exact value of b is {0}", b);
            Console.ReadLine();
        }
    }
}

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

The value of a is 100, and the value of b is 200
The accurate value of a is 100
The accurate value of b is 200

Powered by TorCMS (https://github.com/bukun/TorCMS).