-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconditionStatement.swift
95 lines (73 loc) · 1.66 KB
/
conditionStatement.swift
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// if statements
var name = "Alice"
if name.isEmpty == 0 {
print("Hi! Unknown")
}
print("Hi! Welcome \(name)")
// else statements
var person = "charle"
if person == "alice" || person == "bob" { // || - or logical operator; && - and operator
print("Welcome home alice and bob")
} else {
print("you are not allowed")
}
// else if statments
enum food {
case cherries, apple, carrot, cucumber, fries
}
var eat = food.cherries
if eat == .cherries || eat == .apple {
print("Let's eat some friuts")
} else if eat == .carrot || eat == .cucumber {
print("Let's eat some vegetables")
} else if eat == .fries {
print("Time for some junk food")
} else {
print("I don't want to eat anything")
}
// switch statements
var place = "new york"
switch place {
case "las vegas":
print("night life")
case "new orlando":
print("disney world")
case "alaska":
print("aurora light")
default:
print("sorry IDK about that \(place)")
}
// switch statments by enum
enum biryani {
case vegetable, chicken, lamp
}
var dish = biryani.vegetable
switch dish {
case .vegetable:
print("let's eat \(dish) biryani")
case .chicken:
print("let's eat \(dish) biryani")
case .lamp:
print("let's eat \(dish) biryani")
}
// fall through in switch statement
var exerice = "armDay"
print("remaning exercise")
switch exerice {
case "legsDay":
print("legs")
fallthrough
case "armDay":
print("arm")
fallthrough
case "backDay":
print("back")
fallthrough
default:
print("take rest")
}
// ternary conditional operators
var age = 18
var canVote = age >= 18 ? "yes" : "no" // condition ? if true : if false
var marks = 3.5
print( marks >= 3.0 ? "pass" : "fail" )