控制流程

21
控控控控 控控控控控 if-else… switch-case 控控控控控 while… do-while… for…

Upload: ulysses-watkins

Post on 02-Jan-2016

26 views

Category:

Documents


11 download

DESCRIPTION

控制流程. 選擇性結構 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 ; ..… - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 控制流程

控制流程

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

Page 2: 控制流程

控制流程

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

Page 3: 控制流程

while 迴圈

syntax 1 : while ( 關係運算元

)

statement ;

syntax 2 : while ( 關係運算元

)

{

statement 1 ;

..…

statement n ;

}

Page 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

Page 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.

Page 6: 控制流程

do -- while 迴圈 syntax 1 : do

statement ;

while ( 關係運算元 ) ;

syntax 2 : do

{

statement 1 ;

statement 2 ;

. . .

statement n ;

} while ( 關係運算元 ) ;

Page 7: 控制流程

do -- while 的範例

main( ) { int i=0,c=0; do { cout << i++; cout << ++c << endl; }while( i < 5 ); cout << "We\'re out of the loop.\n"; }

執行結果 : 01 12 23 45 We're out of the loop.

Page 8: 控制流程

隨堂練習一

試利用 do-while 或 while 迴圈改寫程式,讓 (1) 計算 BMI 或 (2) 解一元二次方程式 可重覆執行。

利用 do-while 或 while 迴圈撰寫一猜數字程式。 ( 電腦亂數產生一介於 1~100 整數,再讓 user 猜出此一數字,過程必須告訴 user 所猜之數字太大或太小或已猜中,並計算 user 所猜之次數。 )

Page 9: 控制流程

產生亂數的方法#include <iostream>#include <cstdlib>

#include <ctime>

int main(){

int rand_num; int ………

srand(time(NULL)); // 下亂數種子 rand_num = rand() % 100 + 1; // 產生介於 1~100 的亂數值

……… ………

Page 10: 控制流程

for 迴圈

for ( 初值設定運算式 ; 條件測試運算式 ; 增加值運算式 )

{ …………………; …………………; …………………; }

迴路主體

Page 11: 控制流程

for 迴圈範例 ( 一 )

main( ) { int i; for(i=0 ; i<=5 ; ++i) cout << i << endl; }

想一想 : 印出來的結果為 ?

0 1 2 3 4 5

Page 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";}

想一想 : 印出來的結果為 ?

0112233445We're out of the loop.

Page 13: 控制流程

試利用 for 迴圈撰寫出一個能產生如下圖結果的程式。

(1) (2)        (3) (4)    

* 1         *****       * ** 22   ****        **

*** 333 ***        *** **** 4444 **        **** ***** 55555 *        *****

隨堂練習二

Page 14: 控制流程

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

隨堂練習三試利用 for 迴圈撰寫如下九九乘法表。

Page 15: 控制流程

設定資料輸出寬度格式

#include <iomanip>// 緊接在後的資料用 n 格的寬度印出setw(n);

Ex:cout << "i=" << setw(8) << i << endl;

Page 16: 控制流程

浮點數資料欲控制輸出小數位數

// 浮點數預設輸出小數位數為六位

// 固定式小數位數的輸出 cout.setf(ios_base::fixed, ios_base::floatfield);

// 顯示到小數位第 3 位 cout.precision(3);

Page 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.14163.141593.141593.141590000

Page 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

Page 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;}

Page 20: 控制流程

continue 的用途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

Page 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;}