switch - falling through |
尚未結案
|
Smallcat7315
一般會員 發表:19 回覆:22 積分:7 註冊:2005-05-02 發送簡訊給我 |
請問一下為什麼switch的條件case選項如果都不加break
則當滿足條件時 執行完敘述後 繼續往下執行 且不論後來case選項有無符合
皆會執行該case的敘述 譬如說
int a=10
switch (a)
{
case 11:
printf("11");
case 10:
printf("10");
case 9:
printf("9");
case 8:
printf("8");
default
printf("7");
} 結果 輸出
10 9 8 7
雖然會繼續往下執行 但是a不等於9 8 7為何還是會輸出呢? 發表人 - smallcat7315 於 2005/10/04 19:51:04
|
李國維
高階會員 發表:42 回覆:287 積分:235 註冊:2003-02-07 發送簡訊給我 |
|
Smallcat7315
一般會員 發表:19 回覆:22 積分:7 註冊:2005-05-02 發送簡訊給我 |
|
Smallcat7315
一般會員 發表:19 回覆:22 積分:7 註冊:2005-05-02 發送簡訊給我 |
|
李國維
高階會員 發表:42 回覆:287 積分:235 註冊:2003-02-07 發送簡訊給我 |
Smallcat7315:
switch是selection.不是loop
用到break是因為要告訴程式case到此結束. 請參考
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_10.asp
並節錄一段相關文字
The break statement is used to stop execution and transfer control to the statement after the switch statement. Without a break statement, every statement from the matched case label to the end of the switch, including the default, is executed.
|
Smallcat7315
一般會員 發表:19 回覆:22 積分:7 註冊:2005-05-02 發送簡訊給我 |
引言: Smallcat7315: switch是selection.不是loop 用到break是因為要告訴程式case到此結束. 請參考 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_10.asp 並節錄一段相關文字 The break statement is used to stop execution and transfer control to the statement after the switch statement. Without a break statement, every statement from the matched case label to the end of the switch, including the default, is executed.謝謝你 我來看看 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |