Swift has a type known as Bool
, it is used to represent the values true
and false
.
Swift has 3 logical operators (!
, ||
, &&
) which are used to combine Bools and make expressions that produce different values.
&&
)The and operator in Swift is represented by &&
and returns true
if both values given are true
otherwise it returns false
.
When using the and operator, one Bool is placed on the right side of the &&
and another one on the left side.
true && true // true
true && false // false
||
)The or operator in Swift is represented by ||
and returns true
if at least one of values given is true
. If both of the values are false
then it returns false
.
When using the or operator one bool should be placed on the right side of the ||
and another one on the left side.
true || true // true
true || false // true
false || false // false
!
)The not operator in Swift is represented by !
and returns true
if the given Bool is false
, and returns false
if true
is given.
When using the not operator one Bool should be placed after the operator (!
).
!true // false
!false // true
()
)When working with booleans you can use explicit parentheses to decide which Bools to evaluate first. The result can differ depending on how the parentheses are used. In Swift, what is in parentheses is evaluated first.
true && false && false || true // true
true && false && (false || true) // false
Since what is in parentheses is evaluated first, in the following example, the not operator will apply to the expression inside parentheses.
!true && false // false
!(true && false) // true
You should only use parentheses when they affect the result, otherwise, should they be omitted.
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 concepts, 103 exercises, and real human mentoring, all for free.