SWIFT Basics Flashcards

1
Q

What is an array?

A

Array is an ordered list e.g. var hans : [String] = [“Peter”,”Tina”,”Julia”]

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

What is a Dictionary

A

Dictionary is an unordered list var name =[“Hans”:”Bla”,”Julia”:”Bla2”] key:value

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

How do I access item of Dictionary?

A

nameoflist [“Key”]

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

How do I add item later to Dictionary

A

nameoflist [“NewKey”] = “New Value”

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

For - In Loop When used? How does it work? How does a range work? What is a closed range? What is the alternative?

A

used after array
for item in nameofvar { println(item) } // Each value in array will be assigned individually to constant here named item. Than when exectuing command printing item, it repeats print function until reaching end of array For a range use: for number in 1…10 { println(“(number) times 2 is (number*2)” } //This includes 10: for until 9 use 1..10

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

What is a While loop? How does it look like? How does the Do-While loop differ? What do I have to do that loop is not repeating over and over?

A

Used after array e.g. while onTrain { readBook()} // Code checks onTrain for True/False. If true, it keeps performing { } until false need to create after array a var index =0 to compare for true/false (while loop has index) than: while index < todo.count { println(todo[index]) } //To prevent loop repetation, need to increment index before final } incrementing write index++ Do-While loop ensures that true/ falls condition is executed last, instead of first. Used in cases where loop should be at least executed once e.g. do { } while index < todo.count

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

What is a unary operator? When to use before and when after? Is it differently used in While loop?

A

++ or – // before or after word Used before ++Name for new value, used after Name++ to use with previous values Used different loop, if after it will first assign the value and than increment the var: if prefixed it will first increment the var and than assign

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

What is For-Condition-Increment? How is it used?

A

For-Condition-Increment is used like while loop but shorter e.g. for var index = 0; index < todo.count; index++ {println(todo[index]) }

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

What are comparative Operators?

A

< less than greater than >= greater than or equal == equal != not equal === identical !== not identical

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

Control can be divided into??

A

Two types - loops and conditionals

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

What is an if statement?

A

If on train read book

else if in car listen to music

else play game

Can create chains with else if

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

What is a Switch Statement?

A
  • it is similar like if statement (case function replaces else)

Code e.g.

let cards = 1…13

for card in cards {
switch card {
case 11:
println(“Jack”)
case 12:
println(“Queen”)
default:
println(card)
} // default case needs to be used if not all defined

With switch can use power functions. e.g. define a range

case 1,11…13:
println(“Bla”)

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

What is an operator and where are they used?

A
  • Used in if statements
  • there are two types
    • Comparison Operator
      • 1==1 ; 1!=1 ; 1 > 1, 1 >= 1 etc.
    • Logical Operator (3 types)
      • && AND operator (both expression have to be true)
      • || OR operator (one of two expression needs to be true)
      • ! Not operator
    • e.g. code if distance > 5 && distance > 20 {
      println(“AND”)}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly