44常用d編程標籤和至

標籤語法標籤:.
這個標籤給本行的行尾加個名字.
還可以anExpression(); end: anotherExpression();,但不常用.

void foo(bool condition) {
    writeln("first");
    if (condition) {
        goto end;
    }
    writeln("second");
end:
    writeln("third");
}

goto代理以難以理解著稱.
上面代碼可重寫爲:

void foo(bool condition) {
    writeln("first");

    if (!condition) {
        writeln("second");
    }

    writeln("third");
}

在c中,還有幾種用途:

// --- C 代碼 ---

int foo() {
    // ...

    if (error) {
        goto finally;
    }

    // ...

finally:
    // ... 清理,交資源,撤銷等 ...
    return error;
}

在d中都不必要.垃集,析構器,catch,finally,scope語句,c++中也不必要.
在c中還有外部循環的問題.

// --- C 代碼 ---

    while (condition) {
        while (otherCondition) {
            continue;//內圈
            break;//內圈
            goto continueOuter;// 外圈'continue' 
            goto breakOuter;// 外圈'break' 
        }

    continueOuter:
        ;
    }
breakOuter:

同樣的技術可用於外部switch.goto在d中這種用法也是不必要的,因爲d循環標籤.c++還需要這種方法.

    if (condition) {
        goto aLabel;    // 跳過構造
    }
    auto s = S(42);     // 構造
aLabel:
    s.bar();            // BUG: 沒定義's'

跳過構造.
編譯器阻止:

Error: goto skips declaration of variable deneme.main.s

循環標籤:

outerLoop:
    while (condition) {
        while (otherCondition) {
            continue;
            break;
            continue outerLoop;//在外部標籤繼續
            break outerLoop;//在外部標籤處斷開
        }
    }

即從外部標籤處繼續斷開.
switch語句也可含標籤.一個內部break可從外部switch點中斷.
還有goto case,跳至下例,goto default與goto case expression(跳至XX例),
標籤語法標籤:.
這個標籤給本行的行尾加個名字.
還可以anExpression(); end: anotherExpression();,但不常用.

void foo(bool condition) {
    writeln("first");
    if (condition) {
        goto end;
    }
    writeln("second");
end:
    writeln("third");
}

goto代理以難以理解著稱.
上面代碼可重寫爲:

void foo(bool condition) {
    writeln("first");

    if (!condition) {
        writeln("second");
    }

    writeln("third");
}

在c中,還有幾種用途:

// --- C 代碼 ---

int foo() {
    // ...

    if (error) {
        goto finally;
    }

    // ...

finally:
    // ... 清理,交資源,撤銷等 ...
    return error;
}

在d中都不必要.垃集,析構器,catch,finally,scope語句,c++中也不必要.
在c中還有外部循環的問題.

// --- C 代碼 ---

    while (condition) {
        while (otherCondition) {
            continue;//內圈
            break;//內圈
            goto continueOuter;// 外圈'continue' 
            goto breakOuter;// 外圈'break' 
        }

    continueOuter:
        ;
    }
breakOuter:

同樣的技術可用於外部switch.goto在d中這種用法也是不必要的,因爲d循環標籤.c++還需要這種方法.

    if (condition) {
        goto aLabel;    // 跳過構造
    }
    auto s = S(42);     // 構造
aLabel:
    s.bar();            // BUG: 沒定義's'

跳過構造.
編譯器阻止:

Error: goto skips declaration of variable deneme.main.s

循環標籤:

outerLoop:
    while (condition) {
        while (otherCondition) {
            continue;
            break;
            continue outerLoop;//在外部標籤繼續
            break outerLoop;//在外部標籤處斷開
        }
    }

即從外部標籤處繼續斷開.
switch語句也可含標籤.一個內部break可從外部switch點中斷.
還有goto case,跳至下例,goto default與goto case expression(跳至XX例),

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章