看板 Examination 關於我們 聯絡資訊
※ 引述《usisiu (LOOK)》之銘言: : 題目出處: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; 這一段程式碼要如何解釋? : 先謝謝各位先進 while( A.條件式 ) {B.當條件成立時,就重覆做的事} while(a<b) case 4:{a=a+5;}break; 一開始a=4 b=30 條件成立 a=4+5=9 a=9 b=30 條件成立 a=9+5=14 a=14 b=30 條件成立 a=14+5=19 a=19 b=30 條件成立 a=19+5=24 a=24 b=30 條件成立 a=24+5=29 a=29 b=30 條件成立 a=29+5=34 a=34 b=30 條件不成立 離開while迴圈 遇到break 離開switch 上面的結果 是把程式碼改寫成下面土法煉鋼出來的 while (a<b ) case 4:{a=a+5; printf("in while loop, a= %d.\n", a);} break; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.47.182.113 ※ 文章網址: https://www.ptt.cc/bbs/Examination/M.1529399852.A.ACA.html