zoukankan      html  css  js  c++  java
  • swift基础语法(14-break-continue)

    break: 跳出循环, 无论循环保持条件是否还为真都不会再执行循环
    continue: 跳出本次循环, 如果循环保持条件还为真还会继续执行循环
    OC:
    NSArray *arr = @[@1,@3, @5, @7, @8];
    for (NSNumber *num in arr) {
        if ([num isEqualTo:@(7)]) {
            NSLog(@"找到幸运数字");
            break;
        }
        NSLog(@"没有找到幸运数字");
    }
    输出结果:
    2016-04-01 17:23:07.807 OCTest[4684:1554896] 没有找到幸运数字
    2016-04-01 17:23:07.808 OCTest[4684:1554896] 没有找到幸运数字
    2016-04-01 17:23:07.808 OCTest[4684:1554896] 没有找到幸运数字
    2016-04-01 17:23:07.808 OCTest[4684:1554896] 找到幸运数字
     
    NSArray *arr = @[@1,@3, @5, @7, @8];
    int count = 0;
    for (NSNumber *num in arr) {
        if (num.intValue % 2 != 0 ) {
            NSLog(@"不能被2整除");
            continue;
        }
        NSLog(@"能被2整除");
        count++;
    }
    NSLog(@"count = %d", count);
     
    输出结果:
    2016-04-01 17:23:48.005 OCTest[4694:1560348] 不能被2整除
    2016-04-01 17:23:48.006 OCTest[4694:1560348] 不能被2整除
    2016-04-01 17:23:48.006 OCTest[4694:1560348] 不能被2整除
    2016-04-01 17:23:48.006 OCTest[4694:1560348] 不能被2整除
    2016-04-01 17:23:48.006 OCTest[4694:1560348] 能被2整除
    2016-04-01 17:23:48.006 OCTest[4694:1560348] count = 1
     
     
    Swift:
    var arr:Array<Int> = [1, 3, 5, 7, 8]
    for num in arr{
        if num == 7
        {
            print("找到幸运数字")
            break
        }
        print("没有找到幸运数字")
    }
    输出结果:
    没有找到幸运数字
    没有找到幸运数字
    没有找到幸运数字
    找到幸运数字
     
    var arr1:Array<Int> = [1, 3, 5, 7, 8]
    var count:Int = 0
    for num in arr1{
        if num % 2 != 0
        {
            print("不能被2整除")
            continue
        }
        print("能被2整除")
        count++
    }
    print("count = (count)")
     
    输出结果:
    不能被2整除
    不能被2整除
    不能被2整除
    不能被2整除
    能被2整除
    count = 1
     
     
     
     
     
     
    我们每一种习惯都是由一再重复的行为所铸造的,因此,优秀不是一种行为,而是一种习惯.
  • 相关阅读:
    bzoj2588 Count on a tree
    poco对象生成的几种方式根据你使用不同的ui决定
    airtest本地连接和远程连接
    python音频文件中pcm格式提取
    python提取视频中的音频
    如何理解快速排序的时间复杂度是O(nlogn)
    剑指 Offer 45. 把数组排成最小的数
    剑指 Offer 44. 数字序列中某一位的数字
    剑指 Offer 43. 1~n 整数中 1 出现的次数
    剑指 Offer 41. 数据流中的中位数
  • 原文地址:https://www.cnblogs.com/jordanYang/p/5378310.html
Copyright © 2011-2022 走看看