Ans | for-loops | while-loops | do-while-loops |
---|---|---|---|
1 | for(n=0; n<5; n++)printf("%d",n); |
||
n=0; for( ; n<5 ; ) { printf("%d",n);} |
n=0; while(n<5) { printf("%d",n);} |
n=0; do { printf("%d",n);} while(n<5); |
|
輸出 | 01234 |
01234 |
01234 |
2 | for(n=5; n>0; n--)printf("%d",n); |
||
n=5; for( ; n>0 ; ) { printf("%d",n);} |
n=5; while(n>0) { printf("%d",n);} |
n=5; do { printf("%d",n);} while(n>0); |
|
輸出 | 54321 |
54321 |
54321 |
3 | ... for(n=10; n<5; n++) printf("%d",n); |
n=10; while(n<5) { printf("%d",n);} |
n=10; do { printf("%d",n);} while(n<5); |
輸出 | 沒有 |
沒有 |
10 |
4 | n=5; for( ; ; ) printf("%d",n); |
n=0; while(n<5) { printf("%d",n);} |
n=10; do { printf("%d",n);} while(n>0); |
輸出 | 無窮迴路 |
infinite loop |
無窮迴路 |
5 | ... for(n=5; n>0; n++) { printf("%d",n);} |
n=0; while(n<5) { printf("%d",n);} |
n=10; do { printf("%d",n);} while(n>0); |
輸出 | 無窮迴路 |
infinite loop |
無窮迴路 |
1 | switch(char) |
---|---|
char choice; while(1) { printf("1. 新增 Add Records");} |
|
執行結果 | 1. 新增 Add Records 2. 列出 List Records 3. 修改 Modify Records 4. 刪除 Delete Records 0. 離開 Exit 請選擇 Your choice? 4 刪除紀錄...... |
2 | switch(int) |
int mm, days; while(1) { do {} |
|
執行結果 | Q: 請選擇月份 Enter month (0-12): 1 A: month 1: 31 days ... |
3 | switch(int) |
printf("Q: 輸入月(1-12)、日(1-31) : "); scanf("%i %i", &mm, &dd); days = dd; switch(mm-1) { case 11: days += 30;} printf("A: Total no. of days = %d\n", days); |
|
執行結果 | Q: 輸入月(1-12)、日(1-31) : 5 1 A: Total no. of days = 121 ... |