Chapter 3. Repetition Structures 1. while 2. do-while 3. for 4. continue and break 1
The same calculation or sequence of instructions is repeated using different sets of data. Repetition statements while, do, for Condition to be evaluated A statement to set the initial condition A statement to allow the condition to become false 2
The flowchart of while 1. while 3
While (a<=10) cout<<a<<endl; Executed forever or never executed!! Must set the initial condition A statement to allow the condition to become false 4
Program 3.1.1--- 從 1 加到 N // Sum.cpp #include <iostream> using std::cin; using std::cout; using std::endl; int main() int N, Sum, Count ; while ( Count <= N ) Sum += Count ; Count++; cout << " 總和是 : " << Sum << endl ; return 0; cout << " 請輸入要累計的數目 :" << endl; cin >> N; Count = 1 ; Sum = 0; 5
The flowchart of do-while 2. do-while do statement 1; statement 2; while (condition); 6
Program 3.2.1--- 猜字母 #include <iostream> using std::cin; using std::cout; using std::endl; void main() char Secret1 = 'D', Secret2 = 'd'; char Guess; do cout << " 請輸入一個從 A 至 E 的字母 :" << endl ; cin >> Guess; while ((Guess!= Secret1) && (Guess!= Secret2)); cout << " 你猜對了, 就是 " << Secret1 << endl ; 7
Program 3.2.2 #include <iostream> using namespace::std; void main() char Secret1, Secret2,Guess; bool state1,state2; float i; state1=0;// 重新產生猜測子母 state2=1;// 是否要重猜 do if (state1== 0) i=rand()%26; Secret1=(char)(i+65); Secret2=(char)(Secret1+32); //cout<<secret1<<endl; cout << " 請輸入一個從 A 至 Z 的字母 :" << endl ; cin >> Guess; while (!((Guess<=90 && Guess>=64) (Guess<=122 && Guess>=97))) cout<<" 你輸入的不是英文字母, 請再輸入一次 \n"; cin >> Guess; #include <time.h> srand(time(0)); 8
if ((Guess!= Secret1) && (Guess!= Secret2)) cout<<" 你猜錯了, 請再猜一次 \n"; state1=1; else state1=0; cout<<" 你猜對了, 還要再猜一次嗎?\n"; cin>>guess; //if (Guess == 'y' Guess == 'Y') if (Guess == 89 Guess == 121) state2=1; else state2=0; while (state2 state1); cout <<" 再見 "<< endl ; 9
The flowchart of for 3. for for ( 起始值設定式 ; 條件判斷式 ; 參數改變式 ) 敘述 1; 敘述 2; for (i=0;i<10;i++) cout<<i<<endl; 10
Program 3.3.1--- 從 1 加到 N #include <iostream> using namespace::std; int main() int N, Sum, Count ; cout << " 請輸入要累計的數目 :" << endl; cin >> N; Sum = 0; for ( Count = 1 ; Count <= N ; Count++ ) Sum += Count ; cout << " 總和是 : " << Sum << endl ; return 0; 11
Example 1 for (Count=1 ;Count<=N;Count++) Sum+=Count; Example 2 Count=1; for ( ;Count<=N;Count++) Sum+=Count; Example 3 Count=1; for ( ;Count<=N;) Count++;Sum+=Count; Example 4 Count=1; for ( ;Count<=N; Sum+=Count, Count++) 12
Nested loop Program 3.3.2 #include <iomanip> #include <iostream> using namespace::std; int main() int M=3, N=5; cout << M <<" 列 " << N << " 行矩陣 :" << endl; for (int i = 1; i <= M; i++) for (int j = 1; j <= N ; j++) cout << setw(5)<< i << j ; cout << endl; return 0; 3 列 5 行矩陣 : 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 13
Program 3.3.3---12 12 的表 #include <iostream> #include <iomanip> using namespace std; int main() const int size = 12; int i = 0, j = 0; cout << endl << size << " by " << size << " Multiplication Table" << endl << endl; cout << endl << " "; for(i=1; i<=size; i++) cout << setw(3) << i << " "; cout << endl; 14
for(i=0; i<=size; i++) cout << " "; for(i=1; i<=size; i++) cout << endl << setw(3) << i << " "; for(j=1; j<=size; j++) cout << setw(3) << i*j << " "; cout << endl; return 0; 15
While while (condition) statement 1; statement 2; continue; statement 3; break; statement 4; statement 5; 4. continue and break 16
Program 3.4.1 #include <iostream> using namespace::std; int main() int N, Sum, Count ; if ( Count > N ) break; cout << " 總和是 : " << Sum << endl ; return 0; cout << " 請輸入要累計的數目 : " << endl; cin >> N; Count = 1 ; Sum = 0; while ( true ) Sum += Count ; Count++; 17
for for (I=0;I<=10;I++) statement 1; statement 2; continue; statement 3; break; statement 4; statement 5; 18
Program 3.4.2 #include <iostream> using namespace std; void main() for ( int i = 2 ; i <= 20 ; cout << "C ", i=i+2 ) cout << i; if (i == 6) continue; cout << "A"; if (i == 12) break; cout << "B"; cout << endl ; 2ABC 4ABC 6C 8ABC 10ABC 12A 19
//Example 1 #include <iostream> #include <cmath> using namespace std; void main() float a,b; float x,y; cout<< "Please input the first number x:\n"; cin>>x; cout<< "Please input the first number y:\n"; cin>>y; a=sqrt(x); b=y/10; cout<<"sqrt("<<x<<")="<<a; if (a>b) cout<<">"; else if (a<b) cout<<"<"; else cout<<"="; cout<<y<<"/10="<<b<<endl; 20
Example 1: 將 sqrt(x) > <= y/10 判斷結果這程式改寫成可詢問是否要再重新輸入 x, y, 若不要, 則可輸入 N 結束程式. Example 2: 將 128 個 ASCII 符號逐一列出來 (cout<<char(i)) Example 3: 列出華氏跟攝氏溫度對照表, 華氏從 0 到 300, 每隔 20 度輸出一次結果 (C=(5/9)(F-32)) Example 4: 請由鍵盤輸入 10 個值並將其相加後輸出到螢幕上 21
作業 : 一線上遊戲兩角色 A 與 B 的生命值都是 100, 每次攻擊力皆在 10~30 之間 ( 整數 ), 防禦力在 10~20 之間 ( 整數 ), 此兩角色開始戰鬥時, 在同一回合會隨機產生此回合攻擊值及防禦值, 若攻方攻擊值大於防禦方防禦值時, 則視為擊中防禦方, 此時防禦方會承受所有的攻擊損傷 ( 扣生命值 ), 反之則視為沒有擊中, 就沒有損傷, 當一方生命值為 0 時, 則戰鬥結束, 請寫一程式模擬此戰鬥開始到一方生命值為零結束戰鬥為止. 在程式中請將每回合的戰鬥狀況 ( 損傷狀況 ) 顯示在螢幕上. 在戰鬥結束後請顯示誰為勝方, 並顯示出勝方最後的生命值, 勝方會出現笑臉 :), 敗方會出現哭臉 :(, 但注意同時陣亡之情況. 附加功能 : 每回合可詢問玩家受否要使用回復技能 (Y), 若回答為 Y, 則當回合喪失攻擊機會 ( 仍會產生防禦值 ), 而生命值可回復 10, 但此回復技能只能用兩次. 22
一程式模擬男生 A" 追求 ( 送禮物 ) 女生 B" 之狀況 : 女生每次約會前會暗示有五種東西是她可能喜歡的 ( 其實只喜歡一種 ), 當男生送對禮物 ( 猜對 ) 給女生時, 則感情分數會上升 20 分, 若送錯禮物 ( 猜錯 ) 則會扣 10 分, 二者感情初始分數是 50, 當分數達到 100 時則追求成功, 若分數扣到 0, 則追求失敗. 請將每次送禮物之狀況顯示在螢幕上 ( 送對或送錯以及目前感情分數 ). 程式結束時, 請顯示追求成功或失敗, 成功時可顯示笑臉 :), 失敗時可顯示哭臉 :(. 五種東西可用 0,1,2,3,4 來代替. 附加功能 : 每此送禮物可詢問男生是否要使用 幸運之符, 若使用, 則該次禮物會變成 2 選 1, 但 幸運之符 只能使用兩次. 23