Loops Flashcards
What is a for-in loop used for?
A for-in loop is used when the number of iterations is known before execution. It repeats a block of code for a set range or collection.
Example in Swift:
for i in 1…6 {
print(i)
} // Rolls a dice six times
How does the random(in:) method work in Swift?
The random(in:) method generates a random number between a specified range.
Example in Swift:
let diceRoll = Int.random(in: 1…6) // Generates a random number between 1 and 6
What is a while loop used for?
A while loop is used when the number of iterations is not known, and it continues executing as long as a condition is true.
Example in Swift:
while firstDice != secondDice {
firstDice = Int.random(in: 1…6)
secondDice = Int.random(in: 1…6)
} // Rolls dice until both are equal
How is a repeat-while loop different from a while loop?
A repeat-while loop executes the block of code at least once before checking the condition, making sure the code runs before evaluating the condition.
Example in Swift:
repeat {
firstDice = Int.random(in: 1…6)
secondDice = Int.random(in: 1…6)
} while firstDice != secondDice // Rolls dice until both are equal, but always rolls at least once
When should you use a repeat-while loop?
Use a repeat-while loop when you want to execute the loop at least once before checking the condition.
Example in Swift:
repeat {
print(“Running loop at least once”)
} while false // Executes once despite the false condition
What is a Boolean type in Swift?
A Boolean type returns a value of either true or false.
Example in Swift:
let isLoggedIn = true
What does the ‘equal to’ (==) operator do?
The ‘equal to’ operator checks if two values are equal, returning true if they are equal and false if they are not.
Example in Swift:
let isEqual = (5 == 5) // true
What does the ‘greater than’ (>) operator do?
The ‘greater than’ operator checks if the value on the left is greater than the value on the right.
Example in Swift:
let isGreater = (10 > 5) // true
What does the ‘less than’ (<) operator do?
The ‘less than’ operator checks if the value on the left is less than the value on the right.
Example in Swift:
let isLess = (3 < 5) // true
What does the ‘greater than or equal to’ (>=) operator do?
The ‘greater than or equal to’ operator checks if the value on the left is greater than or equal to the value on the right.
Example in Swift:
let isGreaterOrEqual = (5 >= 5) // true
What does the ‘less than or equal to’ (<=) operator do?
The ‘less than or equal to’ operator checks if the value on the left is less than or equal to the value on the right.
Example in Swift:
let isLessOrEqual = (5 <= 10) // true
What does the ‘not equal to’ (!=) operator do?
The ‘not equal to’ operator checks if two values are not equal, returning true if they are not.
Example in Swift:
let isNotEqual = (5 != 3) // true
What is the logical AND (&&) operator?
The logical AND operator returns true if both conditions are true.
Example in Swift:
let result = (5 > 3 && 10 > 7) // true
What is the logical OR (||) operator?
The logical OR operator returns true if at least one condition is true.
Example in Swift:
let result = (5 > 3 || 3 > 10) // true
What is the NOT (!) operator?
The NOT operator inverts the Boolean value, returning true if it was false, and false if it was true.
Example in Swift:
let isFalse = !(5 == 5) // false
What is the Exclusive OR (XOR) operator?
The Exclusive OR (XOR) operator returns true if exactly one of the conditions is true, but false if both are true or both are false.
Example in Swift:
let result = (true ^ false) // true
What is encryption?
Encryption is the process of converting information into scrambled code to protect it from unauthorized access.
Example:
When paying online, encryption secures your payment details.
What is an if statement in Swift?
An if statement checks a condition and executes a block of code if the condition is true.
Example in Swift:
let isRaining = true
if isRaining {
print(“Bring an umbrella!”)
}
What is an else statement in Swift?
An else statement runs a block of code if the condition in the if statement is false.
Example in Swift:
let isRaining = false
if isRaining {
print(“Bring an umbrella!”)
} else {
print(“No need for an umbrella!”)
}
What is an else-if statement in Swift?
An else-if statement is used to check additional conditions after the initial if condition.
Example in Swift:
let position = 2
if position == 1 {
print(“You came first!”)
} else if position == 2 {
print(“You came second!”)
} else {
print(“You did not finish in the top two.”)
}
How are Booleans used in conditional statements?
Booleans evaluate to either true or false and are used to determine if a condition is met.
Example in Swift:
let coldOutside = true
if coldOutside {
print(“Put on a warm coat.”)
}
How does the logical AND (&&) operator work in Swift?
The AND (&&) operator returns true if both sides of the condition are true.
Example in Swift:
let temp = 70
if temp >= 68 && temp <= 72 {
print(“The temperature is just right!”)
} else {
print(“The temperature is too hot or too cold.”)
}
How does the logical OR (||) operator work in Swift?
The OR (||) operator returns true if at least one side of the condition is true.
Example in Swift:
let mainsPower = false
let batteryPower = true
if mainsPower || batteryPower {
print(“I can use my phone!”)
} else {
print(“Better find a charger.”)
}