if-else运用及技巧(C# 参考)
if-else
if
语句基于布尔表达式的值来识别运行哪个语句。 在下面的示例中,bool
变量condition
已被设置为true
,然后被签入到了if
语句。 输出为The variable is set to true.
。
bool condition = true;if (condition)
{Console.WriteLine("The variable is set to true.");
}
else
{Console.WriteLine("The variable is set to false.");
}
- 你可以通过将本主题中的示例放入控制台应用的
Main
方法中来运行它们。 - C# 中的
if
语句可以采用两种形式,如以下示例所示。
// if-else statement
if (condition)
{then-statement;
}
else
{else-statement;
}
// Next statement in the program.// if statement without an else
if (condition)
{then-statement;
}
// Next statement in the program.
- 在
if-else
语句中,如果condition
计算结果为 true,则then-statement
将运行。 如果condition
为 false,则else-statement
将运行。 由于condition
不能同时为 true 和 false,因此,then-statement
语句的else-statement
和if-else
永远不能同时运行。then-statement
或else-statement
运行后,控件将转移到if
语句之后的下一个语句。 - 在不包括
if
语句的else
语句中,如果condition
为 true,则then-statement
将运行。 如果condition
为 false,则控件将转移到if
语句之后的下一个语句。 then-statement
和else-statement
都可由单个语句或包含在括号中 ({}
) 的多个语句组成。 对于单个语句,括号是可选的,但建议选择。then-statement
和else-statement
中的语句可为任何类型,包括嵌套在原始if
语句中的另一个if
语句。 在嵌套的if
语句中,每个else
子句都属于上一个无相应if
的else
。 在下面的示例中,如果Result1
和m > 10
计算结果都为 true,则将显示n > 20
。 如果m > 10
为 true 但n > 20
为 false,则将显示Result2
。
// Try with m = 12 and then with m = 8.
int m = 12;
int n = 18;if (m > 10)if (n > 20){Console.WriteLine("Result1");}else{Console.WriteLine("Result2");}
- 相反,如果你希望在
Result2
为 false 的时候显示(m > 10)
,则可以通过使用括号来指定此关联,以建立嵌套的if
语句的开头和结尾,如以下示例所示。
// Try with m = 12 and then with m = 8.
if (m > 10)
{if (n > 20)Console.WriteLine("Result1");
}
else
{Console.WriteLine("Result2");
}
- 如果条件
(m > 10)
的计算结果为 false,则显示Result2
。
示例
- 在下例中,当通过键盘输入字符时,该程序将使用嵌套的
if
语句来确定输入的字符是否为字母字符。 如果输入的字符是字母字符,则程序将检查输入的字符是大写还是小写。 每种情况都会显示一条消息。
Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{if (Char.IsLower(c)){Console.WriteLine("The character is lowercase.");}else{Console.WriteLine("The character is uppercase.");}
}
else
{Console.WriteLine("The character isn't an alphabetic character.");
}//Sample Output://Enter a character: 2
//The character isn't an alphabetic character.//Enter a character: A
//The character is uppercase.//Enter a character: h
//The character is lowercase.
- 你也可以将
if
语句嵌套到 else 块中,如以下部分代码所示。 示例将if
语句嵌套在两个 else 块和一个 then 块中。 注释指定每个块中哪些条件为 true 哪些条件为 false。
// Change the values of these variables to test the results.
bool Condition1 = true;
bool Condition2 = true;
bool Condition3 = true;
bool Condition4 = true;if (Condition1)
{// Condition1 is true.
}
else if (Condition2)
{// Condition1 is false and Condition2 is true.
}
else if (Condition3)
{if (Condition4){// Condition1 and Condition2 are false. Condition3 and Condition4 are true.}else{// Condition1, Condition2, and Condition4 are false. Condition3 is true.}
}
else
{// Condition1, Condition2, and Condition3 are false.
}
- 下面的示例确定了输入的字符是一个小写字母,还是大写字母,还是一个数字。 如果所有三个条件都为 false,该字符不是字母数字字符。 此示例显示了每种情况的消息内容。
Console.Write("Enter a character: ");
char ch = (char)Console.Read();if (Char.IsUpper(ch))
{Console.WriteLine("The character is an uppercase letter.");
}
else if (Char.IsLower(ch))
{Console.WriteLine("The character is a lowercase letter.");
}
else if (Char.IsDigit(ch))
{Console.WriteLine("The character is a number.");
}
else
{Console.WriteLine("The character is not alphanumeric.");
}//Sample Input and Output:
//Enter a character: E
//The character is an uppercase letter.//Enter a character: e
//The character is a lowercase letter.//Enter a character: 4
//The character is a number.//Enter a character: =
//The character is not alphanumeric.
- 正如 else 块或 then 块中的语句可以是任何有效的语句一样,你可以将任何有效的布尔表达式用于此条件。 可使用
!
、&&
、||
、&
、|
和^
等逻辑运算符来创建复合条件。 下面的代码演示了示例。
// NOT
bool result = true;
if (!result)
{Console.WriteLine("The condition is true (result is false).");
}
else
{Console.WriteLine("The condition is false (result is true).");
}// Short-circuit AND
int m = 9;
int n = 7;
int p = 5;
if (m >= n && m >= p)
{Console.WriteLine("Nothing is larger than m.");
}// AND and NOT
if (m >= n && !(p > m))
{Console.WriteLine("Nothing is larger than m.");
}// Short-circuit OR
if (m > n || m > p)
{Console.WriteLine("m isn't the smallest.");
}// NOT and OR
m = 4;
if (!(m >= n || m >= p))
{Console.WriteLine("Now m is the smallest.");
}
// Output:
// The condition is false (result is true).
// Nothing is larger than m.
// Nothing is larger than m.
// m isn't the smallest.
// Now m is the smallest.
有关详细信息,请参阅 C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。
if-else运用及技巧(C# 参考)相关推荐
- JavaScript 工作原理之十一-渲染引擎及性能优化小技巧
原文请查阅这里,略有删减,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第十一章. 迄 ...
- 调试笔记--keil 测量周期小技巧
调试笔记–keil 测量周期小技巧 本文参考安富莱专题教程第7期 http://www.armbbs.cn/forum.php?mod=viewthread&tid=87176&ext ...
- 克服过拟合和提高泛化能力的20条技巧和诀窍
克服过拟合和提高泛化能力的20条技巧和诀窍 你是如何提升深度学习模型的效果? 这是我经常被问到的一个问题. 有时候也会换一种问法: 我该如何提高模型的准确率呢? --或者反过来问: 如果我的网络 ...
- JavaScript 工作原理之十一-渲染引擎及性能优化小技巧 1
原文请查阅这里,略有删减,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第十一章. 迄 ...
- [转]25个增强iOS应用程序性能的提示和技巧
在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...
- 大数据分析有哪些技巧
对于大数据而言,以业务为中心的方式分析它的挑战是实现这一目标的唯一方法,即确保公司制定数据管理策略.但是,有一些技术可以优化您的大数据分析,并最大限度地减少可能渗入这些大型数据集的"噪音&q ...
- 优化大数据分析的五个小技巧
数据正在变得越来越重要,一些企业甚至把数据当成自己的"天".近年来越来越多的公司已经意识到数据分析可以带来的价值,并且已经跳上了大数据旅行车.实际上,现在所有的一切都在被监控和测量 ...
- css鼠标拖拉卡顿_66个值得收藏的CSS开发技巧
来源:https://mp.weixin.qq.com/s/hEWqQYfrbTrEJ4CIa16DCQ 作者:前端宇宙 何为技巧,意指表现在文学.工艺.体育等方面的巧妙技能.代码作为一门现代高级工艺 ...
- 邮件营销 – 电子邮件营销的20个技巧
技巧一:解决一个问题 在很多时候,邮件营销人员总是在没有一个适当的邮件营销计划的情况下,就开始迫不及待的向他们的列表用户发送邮件.而更好的处理营销邮件的方式则是:你需要首先确定一个你正需要解决的业务问 ...
- OneNote应用技巧
OneNote应用技巧 目录 - OneNote应用技巧 - 1.快捷键 - 1.1.OneNote快捷键 - 1.2.创建编号和项目符号 ...
最新文章
- Nature Method :Rob Knight发布Striped UniFrac算法轻松分析微生物组大数据
- linux yum install 与 yum groupinstall 区别
- python序列类型-python-序列类型
- laravel blade模板
- PHP 源码 —— is_array 函数源码分析
- android 截屏_图文小编福利:Apowersoft专业截屏王
- Java基础---键盘录入工具(Scanner类)
- 吴恩达深度学习1.4笔记_Neural Networks and Deep Learning_深层神经网络
- 为了方便远程登录写的简单expect脚本
- MATLAB车牌识别系统
- 易语言网络验证UI界面源码
- 代理软件使用拨号不可用,使用wifi正常使用 解决 win10
- 扫清盲点,如何正确的从HttpClient 3.x系统升级到HttpClient 4.x
- r语言 linux plot,详解R语言plot函数参数合集
- carsim/trucksim获取轮胎侧偏刚度、纵向刚度
- 2020蚂蚁森林自动收能量-保持更新
- mysql SELECT/UPDATE command denied to user 'root'@'localhost' for table 'XXX' 报错1142处理
- 动手学:深度学习Task2
- Gem5模拟器,详解官网教程的statistics and output(三)
- Systrace 流畅性实战 2 :案例分析: MIUI 桌面滑动卡顿分析