The guard statement in Swift is used to transfer program control out of a scope if one or more conditions are not met. It is used to simplify an if statement by providing an early exit from a function or loop when a condition is not met.

For example:

func checkAge(age: Int) {
guard age >= 18 else {
print(“You must be 18 or older to use this service”)
return
}
print(“You can use this service”)
}

checkAge(age: 17) // Prints “You must be 18 or older to use this service

Leave a Reply

Your email address will not be published. Required fields are marked *