Latest web development tutorials

Swift conditional statement

Conditional statements through one or more set conditions to execute the program, execute the statement specified condition is true, execute additional statement specified condition is false.

The following figure can be a simple understanding of the execution of the conditional statement:

Swift provides the following types of conditional statements:

Statements description

if statement

if statement consists ofa Boolean expression and one or more execution statements.

if ... else statement

You can have an optionalelse statement if statement,elsestatementexecutes the Boolean expression is false.

if ... else if ... else statement

You can have an optionalelse if after if... elsestatement,else if ... else statement is often used to determine a number of conditions.

Embedded if statements

You can be embeddedif the ifstatement orelse iforelse ifthe.

switch statement

switch statement allows a variable equal to a plurality of test value.

?: Operator

We have already explained in previous chapters of theconditional operator:?, Can be used instead if ... elsestatement. Its general form is as follows:

Exp1 ? Exp2 : Exp3;

Wherein, Exp1, Exp2 and Exp3 expression. Please note that the use of the colon and location.

? Exp1 value of the expression is determined. If Exp1 is true, then the calculated value Exp2, the result is the entire? Expression. If Exp1 is false, then the calculated value Exp3, the result is the entire? Expression.