Swift If Statement with AND Operator
To combine two conditions in If statement with AND logical operator, use &&
between the conditions.
The syntax to use AND logical operator with If statement is
if ( condition_1 && condition_2 ) { //code }
Example
In the following program, we will check if the number is greater than 5, and also if the number is even, in the same if statement. We shall combine the conditions using AND logical operator.
main.swift
var n = 10 if ( (n > 5) && (n % 2 == 0) ) { print("Both the conditions are true.") } else { print("Atleast one of the condition is false.") }
Output
Conclusion
In this Swift Tutorial, we learned how to combine two conditions with AND logical operator in the if boolean expression.