首页 > 编程语言 > 详细

SWIFT——循环语句(for、while)

时间:2015-10-29 18:14:53      阅读:310      评论:0      收藏:0      [点我收藏+]

文章为自己学习所用,系转载。为学习51cto课程的笔记。如有侵权,请私信本人进行删除。

链接如下。

?http://edu.51cto.com/roadmap/view/id-58.html

 

 

1、条件增量for语句 :for init; condition ; increment

2、for-in语句

3、while

4、do while

 1 for var index = 0; index < 3; ++index
 2 {
 3        println("index is \(index)")
 4 }
 5 
 6 var index:Int
 7 for index = 0 ; index < 3 ; ++index
 8 {
 9        println("index is \(index)")
10 }
11 
12 // for - in
13 for index in 1...5
14 {
15       println(index)
16 }
17 
18 var value = 0
19 for _ in 1...20    //不需要索引变量,只要循环20次
20 {
21      value +=3
22 }
23 println(value)        //60
24 
25 let names = ["Bill","Mike","Mary"]
26 for name in names
27 {
28        println("hello\(name)");
29 }
30 
31 let numberOfLegs = [“蜘蛛":8, "蚂蚁":6, "":4]//赋值两边都应有空格
32 
33 for (animalName, legCount) in numberOfLegs
34 {
35         println("\(animalName)有\(legCount)条腿")//输出不会按照上面顺序。
36 }
1 //枚举字符串中的所有字符
2 for c in "hello"
3 {
4       print(c+" ")   // h e l l o
5 }
6 println()   
 1 var i = 0
 2 while i < 10
 3 {
 4          i++; 
 5          println(i);
 6 }
 7 
 8 do
 9 {
10         i--
11         println(i)
12 }while   i > 0

 

SWIFT——循环语句(for、while)

原文:http://www.cnblogs.com/zhuzhubjtu/p/4921055.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!