5.循环控制语句

课程来自慕课网DavidChin老师


for循环
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        for (int i = 0; i<5; i++) {
            NSLog(@"i=%d",i);
        }
    return 0;
    }
}
while循环
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int i=0;
        while (i<5) {
            NSLog(@"i=%d",i);
            i++;
        }
    return 0;
    }
}
do-while循环
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int i=0;
        do {
            NSLog(@"i=%d",i);
            i++;
        } while (i<5);
    }
}
break语句
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        for (int i=0; i<100; i++) {
            NSLog(@"i=%d",i);
            if (i == 4) {
                break;  // 跳出循环
            }
        }
    }
}
continue语句
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        for (int i=0; i<100; i++) {
            if (i > 4) {
                continue;  // 结束本次循环
            }
            NSLog(@"i=%d",i);
        }
    }
}

你可能感兴趣的:(5.循环控制语句)