作者usisiu (LOOK)
看板Examination
標題[課業] 鐵路 107 高員三級 程式語言 第五題
時間Tue Jun 19 16:28:01 2018
題目出處:107年 鐵路特考 高員三級 資訊處理 程式語言 第五題
(一)請問下列C程式執行的結果是什麼? (10分)
#include <stdio.h>
int a,b;
void main(){
a=4;
b=30;
printf("Before the switch statement, a=%d.\n",a);
switch(a){
case 1:a=a*10;break;
case 2:a=a*10;break;
case 3:a=a*10;
while(a<b) case 4:{a=a+5;}break;
case 5:a=a*10;break;
default:
b=a*10;break;
}//end of switch
printf("after the switch statement, a=%d.\n",a);
}
疑惑點:
我在考試的時候,看不懂 while(a<b) case 4:{a=a+5;}break;
所以直接跳過,直接看到 default: b=a*10;break; 這一段,
因此我的答案是寫
Before the switch statement, a=4.
after the switch statement, a=4.
但是我用編譯器跑過一次,發現答案是
Before the switch statement, a=4.
after the switch statement, a=34.
懷疑跟 while(a<b) case 4:{a=a+5;}break; 這一句程式碼有關
但是我又無法解釋為甚麼,第一次看到這一種程式碼,
請問 while(a<b) case 4:{a=a+5;}break; 這一段程式碼要如何解釋?
先謝謝各位先進
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 192.83.186.60
※ 文章網址: https://www.ptt.cc/bbs/Examination/M.1529396884.A.1E5.html
※ 編輯: usisiu (192.83.186.60), 06/19/2018 16:29:48
推 m24737779: case 4(a==4)時,執行while(a<b)迴圈內容a=a+5。之後br 06/23 00:54
→ m24737779: eak跳出switch判斷式。我那時是這樣想的。 06/23 00:54