Game Development Reference
In-Depth Information
Flow of Control
Swift provides the most common methods to control the flow of a program, including the
if
and
switch
statements to support branching and the
for
and
while
statements to
support looping. They are all similar to their Objective-C counterparts, but there are a few
differences.
if
The Swift
if
statement is much like Objective-C's
if
statement, with the exception that
the parentheses surrounding the Boolean expression is optional. You can include them if
you like, but they are not required. To Swift, the following two
if
statements are identic-
al:
var count = 10
if (count < 7){
println("less than 7")
}
else if (count > 7) {
println("greater than 7")
}
else {
println("equals 7")
}
if count < 7 {
println("less than 7")
}
else if count > 7 {
println("greater than 7")
}
else {
println("equals 7")
}
switch