Home » Swift Gaurd Statement

Swift Gaurd Statement

by Online Tutorials Library

Swift Gaurd Statement

Swift Guard statement is used as a substitute of Swift if statement. Guard statement provides benefits over if statement to control the program flow and write simple and clean code.

Syntax:

  • Here, expression is a Boolean expression which either evaluated to true or false.
  • If the expression is evaluated to false, it will execute the statements inside the guard.
  • If the expression is evaluated to true, it will skip the execution of the statements inside the guard.

Note: The guard statement must contain a control statement return, break, continue or throw at the end of the code.

Example:

Output:

Condition is satisfied.  

Guard statement inside a function

Example 2:

In Swift, we can also use a guard statement in function.

Output:

Condition is not satisfied.  Hello after function call  

You may also like