C# Nested if Statements
In C#,Nestedif-else statements are legal, which means you can in aiforelse ifUse another within a statementiforelse ifstatement.
Syntax
In C#Nested ifThe syntax of the statement:
if( boolean_expression 1)
{
/* 当布尔表达式 1 为真时执行 */
if(boolean_expression 2)
{
/* 当布尔表达式 2 为真时执行 */
}
}
You can nestelse if...else, in the same way as nestingifsimilar to statements.
Example
Example
using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
//* Local variable definitions */
int a = 100;
int b = 200;
/* Check boolean condition */
if (a == 100)
{
/* If the condition is true, then check the condition below */
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();
}
}
}
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
//* Local variable definitions */
int a = 100;
int b = 200;
/* Check boolean condition */
if (a == 100)
{
/* If the condition is true, then check the condition below */
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 result:
a 的值是 100,且 b 的值是 200 a 的准确值是 100 b 的准确值是 200other extensions