Presentation is loading. Please wait.

Presentation is loading. Please wait.

for 迴圈 while迴圈 do-while迴圈 break 與 continue goto 與 標籤 程式觀摩

Similar presentations


Presentation on theme: "for 迴圈 while迴圈 do-while迴圈 break 與 continue goto 與 標籤 程式觀摩"— Presentation transcript:

1 for 迴圈 while迴圈 do-while迴圈 break 與 continue goto 與 標籤 程式觀摩
第五章 流程控制(二) 迴圈 for 迴圈 while迴圈 do-while迴圈 break 與 continue goto 與 標籤 程式觀摩

2 for 迴圈的語法 for (初始設定; 條件判斷;變動方式) { 敘述; }
例如 for (i=1; i <= 10 ; i++) { printf("ABC"); 印出 10 個 ABC 例如 for (i=0 ; i <= 10 ; i++) printf("%d",i); 印出

3 for 迴圈的流程圖

4 【範例 5-1-1】 設計一個C程式,使用 for 迴圈,求出 1,2,3,....,100 的和。 8 int i, sum; 9
11 for (i=1; i<=100; i++) 12 { 13 sum += i; 14 } 15 16 printf(" = %d\n",sum);

5 【範例 5-1-1】的輸出畫面 = 5050

6 巢狀for 迴圈 for (初始設定; 條件判斷;變動方式) { 敘述; }
例如 for (i=1 ; i <= 3 ; i++) { for (j=1; j<=2; j++) { printf("%d,%d ", i, j); 印出 1,1 1,2 2,1 2,2 3,1 3,2

7 【範例 5-1-4】 設計一個C程式,使用 for 迴圈,印出下列 n 階三角圖形。圖形的階數應由鍵盤輸入指定 8 int n, i, j;
9 10 printf("Enter level number : "); 11 scanf("%d",&n); 12 for (i=1; i<=n; i++) 13 { 14 for (j=1; j<=i; j++) putchar('*'); 16 putchar('\n'); 17 }

8 【範例 5-1-4】的輸出畫面 Enter level number : 10 * ** *** **** ***** ******
******* ******** ********* **********

9 無限迴圈 for for ( ; ; ) { 敘述 1 if (特殊條件) break; 敘述 2 … }
這種迴圈會無限次重覆,因為無任何判斷條件,永遠視為 '真',不斷地重覆迴圈內的敘述,只能以 break 敘述或其他特殊函數跳出,或是按下Ctrl-Break 來中斷程式的執行

10 while 迴圈的語法 while ( 條件判斷) { } 敘述 1 敘述 2 例如 ch=getchar();
while (ch != '\n') { putchar(ch); ch=getchar();

11 while 迴圈的流程圖

12 【範例 5-2-1】 設計一個C程式,使用 while 迴圈, 求出 1,2,3,...,100 的和。 8 int i, sum; 9
13 { 14 sum += i; 15 i++; 16 } 17 18 printf(" = %d\n",sum);

13 【範例 5-2-2】 p1/2 試設計一個C程式, 輸入一連串的學科成績,以一個負數做為資料的結果,求出其總分及平均成績
11 sum = 0.0; 12 i = 0; 13 printf("Enter next score (negative to quit ) : "); 14 scanf("%f",&score); 15 while (score >= 0) 16 { 17 sum += score; 18 i++; 19 printf("Enter next score (negative to quit ) : "); 20 scanf("%f",&score); 21 }

14 【範例 5-2-2】p2/2 23 printf("Total %d scores counted\n",i);
24 printf("Total score : %8.2f \n",sum); 25 printf("Average score : %8.2f \n",sum/i);

15 【範例 5-2-2】的輸出畫面 Enter next score (negative to quit ) : 99
Total 3 scores counted Total score : Average score :

16 【範例 5-2-3】 試設計一個C程式,輸入一串字元,以 Enter 鍵結束,印出此字串的長度。 8 int length=0;
9 char ch; 10 11 printf("Enter a string ended by Enter key : "); 12 ch = getchar(); 13 while (ch != '\n') 14 { 15 length++; 16 ch = getchar(); 17 } 19 printf("String length is %d\n",length);

17 【範例 5-2-3】的輸出畫面 Enter a string ended by Enter key : abcdefghijkl
String length is 12 說明:  1. 第 12 列至 17 可改為 while ((ch=getchar()) !='\n') length++; 更為簡潔

18 無限迴圈 while while (1) { 敘述 1 if (特殊條件) break; 敘述 2 … }

19 do while 迴圈的語法 do { 敘述 1 … 敘述 n } while (條件判斷); 例如
printf("I love you !"); printf("say again ?"); c=getche(); } while (c==‘Y’);

20 do while 迴圈的流程圖

21 【範例 5-3-1】 設計一個C程式,使用 do-while迴圈,求出 1,2,3,....,100的和。 8 int i, sum; 9
11 12 i=1; 13 do { sum += i; i++; 16 } while (i <= 100) ; 17 18 printf(" = %d\n",sum);

22 break中斷,continue繼續 設計一個C程式,輸入一連串數值,以 0 做為資料結束,求出所有正數的總和。如果資料中有任何負數則忽略之
12 while (i < 100) 13 { 14 printf("Enter next number : "); 15 scanf("%f",&num); 16 if (num == 0) break; 中斷!!!立即離開迴圈 17 if (num < 0) continue; 繼續迴圈的下一輪(到第12行) 18 sum += num; 19 i++; 20 } 21 printf("Sum of %d postive numbers : %8.2f\n", i, sum);

23 goto 跳到某標籤的地方 例如 印出 10 個 ABC count=0; here: printf(""ABC"); count++;
if (count < 10) goto here; 其中 here 就是標籤

24 程式觀摩 【範例 】p1/2 設計一個程式,輸入數個學生的C語言成績,以負數做為資料的結束。求出分數的總平均,並統計及格與不及格的人數。 8 int i ,pass ,fail; 9 float score, sum=0.0; 11 i = pass = fail = 0; 13 printf("Enter %dth student score : ",i+1); 14 scanf("%f",&score); 15 while (score >= 0) 16 { 17 i++; 18 if (score >= 60) pass++; 19 else fail++; 20 sum += score; 21 printf("Enter %dth student score : ",i+1); 22 scanf("%f",&score); 23 }

25 程式觀摩 【範例 5-6-1】p2/2 24 printf("Total %d students.\t",i);
25 printf("Average score = %8.2f\n",sum/i); 26 27 printf(" "); 28 for (i=0; i<5; i++) printf(" "); 29 putchar('\n'); 30 printf("%3d Passed |",pass); 31 for (i=0; i<pass; i++) putchar('P'); 32 putchar('\n'); 33 printf("%3d Failed |",fail); 34 for (i=0; i<fail; i++) putchar('F'); 35 putchar('\n');

26 【範例 5-6-1】的輸出畫面 Enter student scores ended by negative number.
Enter 1th student score : 90 Enter 2th student score : 80 Enter 3th student score : 70 Enter 4th student score : 60 Enter 5th student score : 50 Enter 6th student score : 40 Enter 7th student score : -1 Total 6 students. Average score = 4 Passed |PPPP 2 Failed |FF


Download ppt "for 迴圈 while迴圈 do-while迴圈 break 與 continue goto 與 標籤 程式觀摩"

Similar presentations


Ads by Google