Home » Swift if-else-if Statement

Swift if-else-if Statement

by Online Tutorials Library

Swift if-else-if Statement

The if-else-if statement is used when you want to execute one block of code among many.

Syntax:

As long as the test expressions are true, the if statements are executed from top to bottom and then the control of program jumps outside if-else-if ladder.

If the test expression is false, code inside the else is executed.

Example:

Output:

This number is 0.  

You can see that the constant number is initialized with value 0. Since if statements are executed from top to bottom, it checks the expression number > 0 which evaluates to false. It then checks the next expression number < 0 which also evaluates to false.

Hence, the statement print(“This number is 0.”) inside the else statement.


You may also like