Re: [課業] 鐵路 107 高員三級 程式語言 第五題

作者: p19101910 (Sky)   2018-06-20 09:25:19
(因為跨行排版 所以就不用推文答覆)
原程式(這個例子 在a = 4 時,
如果刪掉(在a = 4 時) 不會執行或測試的指令,
並加上 大括號 和一點註解 方便理解)
真正執行的指令等於如下
#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: ;
case 2: ;
case 3: ;
{
/* 此例因為 a = 4 一路往下到此 */
while(a<b)
{
case 4: a=a+5;
} // END of while
break;
}
}//end of switch
printf("after the switch statement, a=%d.\n",a);
}
題目這種程式, 只是為了考試,說實話 Readability 很差。
在實務業界 不應該會有人寫出這樣的程式。只是出來考人的。
※ 引述《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; 這一句程式碼有關
作者: p19101910 (Sky)   2018-06-20 09:28:00
這題 考 switch 中, case 為什麼有時要加 break如果 沒有 break,和有break,的差別。
作者: cutekid (可愛小孩子)   2018-06-20 10:48:00
可讀性差+1。switch case 中的 case 還第一次見...

Links booklink

Contact Us: admin [ a t ] ucptt.com