swift基础_控制语句

怎么着,swift会创建了吧,生成了我们的main.swift文件:

 

看到hello world了吧,那么在swift里控制语句有事长什么样子的呢,且看:

import Foundation

print("Hello, World!")

//定义一个分数
var score = 80

//定义一个数字
var scoreArr = [90,100,79,70,50,30]

var minScore = 0;
var maxScore = 0;
var avgScore = 0.0
var sumScore = 0.0
var count = scoreArr.count;
//循环所有的元素
for s in scoreArr{
    sumScore = sumScore + Double(s)
    print("s is \(s)")
    if(minScore == 0 || minScore > s){
        minScore = s
    }else if(maxScore == 0 || maxScore < s){
        maxScore = s
    }
}

avgScore = sumScore/Double(count);
print("sumScore is \(sumScore) avgScore is \(avgScore)")
print("max score is \(maxScore) min score is\(minScore)")



for(var i=0;i<count;i++){
    var s = scoreArr[i];
    print("for..i \(i) s = \(s)")
    if(minScore == 0 || minScore > s){
        minScore = s
    }else if(maxScore == 0 || maxScore < s){
        maxScore = s
    }
}
avgScore = sumScore/Double(count);
print("sumScore is \(sumScore) avgScore is \(avgScore)")
print("max score is \(maxScore) min score is\(minScore)")


var index = 0;
repeat{
    if(index >= count){
        break;//退出
    }
    var s = scoreArr[index];
    print("do while s[\(index)]=\(s)");
}while(++index < count);


index = 0;
while(index < count){
    var s = scoreArr[index];//取得第i个元素
    print("do while s[\(index++)]=\(s)");
}


//switch 之前的switch 会穿透下面执行完
let appType = "iOS"
switch appType{
    case "iOS":
        print("iOS")
        fallthrough;//往下走,穿透一层
    case "AD":
        print("AD")
    case "WP":
        print("WP")
    default:
        print("没有匹配值")
}

 

总结:还记得swift刚出来的时候do{}while();被苹果fix成了repeat{}while();真是让有语言基础的人语塞了,for in循环还是保留的,书写风格怎一个简化,但是这个switch语句就是一个fallthrough直接穿透一层是不是吊上天了感觉

posted on 2016-01-24 12:26  乘风波浪  阅读(152)  评论(0)    收藏  举报

导航