Xem mẫu
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 control structures (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 continue enum
if short switch volatile
C++ only keywords asm
delete inline private static_cast try
wchar_t
break default extern int signed typedef while
bool dynamic_cast mutable protected template
typeid
case do float long sizeof union
catch explicit namespace public this
typename
char double for register static unsigned
class false new
reinterpret_cast throw
using
const else goto return struct void
const_cast friend operator
true
virtual
2003 Prentice Hall, Inc. All rights reserved.
4
if Selection Structure
• in C++
If student’s grade is greater than or equal to 60 Print “Passed”
if ( grade cout <<
>= 60 ) "Passed";
A decision can be made on any expression.
grade >= 60 true print “Passed”
zero - false
nonzero - true
Example:
false 3 - 4is true
2003 Prentice Hall, Inc. All rights reserved.
5
if/elseSelection 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 cout <<
else
cout <<
>= 60 ) "Passed";
"Failed";
2003 Prentice Hall, Inc. All rights reserved.
...
- tailieumienphi.vn
nguon tai.lieu . vn