C# Nested if Statements
In C#,Nested if-else statements are legal, which means you can nest one inside a if or else if if statement inside another if or else if if statement.
Syntax
In C#, Nested if statement syntax:
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, in the same way as nesting if statements.
Example
Example
using
System
;
namespace
DecisionMaking
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
//* local variable definitions */
int
a
=
100
;
int
b
=
200
;
/* Check the Boolean condition */
if
(
a
==
100
)
{
/* If the condition is true, check the following condition */
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 code above is compiled and executed, it produces the following result:
a 的值是 100,且 b 的值是 200
a 的准确值是 100
b 的准确值是 200
C# Decision Making