C++ Decision Making

, a

, a

C++ 中的判断语句

Decision Statements

, a

statementDescription
if statementaif statement:
if...else statementaif statementcan be followed by an optionalelse statementThe else statement is executed when the boolean expression is false.
Nested if StatementsYou can in aiforelse ifuse another within the statementiforelse ifstatement.
switch statementaswitchThe statement allows testing a variable for equality with multiple values.
Nested switch StatementsYou can in aswitchuse another within the statementswitch statement.

? : operator

We have already explained in the previous chaptersConditional Operator ? :, can be used to replaceif...elsestatement. Its general form is as follows:

Exp1 ? Exp2 : Exp3;

Here, Exp1, Exp2, and Exp3 are expressions. Note the use and position of the colon.

The value of the ? expression is determined by Exp1. If Exp1 is true, then the value of Exp2 is evaluated, and the result is the value of the entire ? expression. If Exp1 is false, then the value of Exp3 is evaluated, and the result is the value of the entire ? expression.

other extensions