[Swift]Conditional and Loops

0
23

Swift Programming 


  • Conditionals
  • Loops

Keywords : Swift, Conditionals, Loops,  스위프트, 조건문, 반복문, if, else, else if, switch, for, while, stride

Conditionals

In Swift, in order to compare values, we use if(else if, else) and switch. Since Swift has strict rule of type matching, we can’t use binary number such as 0/1 as true/false. First of all, let’s have a look at the operators that you are going to use to compare values with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// AND
true && true // true
true && false // false
false && true // false
false && false // false
// OR
true || true // true
true || false // true
false || true // true
false || false // false
!true // false
!false // true
!(true && true// false
!(true || false// false
!(false || false// true
cs

*NOTE – Swift also provides two identity operators (=== and !===), which you use to test whether two object references both refer to the same object instance. For more information, go to the link here.

IF – ELSE

1
2
3
4
5
6
7
8
9
10
11
if CONDITION {
    STATEMENT
    STATEMENT
    …
    STATEMENT
else {
    STATEMENT
    STATEMENT
    …
    STATEMENT
}
cs

Please have a look at the Basic Statement above with the simple code below at the same time. By using parenthesis{}, we put the statements that we want to declare in the block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var age = 13
var student = “”
if age >= 8 && age < 14 {
    student = “Primary Schooler”
else if age < 17 {
    student = “Middle Schooler”
else if age < 20 {
    student = “High Schooler”
else {
    student = “etc”
}
student // Primary Schooler
cs

Switch

‘Switch’ which is being used for just matching if it’s same value in other languages. ‘switch’ in Swift can do pattern matching like below. Once you get used to use it, it will be easy and useful to make the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var age = 13
var student = “”
switch age {
case 8..<14:
    student = “Primary”
case 14..<17:
    student = “Middle”
case 17..<20:
    student = “High”
default:
    student = “etc”
}
student // Primary Schooler
cs

As you may see above, 8..<14 means 8~13 and we don’t have to use ‘break;’ to each case:.


Loops

There are two major things for Loops. It’s for and while. As you may figure out with the word, loop It’s a loop to repeatedly run the statements that you mentioned in true condition.

while

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Normal while-form
var i = 1
while i <= 10 {
    print(i)
    i = i + 1
}
//It’s at Least one execution
= 1
repeat {
    print(i)
    i = i + 1
while i > 10
cs

The plain while form executes the statement after looking at the condition if it’s true or not but the repeat-while form like do-while form in C or Java executes at least once before checking the condition . Therefore, the second paragraph will be finalized with i having value 2 even though condition is not correct.

for

The basic concept of for form is same as while. Let’s just go for the examples below. I think you can use whatever you think it’s convenient and efficient.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Basic form
for value in range {
statements
}
// prints 1-10
for i in 1...10 {
    print(i)
}
// prints 0-9
for i in 0..<10 {
    print(i)
}
cs

 

In order to sequentially read the values in Array and Dictionary, statement in for form will be below. If you want to find detail information about array and dictionary please go to [Swift]Array and Dictionary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Array and Dictionary Declaration
var languages: [String= [“Korean”“English”“Spanish”]
var capitals: [StringString= [
“Korea”“Seoul”,
“Japan”“Tokyo”,
“China”“Beijing”,
]
for language in languages {
    print(“I do speak \(language)!”)
}
for (country, capital) in capitals {
    print(“Capital of \(country) is \(capital).”)
}
cs

When lowerBound is greater than upperBound, we have to use .reversed() function like below. Otherwise, your code will be crashed.

1
2
3
4
5
6
7
8
9
10
11
// this will crash – don’t do it! 🙂
for i in 10...1 {
    print(i)
}
// this will print the numbers from 10 to 1
for i in (1...10).reversed() {
    print(i)
}
cs

There is additionally stride(from: , to: , by: ) function. Please use it referring to the code below.

1
2
3
4
5
6
7
8
9
10
11
12
//stride(from: 1, to: 10, by: 2)  –> 1, 3, 5, 7, 9
//stride(from: 1, to: 2, by: 0.1) –> 1.0, 1.1 … 1.9
for i in stride(from: 1, to: 10, by: 3) {
    print(i)
}
for i in stride(from: 3, to: 1, by: 1) {
    print(i)
}
// prints: 3 2 1
cs

LEAVE A REPLY