Loops Flashcards

1
Q

What is a for-in loop used for?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the random(in:) method work in Swift?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a while loop used for?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is a repeat-while loop different from a while loop?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should you use a repeat-while loop?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Boolean type in Swift?

A

A Boolean type returns a value of either true or false.
Example in Swift:
let isLoggedIn = true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the ‘equal to’ (==) operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the ‘greater than’ (>) operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the ‘less than’ (<) operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the ‘greater than or equal to’ (>=) operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the ‘less than or equal to’ (<=) operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the ‘not equal to’ (!=) operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the logical AND (&&) operator?

A

The logical AND operator returns true if both conditions are true.
Example in Swift:
let result = (5 > 3 && 10 > 7) // true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the logical OR (||) operator?

A

The logical OR operator returns true if at least one condition is true.
Example in Swift:
let result = (5 > 3 || 3 > 10) // true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the NOT (!) operator?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Exclusive OR (XOR) operator?

A

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

17
Q

What is encryption?

A

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.

18
Q

What is an if statement in Swift?

A

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!”)
}

19
Q

What is an else statement in Swift?

A

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!”)
}

20
Q

What is an else-if statement in Swift?

A

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.”)
}

21
Q

How are Booleans used in conditional statements?

A

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.”)
}

22
Q

How does the logical AND (&&) operator work in Swift?

A

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.”)
}

23
Q

How does the logical OR (||) operator work in Swift?

A

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.”)
}