Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 60 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
60
Dung lượng
244,39 KB
Nội dung
2003 Prentice Hall, Inc. All rights reserved.
1
Chapter 2-Control Structures
Outline
Control Structures
if Selection Structure
if/else Selection Structure
while Repetition Structure
Formulating Algorithms: Case Study 1 (Counter-Controlled
Repetition)
Formulating Algorithms with Top-Down, Stepwise Refinement:
Case Study 2 (Sentinel-Controlled Repetition)
Formulating Algorithms with Top-Down, Stepwise Refinement:
Case Study 3 (Nested Control Structures)
Assignment Operators
Increment and Decrement Operators
Essentials of Counter-Controlled Repetition
for Repetition Structure
switch Multiple-Selection Structure
do/while Repetition Structure
break and continue Statements
Logical Operators
Confusing Equality (==) and Assignment (=) Operators
Structured-Programming Summary
2003 Prentice Hall, Inc. All rights reserved.
2
Control Structures
• Sequential execution
– Statements executed in order
• Transfer of control
– Next statement executed not next one in sequence
• 3 controlstructures (Bohm and Jacopini)
– Sequence structure
• Programs executed sequentially by default
– Selection structures
• if, if/else, switch
– Repetition structures
• while, do/while, for
2003 Prentice Hall, Inc. All rights reserved.
3
Control Structures
• C++ keywords
– Cannot be used as identifiers or variable names
C++ Keywords
Keywords common to the
C and C++ programming
languages
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
C++ only keywords
asm
bool
catch
class
const_cast
delete
dynamic_cast
explicit
false
friend
inline
mutable
namespace new
operator
private
protected
public
reinterpret_cast
static_cast
template
this
throw
true
try
typeid
typename
using
virtual
wchar_t
2003 Prentice Hall, Inc. All rights reserved.
4
if Selection Structure
•inC++
If student’s grade is greater than or equal to 60
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
true
false
grade >= 60
print “Passed”
A decision can be made on
any expression.
zero - false
nonzero - true
Example:
3 - 4 is true
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.
[...]... pass, pass, 2 222222222 = = = = = = = = = = fail): fail): fail): fail): fail): fail): fail): fail): fail): fail): 1 22 1 1 1 2 1 1 2 = = = = = = = = = = pass, pass, pass, pass, pass, pass, pass, pass, pass, pass, 2 222222222 = = = = = = = = = = fail): fail): fail): fail): fail): fail): fail): fail): fail): fail): 1 1 1 1 2 1 1 1 1 1 20 03 Prentice Hall, Inc All rights reserved 25 Assignment... int result; // one exam result 15 // process 10 students using counter-controlled loop 16 while ( studentCounter > result; 20 03 Prentice Hall, Inc All rights reserved 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // if result 1, increment passes; if/else nested in while if... 6 20 03 Prentice Hall, Inc All rights reserved 30 Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires – – – – Name of control variable/loop counter Initial value of control variable Condition to test for final value Increment/decrement to modify control variable when looping 20 03 Prentice Hall, Inc All rights reserved 1 // Fig 2. 16: fig 02_ 16.cpp 2 // Counter-controlled... while ( gradeCounter grade; // read grade from user 21 total = total + grade; // add grade to total 22 gradeCounter = gradeCounter + 1; // increment counter 23 } 20 03 Prentice Hall, Inc All rights reserved 24 25 26 27 28 29 // termination phase average = total / 10; // integer division // display result cout . 20 03 Prentice Hall, Inc. All rights reserved.
1
Chapter 2 - Control Structures
Outline
Control Structures
if Selection Structure
. Case Study 1 (Counter-Controlled
Repetition)
Formulating Algorithms with Top-Down, Stepwise Refinement:
Case Study 2 (Sentinel-Controlled Repetition)