码迷,mamicode.com
首页 > 编程语言 > 详细

swift基础_控制语句

时间:2016-01-24 13:00:24      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

怎么着,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基础_控制语句

标签:

原文地址:http://www.cnblogs.com/xuanzhangran123/p/5154942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!