第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

C語(yǔ)言:goto標(biāo)簽的作用域到底是什么(疑惑)

C語(yǔ)言:goto標(biāo)簽的作用域到底是什么(疑惑)

C
暮色呼如 2019-03-23 10:15:09
網(wǎng)上查看了很多資料都說(shuō):goto到標(biāo)簽以后就按照順序執(zhí)行,這個(gè)標(biāo)簽只是一個(gè)跳轉(zhuǎn)地址跟作用域真沒(méi)什么關(guān)系。但是goto有作用范圍,文件內(nèi)。(我想應(yīng)該是正確吧!)可是我發(fā)現(xiàn) C Primer Plus 中文第五版 181 頁(yè)的例子:(1) if(size > 12)goto a;goto b;a: cost = cost * 1.05flag = 2;b: bill = cost * flag;書(shū)上說(shuō)等效于:if(size > 12){cost = cost * 1.05;flag = 2;}bill = cost * flag;按照網(wǎng)上的理論(goto到標(biāo)簽以后就按照順序執(zhí)行):為什么不是?if(size > 12){cost = cost * 1.05;flag = 2;bill = cost * flag;}bill = cost * flag;(2) if(ibex > 14)goto a;sheds = 2;goto b;a: sheds = 3;b: help = 2 * sheds;書(shū)上說(shuō)等效于:if(ibex > 14)sheds = 3;elsesheds = 2;help = 2 * sheds;同理,按照網(wǎng)上的理論(goto到標(biāo)簽以后就按照順序執(zhí)行):為什么不是?if(ibex > 14){sheds = 3;help = 2 * sheds;}elsesheds = 2;help = 2 * sheds;是書(shū)上錯(cuò)誤,還是網(wǎng)上理論有錯(cuò),或自己沒(méi)有理解正確?
查看完整描述

3 回答

?
開(kāi)滿天機(jī)

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)潔的思路了。

 

 


查看完整回答
反對(duì) 回復(fù) 2019-03-25
?
白豬掌柜的

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)簽。



查看完整回答
反對(duì) 回復(fù) 2019-03-25
?
qq_笑_17

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)層域。



查看完整回答
反對(duì) 回復(fù) 2019-03-25
  • 3 回答
  • 0 關(guān)注
  • 1051 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)