C# 中控制语句是什么?


C# 中的控制语句指定了程序控制流。它包括以下几种:-

if 语句

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

以下是语法:-

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

if-else 语句

if 语句可以后接一个可选的 else 语句,该语句在布尔表达式为 false 时执行。

以下是语法:-

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 );

更新于: 20-6 月-2020

6 千次 + 浏览量

开启您的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.