1 / 21

控制流程

控制流程. 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…. 控制流程. 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…. while 迴圈. syntax 1: while ( 關係運算元 ) statement ;. syntax 2: while ( 關係運算元 ) { statement 1 ; ..…

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. 控制流程 選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…

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

  3. while 迴圈 • syntax 1: while ( 關係運算元 ) statement ; • syntax 2: while ( 關係運算元 ) { statement 1 ; ..… statement n ; }

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

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

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

  7. do -- while的範例 執行結果: 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 <iostream> #include <cstdlib> #include <ctime> int main() { intrand_num; int ……… srand(time(NULL));//下亂數種子 rand_num = rand() % 100 + 1;//產生介於 1~100 的亂數值 ……… ………

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

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

  12. for 迴圈範例(二) 想一想: 印出來的結果為? 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         *****       * ** 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. 浮點數資料欲控制輸出小數位數 //浮點數預設輸出小數位數為六位 //固定式小數位數的輸出 cout.setf(ios_base::fixed, ios_base::floatfield); //顯示到小數位第3位 cout.precision(3);

  17. Sample : // setprecision example #include <iostream> #include <iomanip> using namespace std; int main () { double f =3.14159; cout << setprecision (5) << f << endl; cout << setprecision (9) << f << endl; cout << fixed; cout << setprecision (5) << f << endl; cout << setprecision (9) << f << endl; return 0; } The execution of this example displays something similar to: 3.1416 3.14159 3.14159 3.141590000

  18. break 的用途 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

  19. 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; }

  20. continue 的用途 結果: 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; }

  21. 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; }

More Related