2003 Prentice Hall, Inc. All rights reserved.
5
if/else Selection Structure
• if
– Performs action if condition true
• if/else
– Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
2003 Prentice Hall, Inc. All rights reserved.
6
if/else Selection Structure
• Ternary conditional operator (?:)
– Three arguments (condition, value if true, value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” : “Failed” );
truefalse
print “Failed” print “Passed”
grade >= 60
Condition Value if true Value if false
2003 Prentice Hall, Inc. All rights reserved.
7
Nested
if/else
structures
•Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
2003 Prentice Hall, Inc. All rights reserved.
8
if/else Selection Structure
• Compound statement
– Set of statements within a pair of braces
if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
– Without braces,
cout << "You must take this course again.\n";
always executed
•Block
– Set of statements within braces
2003 Prentice Hall, Inc. All rights reserved.
9
while Repetition Structure
•Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
product <= 1000
product = 2 * product
true
false
2003 Prentice Hall, Inc. All rights reserved.
10
Formulating Algorithms (Counter-Controlled
Repetition)
• Counter-controlled repetition
– Loop repeated until counter reaches certain value
• Definite repetition
– Number of repetitions known
•Example
A class of ten students took a quiz. The grades (integers in
the range 0 to 100) for this quiz are available to you.
Determine the class average on the quiz.
2003 Prentice Hall, Inc.
All rights reserved.
2 // Class average program with counter-controlled repetition.
3 #include <iostream>
4 using std::cout;
5 using std::cin;
6 using std::endl;
7 // function main begins program execution
8 int main()
9 {
10 int total; // sum of grades input by user
11 int gradeCounter; // number of grade to be entered next
12 int grade; // grade value
13 int average; // average of grades
14 // initialization phase
15 total = 0; // initialize total
16 gradeCounter = 1; // initialize loop counter
17 // processing phase
18 while ( gradeCounter <= 10 ) { // loop 10 times
19 cout << "Enter grade: "; // prompt for input
20 cin >> grade; // read grade from user
21 total = total + grade; // add grade to total
22 gradeCounter = gradeCounter + 1; // increment counter
23 }
2003 Prentice Hall, Inc.
All rights reserved.
24 // termination phase
25 average = total / 10; // integer division
26 // display result
27 cout << "Class average is " << average << endl;
28 return 0; // indicate program ended successfully
29 }
Enter grade: 98
Enter grade: 76
Enter grade: 71
Enter grade: 87
Enter grade: 83
Enter grade: 90
Enter grade: 57
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81
2003 Prentice Hall, Inc. All rights reserved.
13
Formulating Algorithms (Sentinel-Controlled
Repetition)
• Suppose problem becomes:
Develop a class-averaging program that will process an
arbitrary number of grades each time the program is run
– Unknown number of students
– How will program know when to end?
• Sentinel value
– Indicates “end of data entry”
– Loop ends when sentinel input
– Sentinel chosen so it cannot be confused with regular input
• -1 in this case
2003 Prentice Hall, Inc. All rights reserved.
14
Formulating Algorithms (Sentinel-Controlled
Repetition)
• Top-down, stepwise refinement
– Begin with pseudocode representation of top
Determine the class average for the quiz
– Divide top into smaller tasks, list in order
Initialize variables
Input, sum and count the quiz grades
Calculate and print the class average
Không có nhận xét nào:
Đăng nhận xét