// Playground-noun: a place where people can play
import UIKit
// For-In loop
// 1 traverse the range of numbers
for index in 1 ... 5 {
println ("\ (index) times 5 is \ (index)")
}
// 2 traverse the array
let names = ["Anna", "Alex", "Brain", "Jack"]
for name in names {
println ("hello, \ (name)")
}
// 3 traverse the dictionary
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (name, legCount) in numberOfLegs {
println ("\ (name) has \ (legCount) legs")
}
// 4 traversing characters
for character in "hello" {
println (character)
}
// For loop
for var index = 0; index <3; index ++ {
println ("index is \ (index)")
}
// While do-while
var i = 0
do {
i ++
} while (i <3)
while (i <5) {
i ++
}
// Conditional statements
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
println ("It ‘s very cold")
}
switch temperatureInFahrenheit {
case 1 ... 20:
println ("1 ... 20")
case 23, 30:
println ("come here")
fallthrough // By default, it will not enter the next judgment. Adding this keyword allows the match to directly enter the next judgment
default:
println ("default")
break // jump out of switch
}
var somePoint = (1, 1)
switch somePoint {
case let (x, y) where x == y:
println ("equal")
default:
break
}
swift-5-Process Control