因為聽聞有的語言若使用break會直接跳出最外層的迴圈,特別做此測試
+ (void) checkContinueCommandInLoop{
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) {
continue;
//break;
}
NSLog(@"j : %i", j);
}
NSLog(@"i : %i", i);
}
}
在objective-C中的結果為:scope只限於當前(最內層)的迴圈
例如
continue:
i會由0~9會跑完
j則會只跑0,1,2,3,4,6,7,8,9
break:
i會由0~9會跑完
j則會只跑0,1,2,3,4
補充:for-in迴圈也適用continue與break
for (NSNumber *number in tempArray) {
if ([number isEqualToNumber:@5]) {
continue;
}
NSLog(@"number : %@",number);
}
補充:for-in迴圈也適用continue與break
for (NSNumber *number in tempArray) {
if ([number isEqualToNumber:@5]) {
continue;
}
NSLog(@"number : %@",number);
}
沒有留言:
張貼留言