swift 基础笔记七(for循环)

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

import UIKit

// for 循环

//你可以使用for-in循环来遍历一个集合里面的所有元素,例如由数字表示的区间、数组中的元素、字符串中的字符。


for index in 1...5{
    println(index);
}

//如果你不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问
var count = 3;
var one = 1;
var two = 2;
for _ in 1...count{
    one *= two
}

// 遍历数组

var arr1 = ["E","n","d","a"];
for name in arr1{
    println(name);
}

// key + value
var arr2 = [1:"E",2:"n",3:"d",4:"a"];
for (key,value) in arr2{
    println("\(key)==>\(value)")
}


// 遍历字符串

var names = "MyNameisEnda";

for name in names{
    println("\(name)");
}

// for 条件递增
for var index = 0;index<3;++index{
    println(index);
}

相关推荐