Csharp 简明教程

C

决策制定结构要求程序员指定一个或多个条件,以便程序进行评估或测试,如果确定条件为真,则执行一条或多条语句,并且如果确定条件为假,则可选地执行其他语句。

Decision making structures requires the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

以下是大多数编程语言中常见的典型决策结构的一般形式 −

Following is the general form of a typical decision making structure found in most of the programming languages −

decision making

C# 提供以下类型的决策制定语句。单击以下链接以查看其详细信息。

C# provides following types of decision making statements. Click the following links to check their detail.

Sr.No.

Statement & Description

1

if statementAn if statement consists of a boolean expression followed by one or more statements.

2

if…​else statementAn if statement can be followed by an optional else statement, which executes when the boolean expression is false.

3

nested if statementsYou can use one if or else if statement inside another if or else if statement(s).

4

switch statementA switch statement allows a variable to be tested for equality against a list of values.

5

nested switch statementsYou can use one switch statement inside another *switch * statement(s).

The ? : Operator

我们在上一章中介绍了 conditional operator ? : ,它可以用来替换 if…​else 语句。它具有以下通用形式 -

We have covered conditional operator ? : in previous chapter which can be used to replace if…​else statements. It has the following general form −

Exp1 ? Exp2 : Exp3;

其中 Exp1、Exp2 和 Exp3 是表达式。请注意冒号的用处和位置。

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.

一个 ? 表达式的值由以下方式确定:Exp1 经过评估。如果为真,则 Exp2 经过评估,并成为整个 ? 表达式的值。如果 Exp1 为假,则 Exp3 经过评估,它的值成为表达式的值。

The value of a ? expression is determined as follows: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.