Home » Swift if Statement

Swift if Statement

by Online Tutorials Library

Swift If Statement

The if statement is a control flow statement which is used when we want to perform different actions according to some specified conditions which are either true or false.

Syntax:

Here, expression is a Boolean expression which returns either true or false.

  • If the expression is evaluated to true, statements inside the code block of if is executed.
  • If the expression is evaluated to false, statements inside the code block of if is skipped without execution.

Example: (if condition is true)

Output:

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

In the above program, constant number is initialized with a value 5. Here, test expression evaluates true so inside the body of if statement is executed.

Example: (if condition is false)

If we initialise the value with negative number like -5 and the test condition is same then the test expression will evaluate false. So, the statement inside the code block of if is skipped without execution.

Output:

This will be executed anyways.  

In the above example, you can see that the statement inside the code block of if is not executed.


You may also like