3 回答

TA貢獻1784條經驗 獲得超7個贊
標簽本身沒有作用域的,只是一個標志點。
但是goto本身有限制,只能是當前函數。所以,從這個角度來說,標簽的作用域也可以說是當前函數。
比如
123456789 | void func() { int a; a=0; loop: a++; if (a<10) goto loop; printf ( "%d" ,a); } |
這個程序中,loop標簽就與goto配合起到了跳轉作用。
對于goto來說,這個標簽只要是在本函數內的就是合法的,無論是在goto前還是goto后。
但是,如下程序:
123456789101112 | int a = 0; void func1( void ) { loop: printf ( "%d" ,a); } void func2( void ) { a++; if (a<10) goto loop; } |
在func2中調用goto使用了func1中的標簽loop,在編譯的時候就會報錯。因為使用goto時只會在本函數中查找該標簽。

TA貢獻1963條經驗 獲得超6個贊
(1) if(size > 12) goto a;
goto b;
a: cost = cost * 1.05
flag = 2;
b: bill = cost * flag;
等效于:
if (size >12) {
a: cost = cost * 1.05
flag = 2;
b: bill = cost * flag;
} else {
b: bill = cost * flag;
};
==========
(2) if(ibex > 14) goto a;
sheds = 2;
goto b;
a: sheds = 3;
b: help = 2 * sheds;
等效于:
if(ibex > 14) {
a: sheds = 3;
b: help = 2 * sheds;
} else {
sheds = 2;
b: help = 2 * sheds;
};
========
goto 語句用于本函數范圍。
goto 語句 可以在本域內 轉向。
goto 語句 可從本域轉 本域的外層域。
goto 語句 不可從本域轉 本域的內層域。

TA貢獻1843條經驗 獲得超7個贊
12345 | if (ibex > 14) sheds = 3; else sheds = 2; help = 2 * sheds; |
與
12345678 | if (ibex > 14) { sheds = 3; help = 2 * sheds; } else sheds = 2; help = 2 * sheds; |
有區別嗎?這2個是沒有任何區別的。
在你看來,區別是help = 2 * sheds;這句話的地方,但是,你發現了沒有,無論是上面的一個,還是下面的一個。不管if語句成立或者不成立,help = 2 * sheds;這句語句都是會執行的。所以,雖然在寫法上有一點區別,但是結果確實完全是一樣的。
這也是一種簡潔程序的一種思路,你現在有可能體會不到,等你以后編寫多了,你就能體會到這種簡潔的思路了。
- 3 回答
- 0 關注
- 1329 瀏覽
添加回答
舉報