What the and operator does
Sometimes one test isn't enough. The logical and operator, written &&, joins two conditions and gives true only when both of them are true. If either side is false, the whole expression is false.
10 == 10 && 7 < 10 // true
Both parts are true here: 10 == 10 and 7 < 10. So the combined expression is true.
| Left | Right | Left && Right |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Using it in an if statement
The combined condition goes into an if, with no parentheses needed:
let x = 10
if x != 7 && 12 < x {
print("This is true!")
}
The first part, x != 7, is true. The second, 12 < x, is false because 12 is not less than 10. One false side makes the whole condition false, so the block is skipped and the program prints nothing.
Details that matter in Swift
- Both sides must be
Bool. Each operand is a complete comparison. Writingx == 1 && 2is an error, because2is not a Boolean. - Short-circuiting. Swift evaluates the left side first, and when it is
false, the right side is never evaluated, since the answer is already known. This makes guards like!items.isEmpty && items[0] > 0safe: with an empty array, the check on the left fails and the indexing on the right never happens. - No chained comparisons. To test that
xis between two limits, write1 < x && x < 10. The form1 < x < 10does not compile. - The symbol, not the word. Swift has no
andkeyword.if weather == "sunny" and temperature > 85fails withexpected '{' after 'if' condition. - Precedence.
&&binds tighter than||(or). Sotrue || false && falseistrue, while(true || false) && falseisfalse. Add parentheses when you mix them. - Not. The operator
!flips a Boolean:!(3 > 2)isfalse.
The exercise
The code declares two constants:
let weather = "rainy"
let temperature = 90
The program should print Going to the beach only when the weather is "sunny" and the temperature is above 85. In any other case it should print Learning to code.
- Write the
if. Test the weather withweather == "sunny"and the temperature withtemperature > 85, and join them with&&. - Print the beach message inside the braces. It appears only when both checks pass.
- Add the
else. It prints"Learning to code", so the program still answers when either check is false.
let weather = "rainy"
let temperature = 90
if weather == "sunny" && temperature > 85 {
print("Going to the beach")
} else {
print("Learning to code")
}
The temperature of 90 passes its check, but the weather is "rainy", so the first check fails. The program prints Learning to code. Changing the weather to "sunny" makes both checks true, and it prints Going to the beach. Changing only the temperature to 80 with sunny weather fails the second check and also prints Learning to code.
Common mistakes
- Using one
&. A single ampersand is a different operator, the bitwise and, and it does not combine two Boolean conditions. - Dropping the second comparison.
weather == "sunny" && temperaturefails to compile, becausetemperaturealone is anInt. - Comparing strings with the wrong case.
"Sunny"is not equal to"sunny". - Using
>=by habit. The task says above 85, so a temperature of exactly 85 doesn't qualify.
Key takeaways
a && bis true only when bothaandbare true.- Each side is a full Boolean expression, and the right side is skipped when the left is false.
- Use
&&for ranges and for rules with several requirements, and add parentheses when mixing it with||.
