我们如何在C#中使用嵌套的if语句?
在另一个iforelseif语句中使用一个iforelseif语句。嵌套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 10 Exact value of a is : 5 Exact value of b is : 10
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短