C in ObjC系列(1) 迴圈

C in ObjC系列(1) 迴圈

C in ObjC系列為Objective-C中保留C語言語法的系列介紹。

迴圈

迴圈是每個程式語言都具備的功能,Objective-C沿用C語言的迴圈語法,下面的範例使用多種迴圈的語法來完成相同的結果,會印出變數內容4、3、2、1依序遞減。

while {}

    while (<#condition#>) {
        <#statements#>
    }

  • 先判斷 <#condition#> 符合條件就執行迴圈內程式
  • <#statements#> 程式內容
//-----------start-----------
    NSInteger m = 4;
    while (m > 0) {
        NSLog(@"%ld",m);
        m = m -1;
    }
//------------end------------

m > 0 執行迴圈。

輸出結果:

2014-08-17 14:48:20.496 LoopDemo[64502:303] 4
2014-08-17 14:48:20.496 LoopDemo[64502:303] 3
2014-08-17 14:48:20.496 LoopDemo[64502:303] 2
2014-08-17 14:48:20.497 LoopDemo[64502:303] 1

do {} while

    do {
        <#statements#>
    } while (<#condition#>);

  • 先執行 do ... while的程式後再判斷<#condition#> 符合條件就繼續執行迴圈內程式
  • <#statements#> 程式內容
//-----------start-----------
    NSInteger m = 4;
    do {
        NSLog(@"%ld",m);
        m = m -1;
    } while (m > 0);
//------------end------------

m > 0 執行迴圈。

輸出結果:

2014-08-17 14:51:02.062 LoopDemo[64562:303] 4
2014-08-17 14:51:02.062 LoopDemo[64562:303] 3
2014-08-17 14:51:02.063 LoopDemo[64562:303] 2
2014-08-17 14:51:02.063 LoopDemo[64562:303] 1

for {}

    for (<#initialization#>; <#condition#>; <#increment#>) {
        <#statements#>
    }

  • <#initialization#> 初始值,可以在此定義新的變數或是用已知的
  • <#condition#> 條件成立時繼續執行迴圈內程式
  • <#increment#> 初始值變數變化算式
  • <#statements#> 程式內容
//-----------start-----------
    for (NSInteger m = 4; m > 0; m = m - 1) {
        NSLog(@"%ld",m);
    }
//------------end------------

m 變數初始值為4,每次執行迴圈後將m減1,如果 m > 0的話繼續執行迴圈

2014-08-17 14:58:27.218 LoopDemo[64742:303] 4
2014-08-17 14:58:27.218 LoopDemo[64742:303] 3
2014-08-17 14:58:27.219 LoopDemo[64742:303] 2
2014-08-17 14:58:27.219 LoopDemo[64742:303] 1

for in {}

    for (<#type *object#> in <#collection#>) {
        <#statements#>
    }

  • <#collection#> 列舉(尋訪)的物件
  • <#type *object#> 列舉(尋訪)中取得的物件型態
  • <#statements#> 程式內容

這雖然在原先C的定義中未有此功能,但Objective-C將for增加在物件上使用會有列舉(尋訪)的功能,它會依照物件的連結關系將物件一一列舉(尋訪),直到物件無法再列舉(尋訪)為止,範例會將要印出的數值變數存入陣列中,再使用for將陣列列舉出來。

//-----------start-----------
    NSArray *m = @[@4,@3,@2,@1];

    for (NSNumber  *mm in m) {
        NSLog(@"%ld",[mm integerValue]);
    }
//------------end------------

輸出結果:

2014-08-17 15:13:26.162 LoopDemo[64922:303] 4
2014-08-17 15:13:26.162 LoopDemo[64922:303] 3
2014-08-17 15:13:26.162 LoopDemo[64922:303] 2
2014-08-17 15:13:26.163 LoopDemo[64922:303] 1

此語法在許多物件語法中都會提供該功能,像:Java、Python…等。

break、continue

有時,執行迴圈時,你會因為某些條件成立後想要確即的離開迴圈,或是條件成立後,想要跳離次此的迴圈,繼續下一個迴圈,要達成這二種功能,程式語言提供了continuebreak

  • continue 跳離次此迴圈不執行後面的程式內容,但不離開迴圈,直接下一次迴圈
  • break 中斷迴圈不執行後面的程式內容,並且離開迴圈
//-----------start-----------
    NSArray *m = @[@4,@3,@2,@1];

    for (NSNumber  *mm in m) {

        if ([mm integerValue] == 3) {
            NSLog(@"continue");
            continue;
        }

        if ([mm integerValue] == 2) {
            NSLog(@"break");
            break;
        }

        NSLog(@"%ld",[mm integerValue]);
    }
//------------end------------

mm = 3時印出continue後不執行後面的程式內容,直接執行下一個迴圈判斷式,並且mm = 2時,印出break後跳離此迴圈並且不執行後面的程式內容,所以程式中只會印出數值4

輸出結果:

2014-08-17 15:33:18.190 LoopDemo[65172:303] 4
2014-08-17 15:33:18.190 LoopDemo[65172:303] continue
2014-08-17 15:33:18.191 LoopDemo[65172:303] break