Getting Started with App Development [Section 5: Control Flow] Flashcards

1
Q

code that makes decisions about what lines of code should be executed depending on results of previously executed code is called _________ ________

A

control flow

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

Operator:

==

A

Type: Comparison

Description: Two items must be equal

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

Operator:

!=

A

Type: Comparison

Description: The values must not be equal to each other

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

Operator:

>

A

Type: Comparison

Description: Value on the left must be greater than the value on the right

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

Operator:

> =

A

Type: Comparison

Description: Value on the left must be greater than or equal to the value on the right

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

Operator:

<

A

Type: Comparison

Description: Value on the left must be less than the value on the right

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

Operator:

<=

A

Type: Comparison

Description: Value on the left must be less than or equal to the value on the right

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

Operator:

&&

A

Type: Logical

Description: AND — the conditional statement on the left AND right must be true

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

Operator:

||

A

Type: Logical

Description: OR — the conditional statement on the left OR right must be true

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

Operator:

!

A

Type: Logical

Description: NOT — returns the logical opposite of the conditional statement immediately following the operator

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

An ____ ________ states that “if this condition is true, then run this block of code”

A

if statement

let temperature = 32
if temperature >= 32 {
print(”The water is boiling.”)
}
Console Output:
The water is boiling.

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

By adding an ______ _________ to an if statement, you can specify a block of code to execute if the condition is not true

A

else clause

e.g.,

let temperature = 32
if temperature >= 32 {
print(”The water is boiling.”)
} else {
print(”The water is not boiling.”)
}

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

Via using ______ _______, you can declare more blocks of code to run based on any number of conditions

A

else if

e.g.,

var finishPosition = 2

if finishPosition == 1 {
print(”Congratulations, you won the gold medal!”)
} else if finishPosition == 2 {
print(”You came in second place, you won a silver medal!”)
} else {
print(”You did not win a gold or silver medal.”)
}

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

It’s possible to invert a ______ value using the logical NOT operator, which is represented with “!”

A

Bool

e.g.,

var isSnowing = false

if !isSnowing {
print(”It is not snowing.”)
}
Console Output:
It is not snowing.

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

You can also use the logical AND operator, represented by ____, to cheque if two or more conditions are true

A

&&

e.g.,

let temperature = 70

if temperature >= 65 && temperature <= 75 {
print(”The temperature is just right.”)
} else if temperature < 65 {
print(”It is too cold.”)
} else {
print(”It is too hot.”)
}
Console Output:
The temperature is just right.

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

You’ve yet another option to check if either one of two conditions is true: the ______ operator [represented by ||]

A

logical OR

var isPluggedIn = false
var hasBatteryPower = true

if isPluggedIn || hasBatteryPower {
print(”You can use your laptop.”)
} else {
print(”😱”)
}
Console Output:
You can use your laptop.

17
Q

Switch contains another tool for control flow known as a _______ statement that’s optimal for working with many potential conditions

A

switch

18
Q

A basic switch statement takes a value with multiple options and allows you to run separate code based on each option, or _______

A

case

19
Q

You can also provide a _______ case to specify a block of code that will run in all the cases you haven’t specifically defined

A

default

e.g.,

let numberOfWheels = 2
switch numberOfWheels {
case 0:
print(“Missing something?”)
case 1:
print(”Unicycle”)
case 2:
print(”Bicycle”)
case 3:
print(”Tricycle”)
case 4:
print(”Quadcycle”)
default:
print(”That’s a lot of wheels!”)
}

20
Q

Any given ____ statement can also evaluate multiple conditions at once

A

case

e.g.,

The following code, for example, checks whether a character is a vowel or not:

let character = “z”

switch character {
case “a”, “e”, “i”, “o”, “u”:
print(”This character is a vowel.”)
default:
print(”This character is a consonant.”)
}

21
Q

When working with numbers, you can use ________ matching to cheque for inclusion in a range

A

interval

e.g.,

switch distance {
case 0…9:
print(”Your destination is close.”)
case 10…99:
print(”Your destination is a medium distance from here.”)
case 100…999:
print(”Your destination is far from here.”)
default:
print(”Are you sure you want to travel this far?”)
}

22
Q

The switch ________ is the right tool for control flow when you want to run different code based on many different conditions

A

operator

23
Q

If a certain condition is _____, you want to set a variable to one value

A

true

24
Q

If the condition is _______, you want to set the variable to a different value

A

false

e.g.,

var largest: Int

let a = 15
let b = 4

if a > b {
largest = a
} else {
largest = b
}

25
Q

many programming languages include a special operator, known as a ______ ________ operator (?:), for writing more concise code

A

ternary conditional

26
Q

The aforementioned term is also commonly referred to as the “__________ __________”

A

ternary operator

27
Q

The ternary operator has three parts

A
  1. A question with a true or false answer.
  2. A value if the answer to the question is true.
  3. A value if the answer to the question is false.
28
Q

Example of a ternary operator

A

var largest: Int
let a = 15
let b = 4

largest = a > b ? a : b

in layman’s terms: “if a > b, assign a to the largest variable; otherwise, assign b”

29
Q

Swift provides a global function to find the largest value in an easier-to-read and more concise expression

A

largest = max(a, b)