Presentation is loading. Please wait.

Presentation is loading. Please wait.

選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…

Similar presentations


Presentation on theme: "選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…"— Presentation transcript:

1 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…
控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…

2 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…
控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…

3 while 迴圈 { statement 1 ; ..… statement n ; } syntax 2: syntax 1:

4 while 範例(一) main( ) { int i=0; while ( i < 5 )
cout << i++ << endl; cout << "We're out of the loop.\n"; } 執行結果: 1 2 3 4 We're out of the loop

5 while 範例(二) main( ) { int i=0, c=0; while ( i < 5 )
cout << i++; cout << ++c << endl; } cout << "We\'re out of the loop.\n"; 執行結果: 01 12 23 34 45 We're out of the loop.

6 do -- while 迴圈 syntax 2: syntax 1: do do statement ; while (關係運算元) ; {
. . . statement n ; } while (關係運算元) ; syntax 1: do statement ; while (關係運算元) ;

7 do -- while 的範例 main( ) { int i=0,c=0; do { cout << i++;
執行結果: 01 12 23 45 We're out of the loop. main( ) { int i=0,c=0; do { cout << i++; cout << ++c << endl; }while( i < 5 ); cout << "We\'re out of the loop.\n"; }

8 隨堂練習一 試利用 do-while 或 while迴圈改寫程式,讓(1)計算BMI 或 (2)解一元二次方程式 可重覆執行。
利用do-while 或 while 迴圈撰寫一猜數字程式。(電腦亂數產生一介於1~100整數,再讓 user 猜出此一數字,過程必須告訴 user 所猜之數字太大或太小或已猜中,並計算 user 所猜之次數。)

9 產生亂數的方法 #include <ctime> #include <iostream>
#include <cstdlib> #include <ctime> int main() { int rand_num; int ……… srand(time(NULL)); //下亂數種子 rand_num = rand() % ; //產生介於 1~100 的亂數值 ………

10 for 迴圈 for (初值設定運算式; 條件測試運算式; 增加值運算式) { …………………; } 迴路主體

11 for 迴圈範例(一) 1 2 3 4 5 main( ) { int i; for(i=0 ; i<=5 ; ++i)
cout << i << endl; } 想一想: 印出來的結果為? 1 2 3 4 5

12 for 迴圈範例(二) 01 12 23 34 45 We're out of the loop. main( ) { int i, c ;
想一想: 印出來的結果為? main( ) { int i, c ; for ( i=0,c=1 ; i < 5 ; ++i,++c ) cout << i; cout << c << endl; } cout << "We're out of the loop.\n"; 01 12 23 34 45 We're out of the loop.

13 隨堂練習二 試利用for迴圈撰寫出一個能產生如下圖結果的程式。 (1) (2) (3) (4) * 1 ***** *
(1) (2)        (3) (4)     * 1         *****       * ** 22   ****        ** *** 333 ***        *** **** 4444 **        **** ***** 55555 *        *****

14 隨堂練習三 試利用 for 迴圈撰寫如下九九乘法表。
1*1= 1 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9 1*2= 2 2*2= 4 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 1*3= 3 2*3= 6 3*3= 9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27 1*4= 4 2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36 1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45 1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54 1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63 1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72 1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

15 設定資料輸出寬度格式 #include <iomanip> //緊接在後的資料用n格的寬度印出 setw(n); Ex: cout << "i=" << setw(8) << i << endl;

16 浮點數資料欲控制輸出小數位數 //浮點數預設輸出小數位數為六位 //固定式小數位數的輸出 //顯示到小數位第2位
cout.setf(ios_base::fixed, ios_base::floatfield); //顯示到小數位第2位 cout.precision(2);

17 break 的用途 結果: i=1 i=2 int main( ) { int i= 0 ; while(i <= 5) { ++i;
if ( i == 3 ) break ; cout << "i=" << i << endl; } system("PAUSE"); return 0; 結果: i=1 i=2

18 break 的用途二 int main() { int item, total = 0; while (true) { cout << "請輸入欲加總的數字 或 0 表示結束\n"; cin >> item; if (item == 0) break; total += item; cout << "小計:" << total << ‘\n’; } cout << "總合:" << total << ‘\n’; system("PAUSE"); return 0;

19 continue 的用途 system("PAUSE"); return 0; 結果: i=1 i=2 i=4 i=5 i=6
int main( ) { int i= 0; while(i <= 5) { ++i; if ( i == 3 ) continue ; cout << “i=” << i << endl; } system("PAUSE"); return 0; 結果: i=1 i=2 i=4 i=5 i=6

20 continue 的用途二 int main() { int item, minus_item = 0, total = 0; while (true) { cout << "請輸入欲加總的數字 或 0 表示結束\n"; cin >> item; if (item == 0) break; if (item < 0) { ++minus_item; continue; } total += item; cout << "小計:" << total << ‘\n’; cout << "總合:" << total << ‘\n’; system("PAUSE"); return 0;


Download ppt "選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…"

Similar presentations


Ads by Google