Notes Flashcards

1
Q

Constant Boolean

A

It’s smart to use constants in general because they don’t allocate as much space and you don’t have to keep track of them but it does seem like a Boolean would need to have the ability to change.

One good use of a constant Boolean is in a situation where you have to calculate something and only care about if it meets a requirement.

Ex. I want to know if today is a Sunday and if the temperature is greater than 70 degrees. You could then use that constant Boolean in other places. I could probably find some examples

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

Ternary Operations

A

Let’s make it simple:
a ? b : c - you can read as “ if (?) a is true, then b, else (:) c “

or in some other case,
(a<5) ? b : c - you can read as “ if (?) a is less then 5, then b, else (:) c “

an example is name=age>12?”teen”:”child” which sets name to “teen” if age is >12 and “child” otherwise. they can be string together, meaning a=b?c:d?e:f is valid.

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

!

A

”!” reads “not” and inverts a Boolean value, so if it was true it makes it false and if it was false it makes it true

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

i++

A

i++ is the same as i+=1 or i=I+1

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

a && b

A

Both have to be true

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

a || b

A

One has to be true

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

Optionals

A

In Swift if you want to create a variable without assigning an initial value (when it is declared), it has to be created as “optional”. code, its value automatically defaults to nil, unless an initial default value is provided.

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

Switch

A

Use the switch statement as an alternative to the if statement for multiple potential states.

switch distance { 
case 0:
   print("not a valid distance") 
case 1,2,3,4,5: 
   print("near")
default: 
   print("too far")
 }

Every switch statement must be exhaustive, i.e. take every possible value into consideration

ou can define a default catch-all case to cover any values that are not explicitly addressed. Indicate the catch-all case by using the keyword default. This always appears last.

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

Repeat-While

A

The repeat-while loop is the alternate while loop. It first makes a single pass through the loop block, then considers the loop’s condition, and repeats the loop until the condition shows as false.

repeat {
x -= 1
} while x > 0

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

The for-in Loop

A

Use the for-in loop to iterate over a sequence, such as ranges of numbers, items in an array, or characters in a string.
The following example prints the first few entries in the five-times-table:

for index in 1...5 {
   print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Control Transfer

A

Control transfer statements alter the code execution by transferring control from one piece of code to another. Swift’s four control transfer statements are continue, break, fallthrough, and return (which will be discussed in the upcoming lessons

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

Continue

A

The continue statement stops the loop, then restarts it at the beginning of its next cycle.
The example below shows how to use the continue statement to skip over even numbers.

for num in 1...10 {
   if num%2 == 0 {
      continue
   }
   print(num)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Break

A

Use the break statement to immediately end the execution of an entire control flow statement. Also, the break statement is used within a switch statement or a loop statement to terminate its execution sooner than would otherwise be the case.

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

Break in a Loop Statement

A

When a break statement is used within a loop statement, the loop’s execution immediately stops. Control transfers to the first line of code following the loop’s closing brace (}). The current iteration’s remaining code is skipped, and no further iterations of the loop are initiated.
For example, you can have a loop that breaks out when the value of a becomes less than that of b:

var b = 7
var a = 10
while a > 0 {
   if(a < b) {
     break
   }
   a-=1
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Break in a Switch Statement

A

A break causes a switch statement to end its execution immediately, and transfers control to the first line of code that follows the switch statement’s closing brace (}).

var a = 5
var letter = "X"
switch a {
   case 1:
     letter = "A"
   case 2: 
     letter = "B"
   default: 
     break
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Fallthrough

A

In Swift, switch statements do not fall through the bottom of each case into the next. Instead, the entire switch statement completes its execution when the first matching case is completed.
By contrast, C requires insertion of an explicit break statement at the end of every switch case to prevent fallthrough. By eliminating default fallthrough, Swift allows for more concise and predictable switch statements in comparison with C, and thus avoids inadvertently executing multiple switch cases.

In cases that require C-style fallthrough behavior, use the fallthrough keyword on a case-by-case basis. The example below uses fallthrough to create a number’s textual description.

let myInt = 5
var desc = "The number \(myInt) is"
switch myInt {
   case 2, 3, 5, 7, 11, 13, 17, 19:
      desc += " a prime number, and also"
      fallthrough
   default:
     desc += " an integer."
}
print(desc)
17
Q

Empty Strings

A

An empty String value can be created as the starting point for a longer string. To do this, either assign an empty string literal to a variable or initialize a new String instance using initializer syntax:

var emptyString = "" // empty string literal
var anotherEmptyString = String() // initializer syntax