Swift provides a built-in type called Bool to represent truth values: true and false.
Swift provides three logical operators (!, &&, and ||) to combine and evaluate Boolean expressions.
&&)The and operator (&&) returns true only if both operands are true.
If either operand is false, it returns false.
Place an expression or Boolean value on each side of the && symbol:
true && true // true
true && false // false
||)The or operator (||) returns true if at least one of the operands is true.
It returns false only when both operands are false.
Place an expression or Boolean value on each side of the || symbol:
true || true // true
true || false // true
false || false // false
!)The not operator (!) inverts a Boolean value.
It returns true if the value is false, and false if the value is true.
Place the ! operator immediately before the value or expression:
!true // false
!false // true
())You can use parentheses to group expressions and control the order of evaluation. In Swift, expressions inside parentheses are evaluated first:
true && false && false || true // true
true && false && (false || true) // false
Grouping with parentheses also determines how the NOT operator applies to complex expressions:
!true && false // false (evaluates (!true) first, then false && false)
!(true && false) // true (evaluates (true && false) first to false, then !false)
Use parentheses when you need to clarify evaluation order or make complex conditions easier to read. Avoid adding unnecessary parentheses when the default precedence is already clear.
You are in process of developing the new highly appreciated game Wings Quest. In the game you are a bird that moves around and collects seeds. The player wins by picking up all the seeds. If the player comes in contact with an eagle, then the player will lose all their seeds and lose the game. There is an exception to this rule: the player can have an active power-up that makes them fly higher than other birds.
Your goal is to write some rules that will be used in the game.
In the game, the bird will get bonus points if they touch an eagle while having a power-up.
Define the function bonusPoints(powerUpActive:touchingEagle:) that takes two arguments powerUpActive, which holds if the bird has an active power-up, and the argument touchingEagle which holds if the bird is touching an eagle.
The function should return true only if the bird has a power-up active and is touching an eagle, and false otherwise.
bonusPoints(powerUpActive: false, touchingEagle: true)
// Returns false
In the game, the player gets points when picking up a seed or a power-up.
Define the function score(touchingPowerUp:touchingSeed:) that takes two arguments touchingPowerUp, which holds if the bird is touching a power-up, the argument touchingSeed which holds if the bird is touching a seed.
The function should return true if the bird is touching a power-up or a seed, and return false otherwise.
score(touchingPowerUp: true, touchingSeed: true)
// Returns true
Define the function lose(powerUpActive:touchingEagle:) that takes two arguments powerUpActive, which holds if the bird has an active power-up, and the argument touchingEagle which holds if the bird is touching an eagle.
The function should return true if the character is touching an eagle and does not have a power-up active, and return false otherwise.
lose(powerUpActive: false, touchingEagle: true)
// Returns true
Define the win(hasPickedUpAllSeeds:powerUpActive:touchingEagle:) function that takes the arguments:
hasPickedUpAllSeeds if the bird has picked up all of the seeds.powerUpActive if the bird has a power-up active.touchingEagle if the bird is touching an eagle.The function should return true if the bird has gathered all of the seeds and has not lost based on the arguments defined in part 3, and return false otherwise.
win(hasPickedUpAllSeeds: false, powerUpActive: true, touchingEagle: false)
// Returns false
Sign up to Exercism to learn and master Swift with 35 concepts116 exercises, and real human mentoring, all for free.