1 / 32

程式設計的基礎

程式設計的基礎. 1. 程式設計簡介 2. 循序結構 3. 條件結構 4. 重覆結構. 程式設計簡介 ( 1/5 ). 程式設計的步驟:. 定義問題. 測試與除錯 (測試結果). 擬定解題計畫. 撰寫程式 (解決問題). 製作文件. 內縮程式敘述. if (a &gt; b) { printf(&quot;a=%d<br>&quot;, a); printf(&quot;b=%d<br>&quot;, b); printf(&quot;a 大於 b<br> &quot; ); }. 程式設計簡介 ( 2/5 ). 程式撰寫的原則: 養成宣告變數及常數的習慣。 遵照一定的命名規則。 善用副程式或函數。

donar
Download Presentation

程式設計的基礎

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 程式設計的基礎 1.程式設計簡介 2.循序結構 3.條件結構 4.重覆結構

  2. 程式設計簡介(1/5) • 程式設計的步驟: 定義問題 測試與除錯 (測試結果) 擬定解題計畫 撰寫程式 (解決問題) 製作文件

  3. 內縮程式敘述 if (a > b) { printf("a=%d\n", a); printf("b=%d\n", b); printf("a大於b\n"); } 程式設計簡介(2/5) • 程式撰寫的原則: • 養成宣告變數及常數的習慣。 • 遵照一定的命名規則。 • 善用副程式或函數。 • 善用內縮格式。 • 儘量使用註解。

  4. 程式敘述區塊 程式設計簡介(3/5) • 程式的基本結構:循序、條件、及重覆等3種。 • 循序結構:由上而下依序執行程式敘述的程式結構。

  5. 條件式 F T 程式敘述區塊 程式敘述區塊 程式設計簡介(4/5) • 程式的基本結構: • 條件結構:利用分歧敘述來決定執行路徑的程式結構。

  6. 條件式 程式敘述區塊 T F 程式設計簡介(5/5) • 程式的基本結構: • 重覆結構:利用迴圈重覆執行程式敘述的程式結構。

  7. 循序結構 範例 printf(“Hello World !!!\n”); printf(“My name is Jenny.\n”); printf(“This is my first program.\n”);

  8. 條件結構

  9. if (判斷條件)敘述; F If 判斷條件 單 行 多 行 T 敘述主體 其他敘述 條件結構:if 語法 if (判斷條件) {敘述1;敘述2;… }

  10. 條件結構:範例一 int score=70; if(score >=60) printf(“及格\n”); if(score < 60) printf(“不及格\n”); 【輸出結果】 及格

  11. T If 判斷條件 F 敘述主體1 敘述主體2 其他敘述 條件結構:if-else 語法 if(判斷條件) 敘述; else 敘述; if(判斷條件) { 敘述主體1; } else { 敘述主體2 ; }

  12. 條件結構:範例二 int a; printf(“input a number:”); scanf(“%d”,&a); if(a%2==1) printf(“%d是奇數\n”,a); else printf(“%d是偶數\n”,a); 【輸出結果】 假設a=10 10是偶數

  13. 判斷條件1 敘述主體1 T F 敘述主體2 判斷條件2 T F 其他敘述 條件結構:else-if 語法 if(判斷條件1) { 敘述主體1; } else if(判斷條件2) { 敘述主體2 ; }

  14. 條件結構:範例三 int a = 0; if (a>0) printf("%d大於0!\n", a); else if (a==0) printf("a等於0!\n"); else printf(“%d小於0!\n", a); 【輸出結果】 a等於0!

  15. switch運算式 選擇值1 選擇值2 選擇值n default 敘述主體1 敘述主體2 敘述主體n 敘述主體 … break; break; break; 其他敘述 條件結構: switch語法 switch (運算式) { case 選擇值1 : 敘述主體1 ; break; case 選擇值2 : 敘述主體2 ; break; . . . case 選擇值n : 敘述主體n ; break; default : 敘述主體 ; }

  16. 條件結構:範例四 int a, b; char oper; printf(“輸入運算式(如2+3): ”); scanf(“%d %c %d”, &a, &oper, &b); switch (oper) { case‘+’ : printf(“%d + %d = %d \n” ,a ,b ,a+b); break; case‘-’: printf(“%d - %d = %d \n ”,a ,b ,a-b); break; case‘*’: printf(“%d * %d = %d \n ”,a ,b ,a*b); break; case‘/’ : printf(“%d / %d = %.3f \n ”,a ,b ,(float)a/b); break; case‘%’ : printf(“%d %% %d = %d \n” ,a ,b ,a%b); break; default : printf (“無法辨識運算式!!\n”); } 【輸出結果】 輸入運算式(如2+3): 100/7 100 / 7 = 14.286

  17. 重覆結構

  18. 重覆結構:For迴圈的基本流程

  19. 注意沒有分號 注意沒有分號 注意沒有分號 重覆結構:For迴圈敘述格式 • 明確知道要重複執行的次數時,使用 for迴圈 for (設定初值;判斷條件;設定增減量) 敘述; for (設定初值;判斷條件;設定增減量) { 敘述1; 敘述2; ……… }

  20. 重覆結構:範例一 int a; for (a=0; a<5; a++) printf("%d ", a); printf("\nEnd of for loop\n"); a 0 1 2 3 【輸出結果】 4 0 1 2 3 4 End of for loop 5

  21. 重覆結構:範例二 int i, sum=0; for (i=1; i<=10; i++) sum+=i; printf(“1+2+3+...+10=%d\n”, sum); i sum 1 0+1=1 2 1+2=3 3 3+3=6 4 6+4=10 5 10+5=15 6 15+6=21 【輸出結果】 7 21+7=28 1+2+3+…+10=55 8 28+8=36 9 36+9=45 10 45+10=55

  22. 重覆結構:範例三 int i; srand((unsigned)time(NULL)); printf("電腦選號的結果: "); for (i=0; i<6; i++) printf("%02d ", rand()%42 + 1); printf(“\n”); 【輸出結果】 電腦選號的結果: 06 40 37 36 17 15

  23. 重覆結構:範例四 int i, j; for (i=1; i<=9; i++) { for (j=1; j<=9; j++) printf(“%d*%d=%2d ”,i,j,i*j); printf(“\n”); } i j 1 1~9 2 1~9 3 1~9 4 1~9 5 1~9 6 1~9 7 1~9 【輸出結果】 8 1~9 9 1~9

  24. 重覆結構:範例五 int i, j, n=6; for (i=1; i<=n; i++) { for (j=1; j<=i; j++) printf(“*”); printf(“\n”); } i j 1 1~1 2 1~2 3 1~3 4 1~4 5 1~5 6 1~6 【輸出結果】 * ** *** **** ***** ******

  25. 設定初值 false 判斷 條件 true 其他敘述 迴圈主體 設定增減量 重覆結構:While迴圈的基本流程

  26. 注意沒有分號 注意沒有分號 注意沒有分號 重覆結構:While迴圈敘述格式 • 當條件成立時,進入 while 主體,重複執行敘述,直到條件不成立為止。 while (判斷條件) 敘述; while (判斷條件) {敘述1;敘述2;… }

  27. 重覆結構:範例六 a int a=0; while (a<5) { printf("%d ",a); a++; } printf(“\nEnd of while loop\n"); 0 1 2 3 4 【輸出結果】 5 0 1 2 3 4 End of while loop

  28. 設定初值 迴圈主體 設定增減量 true 判斷 條件 false 其他敘述 重覆結構:do while迴圈的基本流程

  29. 注意有分號 注意有分號 重覆結構:do while迴圈敘述格式 do 敘述; while (判斷條件); do {敘述1;敘述2;… } while (判斷條件);

  30. 重覆結構:範例七 a int a=5; do { printf("%d ",a); a--; } while (a>0); printf(“\nEnd of while loop\n"); 5 4 3 2 1 【輸出結果】 0 5 4 3 2 1 End of while loop

  31. 重覆結構:範例八 int a, r; /*將正整數到過來印*/ do { printf(“Input an integer:”); scanf(“%d”, &a); }while (a<=0); while (a!=0) { r=a%10; a/=10; printf(“%d”, r); } 假設a=123 a r 123 3 12 2 1 1 0

  32. 迴圈的比較

More Related