swift学习笔记之基本数据类型-控制流

1. 代码示例

//: Playground - noun: a place where people can play
import Cocoa

//循环语句
var words = ["A", "B", "C"]
for letter in words{
    print(letter)
}

for var i=0; i<words.count; i++ {
    print(words[i])
}

print("\n")

var i = 0, j = 5
repeat{
    print(i++)
}while(i<j)
print("\n")


//条件语句
var a = 0,b=3
if(a>b){
    print("a is greater than b")
}else{
    print("b is greater than a")
}

var d = 1
switch(d){
case 1:
    print("1")
case 2:
    print("2")
default:
    print("nothing")
}
print("\n")

//控制传递
//1. switch默认带有break的效果
//2. ffallthrough意味着将会执行完后面的case语句
switch(d){
    case 1:
    print("1")
    fallthrough
    case 2:
    print("2")
    default:
    print("nothing")
}
2. 运行结果

swift学习笔记之基本数据类型-控制流_第1张图片

你可能感兴趣的:(swift)