Swift has a logical operator &&. The && operator is very useful, it takes in two values. For example, a && b works like:
a is checked if it is true or false.b if a is true. If a is false, return falsetrue if a and b are true and false otherwise.The && operator is equivalent to this function (it doesn't use any shortcuts so it is easier to understand for beginners):
func and(_ a: Bool, _ b: Bool) -> Bool {
if (a == false) {
return false; // stops function and returns false
}
else if (b == true) { // "a" must be true because it would return otherwise
return true;
} else {
return false;
}
}So, && will only return true for true && true.
Make a function using &&.
and(true, false) ➞ false
and(true, true) ➞ true
and(false, true) ➞ false
and(false, false) ➞ falseN/A