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