3 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
標(biāo)簽本身沒有作用域的,只是一個(gè)標(biāo)志點(diǎn)。
但是goto本身有限制,只能是當(dāng)前函數(shù)。所以,從這個(gè)角度來說,標(biāo)簽的作用域也可以說是當(dāng)前函數(shù)。
比如
123456789 | void func() { int a; a=0; loop: a++; if (a<10) goto loop; printf ( "%d" ,a); } |
這個(gè)程序中,loop標(biāo)簽就與goto配合起到了跳轉(zhuǎn)作用。
對(duì)于goto來說,這個(gè)標(biāo)簽只要是在本函數(shù)內(nèi)的就是合法的,無論是在goto前還是goto后。
但是,如下程序:
123456789101112 | int a = 0; void func1( void ) { loop: printf ( "%d" ,a); } void func2( void ) { a++; if (a<10) goto loop; } |
在func2中調(diào)用goto使用了func1中的標(biāo)簽loop,在編譯的時(shí)候就會(huì)報(bào)錯(cuò)。因?yàn)槭褂胓oto時(shí)只會(huì)在本函數(shù)中查找該標(biāo)簽。

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
(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 語句用于本函數(shù)范圍。
goto 語句 可以在本域內(nèi) 轉(zhuǎn)向。
goto 語句 可從本域轉(zhuǎn) 本域的外層域。
goto 語句 不可從本域轉(zhuǎn) 本域的內(nèi)層域。

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
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; |
有區(qū)別嗎?這2個(gè)是沒有任何區(qū)別的。
在你看來,區(qū)別是help = 2 * sheds;這句話的地方,但是,你發(fā)現(xiàn)了沒有,無論是上面的一個(gè),還是下面的一個(gè)。不管if語句成立或者不成立,help = 2 * sheds;這句語句都是會(huì)執(zhí)行的。所以,雖然在寫法上有一點(diǎn)區(qū)別,但是結(jié)果確實(shí)完全是一樣的。
這也是一種簡潔程序的一種思路,你現(xiàn)在有可能體會(huì)不到,等你以后編寫多了,你就能體會(huì)到這種簡潔的思路了。
- 3 回答
- 0 關(guān)注
- 1324 瀏覽
添加回答
舉報(bào)