-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathif-else.swift
More file actions
37 lines (28 loc) · 732 Bytes
/
if-else.swift
File metadata and controls
37 lines (28 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let num = 9
// Parens are optional.
if (num < 0) {
print("num is negative")
} else if num < 10 {
print("num is single-digit") // num is single-digit
} else {
print("num is multi-digit")
}
// There are no 'truthy' conditionals.
// The condition must be a boolean expression...
if 7 % 2 == 2 {
print("7 is even") // IDE helpfully notes, "Will never be executed"
} else {
print("7 is odd") // 7 is odd
}
// ...But optionals allow for shorthand
// non-nil + assignment conditionals
var optionalString:String? = "Hello?"
if let a = optionalString {
print(a) // Hello?
}
optionalString = nil
if let b = optionalString {
print("yep")
} else {
print("nope") // nope
}