標籤語法標籤:
.
這個標籤給本行的行尾加個名字.
還可以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例)
,