十二 Swift3.0之 for循环

时间:2021-05-22 20:27:19
/* for循环 格式: for (初始化表达式;循环保持条件;循环后表达式) {需要执行的语句} OC: int sum = 0; for (int i = 0; i <= 10; i++) { sum = i++; } NSLog(@"%d", sum); int sum = 0; int i = 0; for (; i <= 10; i++) { sum = i++; } NSLog(@"%d", sum); int sum = 0; int i = 0; for (; i <= 10; ) { sum = i++; i++; } NSLog(@"%d", sum); int sum = 0; int i = 0; for ( ; ; ) { sum = i++; i++; if (i > 10) { break; } } NSLog(@"%d", sum); int sum = 0; int i = 0; for ( ; ; ) { sum = i++; i++; NSLog(@"%d", sum); } 如果只有一条指令for后面的大括号可以省略 for后面的三个参数都可以省略, 如果省略循环保持语句, 那么默认为真 Swift: 0.for后的圆括号可以省略 1.传统的for循环 在swift3.0被取消 2.i++ ++i 在swift3.0被取消 */
// 更新 2017.11.30
for i in 0..<5          //等同代码 fot( int i = 0 ; i < 5 ; i++ )
{
    print(i)
}


for i in 0...5          //等同代码 for( int i = 0 ; i <= 5 ; i++ )
{
    print(i)
}


//反向循环

for i in (0..<10).reversed()            //等同代码 for(int i = 10 ; i > 0 ; i --)
{
    print(i)
}
}

/* for in循环 格式: for (接收参数 in 取出的参数) {需要执行的语句} for in含义: 从(in)取出什么给什么, 直到取完为止 OC: for (NSNumber *i in @[@1, @2, @3, @4, @5]) { NSLog(@"%@", i); } NSDictionary *dict = @{@"name":@"lnj", @"age":@30}; for (NSArray *keys in dict.allKeys) { NSLog(@"%@", keys); } NSDictionary *dict = @{@"name":@"lnj", @"age":@30}; for (NSArray *keys in dict.allValues) { NSLog(@"%@", keys); } Swift: for in 一般用于遍历区间或者集合 */

var sum4:Int = 0
for i4 in 1...10 // 会将区间的值依次赋值给i
{
    sum4 += i4;
}
print(sum4)

for dict in ["name":"lnj", "age":30]
{
    print(dict);
}

for (key, value) in ["name":"lnj", "age":30]
{
    print("\(key) = \(value)")
}