3 回答

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
1 2 3 4 5 | if(ibex > 14) sheds = 3; else sheds = 2; help = 2 * sheds; |
與
1 2 3 4 5 6 7 8 | if(ibex > 14) { sheds = 3; help = 2 * sheds; } else sheds = 2; help = 2 * sheds; |
有區(qū)別嗎?這2個(gè)是沒(méi)有任何區(qū)別的。
在你看來(lái),區(qū)別是help = 2 * sheds;這句話的地方,但是,你發(fā)現(xiàn)了沒(méi)有,無(wú)論是上面的一個(gè),還是下面的一個(gè)。不管if語(yǔ)句成立或者不成立,help = 2 * sheds;這句語(yǔ)句都是會(huì)執(zhí)行的。所以,雖然在寫(xiě)法上有一點(diǎn)區(qū)別,但是結(jié)果確實(shí)完全是一樣的。
這也是一種簡(jiǎn)潔程序的一種思路,你現(xiàn)在有可能體會(huì)不到,等你以后編寫(xiě)多了,你就能體會(huì)到這種簡(jiǎn)潔的思路了。

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
標(biāo)簽本身沒(méi)有作用域的,只是一個(gè)標(biāo)志點(diǎn)。
但是goto本身有限制,只能是當(dāng)前函數(shù)。所以,從這個(gè)角度來(lái)說(shuō),標(biāo)簽的作用域也可以說(shuō)是當(dāng)前函數(shù)。
比如
1 2 3 4 5 6 7 8 9 | 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來(lái)說(shuō),這個(gè)標(biāo)簽只要是在本函數(shù)內(nèi)的就是合法的,無(wú)論是在goto前還是goto后。
但是,如下程序:
1 2 3 4 5 6 7 8 9 10 11 12 | 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)1818條經(jīng)驗(yàn) 獲得超7個(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 語(yǔ)句用于本函數(shù)范圍。
goto 語(yǔ)句 可以在本域內(nèi) 轉(zhuǎn)向。
goto 語(yǔ)句 可從本域轉(zhuǎn) 本域的外層域。
goto 語(yǔ)句 不可從本域轉(zhuǎn) 本域的內(nèi)層域。
- 3 回答
- 0 關(guān)注
- 1051 瀏覽
添加回答
舉報(bào)