Upload
frederick-burke
View
294
Download
5
Embed Size (px)
Citation preview
C++ Control Structures
程式的控制、運算、判斷跟你之前學過的程式語言是類似的,所以這裡我們只用程式碼來說明。你應該而且必須能看得懂這些程式 !!
// Fig. 1.2: fig01_02.cpp
// A first program in C++
#include <iostream>
int main()
{
std::cout << "Welcome to C++!\n";
return 0; // indicate that program ended successfully
}
你能回答這些問題嗎 ?
// 是做什麼用的什麼是 #include? 什麼是 iostream?
什麼是 std? cout? \n?
// Printing a line with multiple statements
#include <iostream>
using std::cout;
int main()
{
cout << "Welcome ";
cout << "to C++!\n";
return 0; // indicate that program ended successfully
}
你能回答這些問題嗎 ?
什麼是 using std::cout? 為什麼 main( ) 裡的 cout 前不加 std::? 這個 program 的 output 是什麼 ?
// Printing multiple lines with a single statement
#include <iostream>
Using namespace std;
int main()
{
cout << "Welcome\nto\n\nC++!“<< endl;
return 0; // indicate that program ended successfully
}
你能回答這些問題嗎 ?
什麼是 using namespace std? 什麼是 endl?這個 program 的 output 是什麼 ?
// Fig. 1.6: fig01_06.cpp Addition program#include <iostream>
int main(){ int integer1, integer2, sum; // declaration
std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assignment of sum std::cout << "Sum is " << sum << std::endl; // print sum
return 0; // indicate that program ended successfully}
你能回答這些問題嗎 ?
什麼是 cin?
// Fig. 1.14: fig01_14.cpp//Using if statements, relational operators, and equality operators
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main(){ int num1, num2;
cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; cin >> num1 >> num2; // read two integers
if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << endl;
你能回答這些問題嗎 ?
你知道這些邏輯判斷代表的意思嗎 ? = = != < <= > >=
它們應該用在什麼地方 ?
if ( num1 != num2 ) cout << num1 << " is not equal to " << num2 << endl;
if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl;
if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl;
if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl;
if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl;
return 0; // indicate that program ended successfully}
//Fig. 2.14: fig02_14.cpp Preincrementing and postincrementing#include <iostream>using std::cout;using std::endl;
int main(){ int c;
c = 5; cout << c << endl; // print 5 cout << c++ << endl; // print 5 then postincrement cout << c << endl << endl; // print 6
c = 5; cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6
return 0; // successful termination}
你能回答這些問題嗎 ?
cout << c++; 跟 cout << ++c;有何不同 ?
// Fig. 2.7: fig02_07.cpp//Class average program with counter-controlled repetition
#include <iostream>
using std::cout;using std::cin;using std::endl;
int main(){ int total, // sum of grades gradeCounter, // number of grades entered grade, // one grade average; // average of grades
// initialization phase total = 0; // clear total gradeCounter = 1; // prepare to loop
如何寫一個 while loop?
1. 變數及 data 的宣告與初始化while( 2. 變數的判斷 ){ 3. 處理資料 4. 變數的改變}
// processing phase while ( gradeCounter <= 10 ) { // loop 10 times cout << "Enter grade: "; // prompt for input cin >> grade; // input grade total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter }
// termination phase average = total / 10; // integer division cout << "Class average is " << average << endl;
return 0; // indicate program ended successfully}
你能回答這些問題嗎 ?
x = x + 1 在 C++ 裡是什麼意思 ?
// Fig. 2.17: fig02_17.cpp// Counter-controlled repetition with the for structure#include <iostream>
using std::cout;using std::endl;
int main(){ // Initialization, repetition condition, and incrementing // are all included in the for structure header.
for ( int counter = 1; counter <= 10; counter++ ) cout << counter << endl;
return 0;}
如何寫一個 for loop?
data 的宣告與初始化for( 1. 變數宣告初始化 ; 2. 變數的判斷 ; 4. 變數的改變 ){ 3. 處理 data }
// Fig. 2.20: fig02_20.cpp Summation with for#include <iostream>
using std::cout;using std::endl;
int main(){ int sum = 0;
for ( int number = 2; number <= 100; number += 2 ) sum += number;
cout << "Sum is " << sum << endl;
return 0;}
// Fig. 2.24: fig02_24.cpp// Using the do/while repetition structure#include <iostream>
using std::cout;using std::endl;
int main(){ int counter = 1;
do { cout << counter << " "; } while ( ++counter <= 10 );
cout << endl;
return 0;}
如何寫一個 do/while loop?
1. 變數及 data 的宣告與初始化do{ 2. 處理 data 3. 變數的改變} while( 4. 變數的判斷 );
或1. 變數及 data 的宣告與初始化do{ 2. 處理 data} while(3. 變數的改變 4. 變數的判斷 );
你能回答這些問題嗎 ?
你知道什麼時候該用 while 什麼時候該用 do/while
什麼時候該用 for 嗎 ?
// Fig. 2.21: fig02_21.cpp Calculating compound interest#include <iostream>
using std::cout;using std::endl;using std::ios;
#include <iomanip>
using std::setw;using std::setiosflags;using std::setprecision;
#include <cmath>
int main(){ double amount, // amount on deposit principal = 1000.0, // starting principal rate = .05; // interest rate
利用 iomanip 中的setw setioflags setprecision來編排資料的格式
利用 cmath library 中的 pow來計算 數字的 power
cout << "Year" << setw( 21 ) << "Amount on deposit" << endl;
// set the floating-point number format cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
for ( int year = 1; year <= 10; year++ ) { amount = principal * pow( 1.0 + rate, year ); cout << setw( 4 ) << year << setw( 21 ) << amount << endl; }
return 0;}
// Fig. 2.22: fig02_22.cpp Counting letter grades#include <iostream>
using std::cout;using std::cin;using std::endl;
int main(){ int grade, // one grade aCount = 0, // number of A's bCount = 0, // number of B's cCount = 0, // number of C's dCount = 0, // number of D's fCount = 0; // number of F's
cout << "Enter the letter grades." << endl << "Enter the EOF character to end input." << endl;
while ( ( grade = cin.get() ) != EOF ) {
switch ( grade ) { // switch nested in while
case 'A': // grade was uppercase A case 'a': // or lowercase a ++aCount; break; // necessary to exit switch
case 'B': // grade was uppercase B case 'b': ++bCount; // or lowercase b break;
case 'C': // grade was uppercase C case 'c': ++cCount; // or lowercase c break;
case 'D': // grade was uppercase D case 'd': ++dCount; // or lowercase d break;
case 'F': // grade was uppercase F case 'f': ++fCount; // or lowercase f break;
case '\n': case '\t': // ignore newlines and tabs, case ' ': break; // and spaces in input
default: // catch all other characters cout << "Incorrect letter grade entered." << " Enter a new grade." << endl; break; // optional } } cout << "\n\nTotals for each letter grade are:" << "\nA: " << aCount << "\nB: " << bCount << "\nC: " << cCount << "\nD: " << dCount << "\nF: " << fCount << endl;
return 0;}
你能回答這些問題嗎 ?
你知道什麼時候該用 if/else 什麼時候該用 switch 嗎 ?
你知道 “ ? : “ 這個運算怎麼用嗎 ?
// Fig. 2.26: fig02_26.cpp// Using the break statement in a for structure#include <iostream>
using std::cout;using std::endl;
int main(){ int x; // x declared here so it can be used after the loop for ( x = 1; x <= 10; x++ ) {
if ( x == 5 ) break; // break loop only if x is 5
cout << x << " "; }
cout << "\nBroke out of loop at x of " << x << endl; return 0;}
// Fig. 2.27: fig02_07.cpp// Using the continue statement in a for structure#include <iostream>
using std::cout;using std::endl;
int main(){ for ( int x = 1; x <= 10; x++ ) {
if ( x == 5 ) continue; // skip remaining code in loop // only if x is 5
cout << x << " "; }
cout << "\nUsed continue to skip printing the value 5" << endl; return 0;}