Home » Swift if-else Statement

Swift if-else Statement

by Online Tutorials Library

Swift if-else Statement

The Swift if-else statement contains two statements: if statement and else statement.

If the test evaluation is true, the if statement is executed and if the the test evaluation is false, then the else statement is executed.

Syntax:

Example:

Output:

This is a positive number.  This will be executed anyways.  

In the above program, the constant number is positive (5) so, the test case is evaluated true and statement inside the code block of if is executed.

Let us change the constant number value to negative (-5) and the test condition is same.

Output:

This is not a positive number.  This will be executed anyways.  

You can see that it executes the statement inside the code block of else.


You may also like