什么是 C# 中的控制语句?


在 C# 中,程序控制流的流程 由控制语句指定。其中包括以下内容 −

if 语句

if 语句由布尔表达式后跟一条或多条语句组成。

以下是语法 −

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
}

if-else 语句

if 语句后面可以跟一个可选的 else 语句,当布尔表达式为假时执行此语句。

以下是语法 −

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
} else {
   /* statement(s) will execute if the boolean expression is false */
}

for 循环

重复执行一系列语句多次,并缩写管理循环变量的代码。

以下是语法 −

for ( init; condition; increment ) {
   statement(s);
}

while 循环

不断重复一条语句或一组语句,直到给定条件为真。在执行循环主体之前,它将测试条件。

以下是语法 −

while(condition) {
   statement(s);
}

do…while 循环

类似于 while 语句,不同之处在于它在循环体末尾测试条件。

以下是语法 −

do {
   statement(s);
} while( condition );

更新时间: 2020 年 6 月 20 日

6K+ 浏览次数

开启你的职业生涯

完成课程后取得认证

开始
广告
© . All rights reserved.