我们如何在C#中使用嵌套的if语句?
在另一个if or else if语句中使用一个if or else if语句。嵌套if语句的语法如下-
if( boolean_expression 1) {/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}
以下示例显示了C#中嵌套if语句的用法。在这里,我们有两个if语句检查两个条件。
if (a == 5) {/* if condition is true then check the following */
if (b == 10) {
/* if condition is true then print the following */
Console.WriteLine("Value of a is 5 and b is 10");
}
}
这是完整的示例。
示例
using System;namespace Demo {
class Program {
static void Main(string[] args) {
//*局部变量定义* /
int a = 5;
int b = 10;
/* check the boolean condition */
if (a == 5) {
/* if condition is true then check the following */
if (b == 10) {
/* if condition is true then print the following */
Console.WriteLine("Value of a is 5 and b is 10");
}
}
Console.WriteLine("Exact value of a is : {0}", a);
Console.WriteLine("Exact value of b is : {0}", b);
Console.ReadLine();
}
}
}
输出结果
Value of a is 5 and b is 10Exact value of a is : 5
Exact value of b is : 10
以上是 我们如何在C#中使用嵌套的if语句? 的全部内容, 来源链接: utcz.com/z/321616.html