Sequence structure Selection structure l l Iteration structure

  • Slides: 17
Download presentation

結構化程式設計 ¢ 循序性結構 (Sequence structure) ¢ 選擇性結構 (Selection structure) l l ¢ 迭代性結構 (Iteration

結構化程式設計 ¢ 循序性結構 (Sequence structure) ¢ 選擇性結構 (Selection structure) l l ¢ 迭代性結構 (Iteration structure) l l 2 if / else if switch for while / do. . . while C程式語言 / 迴圈敘述句 2020/12/7

快問快答 int a; for (a=0; a<5; a++) printf("%d ", a); printf("n. End of for

快問快答 int a; for (a=0; a<5; a++) printf("%d ", a); printf("n. End of for loop"); 【輸出結果】 0 1 2 3 4 End of for loop 5 C程式語言 / 迴圈敘述句 2020/12/7

隨堂練習 請利用 for 迴圈,印出下列圖形 (1)11111 (2)1 (3)11111 (4) 1 22222 22 33333 333 44444

隨堂練習 請利用 for 迴圈,印出下列圖形 (1)11111 (2)1 (3)11111 (4) 1 22222 22 33333 333 44444 44 4444 55555 5 55555 7 C程式語言 / 迴圈敘述句 2020/12/7

快問快答 int a=0; while (a<5) { printf("%d ", a); a++; } printf("n. End of

快問快答 int a=0; while (a<5) { printf("%d ", a); a++; } printf("n. End of while loop"); 【輸出結果】 0 1 2 3 4 End of while loop 11 C程式語言 / 迴圈敘述句 2020/12/7

快問快答 int a=5; do { printf("%d ", a); a--; } while (a>0); printf("n. End

快問快答 int a=5; do { printf("%d ", a); a--; } while (a>0); printf("n. End of while loop"); 【輸出結果】 5 4 3 2 1 End of while loop 13 C程式語言 / 迴圈敘述句 2020/12/7

關鍵字:break、continue 用途 break continue 跳出迴圈 回到迴圈起始處繼續下一個執行 a=0; while (a<5) { a++; if (a==3) 範例

關鍵字:break、continue 用途 break continue 跳出迴圈 回到迴圈起始處繼續下一個執行 a=0; while (a<5) { a++; if (a==3) 範例 break; printf("%d ", a); } printf("Endn"); a=0; while (a<5) { a++; if (a==3) contunue; printf("%d ", a); } printf("Endn"); 結果 1 2 End 1 2 4 5 End 15 C程式語言 / 迴圈敘述句 2020/12/7

參考程式碼 int main() { char ans; ……………… do { ……………… printf("要繼續執行嗎(y/n)? "); scanf(" %c",

參考程式碼 int main() { char ans; ……………… do { ……………… printf("要繼續執行嗎(y/n)? "); scanf(" %c", &ans); } while (ans=='y' || ans=='Y'); printf("Byebye!"); system("PAUSE"); return 0; } 17 C程式語言 / 迴圈敘述句 2020/12/7