我们如何在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 新房祝福语简短10字
14 长辈新年祝福语大全简短
15 新年给大家简短祝福语
16 生日祝福语简短独特姑姑
17 芭比的婚礼祝福语简短
18 邀请新人祝福语简短英文