C in ObjC系列(2) 流程控制

C in ObjC系列(2) 流程控制

C in ObjC系列为Objective-C中保留C语言语法的系列介绍。

流程控制

程式中可以去判断条件成立时要利用程式做什么样的动作,这就称为流程控制,下面介绍的语法中,会利用不同语法来呈现相同输出结果。

if

if 的功能可以有很多种配合方式,当你只需要条件成立时执行程式内容时:

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

除了条件成立外,条件不成立时也要执行程式内容时可以增加:

    if (<#condition#>) {
        <#statements#>
    } else {
        <#statements#>
    }

多种条件成立时与不成立时各别执行程式内容时:

    if (<#condition#>) {
        <#statements#>
    } else if (<#expression#>) {
        <#statements#>
    } else {
        <#statements#>
    }

多种条件判断时,依照下面格式增加条件判断:

    if (<#condition#>) {
        <#statements#>
    } else if (<#expression#>) {
        <#statements#>
    } else if (<#expression#>) {
        <#statements#>
        :
        :
    } else if (<#expression#>) {
        <#statements#>
    }  else {
        <#statements#>
    }

  • <#condition#> 条件判断式
  • <#statements#> 执行程式内容

程式范例:

//-----------start-----------
    for (NSInteger m = 4; m > 0; m --) {
        if (m == 1) {
            NSLog(@"1     m = 1");
        } else if (m == 2) {
            NSLog(@"2     m = 2");
        } else {
            NSLog(@"Other m = %ld",m);
        }
    }
//------------end------------

输出结果:

2014-08-17 16:18:33.318 LoopDemo[65795:303] Other m = 4
2014-08-17 16:18:33.318 LoopDemo[65795:303] Other m = 3
2014-08-17 16:18:33.318 LoopDemo[65795:303] 2     m = 2
2014-08-17 16:18:33.318 LoopDemo[65795:303] 1     m = 1

switch

    switch (<#expression#>) {
        case <#constant#>:
            <#statements#>
            break;
        default:
            break;
    }

  • <#expression#> 传入变数的名称
  • <#constant#> 依据传入变数值内容成立时,执行下面的程式内容
  • <#statements#> 执行程式内容
  • default:条件都未成立时,会执行default这个预设的程式内容

每个case条件成立时,执行程式内容结束后必需要增加break来中断switch/case

//-----------start-----------
    for (NSInteger m = 4; m > 0; m --) {
        switch (m) {
            case 1:
                NSLog(@"1     m = 1");
                break;
            case 2:
                NSLog(@"2     m = 2");
                break;
            default:
                NSLog(@"Other m = %ld",m);
                break;
        }
    }
//------------end------------

输出结果:

2014-08-17 16:41:57.648 LoopDemo[66124:303] Other m = 4
2014-08-17 16:41:57.649 LoopDemo[66124:303] Other m = 3
2014-08-17 16:41:57.649 LoopDemo[66124:303] 2     m = 2
2014-08-17 16:41:57.650 LoopDemo[66124:303] 1     m = 1

未加入break时程式会继续执行下一个内容:

//-----------start-----------
    for (NSInteger m = 4; m > 0; m --) {
        switch (m) {
            case 1:
                NSLog(@"1     m = 1");
            case 2:
                NSLog(@"2     m = 2");
                break;
            default:
                NSLog(@"Other m = %ld",m);
                break;
        }
    }
//------------end------------

输出结果:

2014-08-17 16:34:29.942 LoopDemo[66010:303] Other m = 4
2014-08-17 16:34:29.943 LoopDemo[66010:303] Other m = 3
2014-08-17 16:34:29.943 LoopDemo[66010:303] 2     m = 2
2014-08-17 16:34:29.943 LoopDemo[66010:303] 1     m = 1
2014-08-17 16:34:29.943 LoopDemo[66010:303] 2     m = 2

m = 1时,执行程式内容完成后未加入break,所以会继鑟执行case 2:后的内容,然而输出结果会多印出2 m = 2

有先前未加入break的方式,所以您要多个判断都执行程式内容的相同结果,那可以写成这样:

//-----------start-----------
    for (NSInteger m = 4; m > 0; m --) {
        switch (m) {
            case 1:
            case 2:
                NSLog(@"1,2     m = 1 or 2");
                break;
            default:
                NSLog(@"Other m = %ld",m);
                break;
        }
    }
//------------end------------

输出结果:

2014-08-17 16:38:16.872 LoopDemo[66067:303] Other m = 4
2014-08-17 16:38:16.873 LoopDemo[66067:303] Other m = 3
2014-08-17 16:38:16.873 LoopDemo[66067:303] 1,2     m = 1 or 2
2014-08-17 16:38:16.874 LoopDemo[66067:303] 1,2     m = 1 or 2