Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Control Structures - I
Control Structures
Sequential execution
Transfer of control
When the next statement executed is not the next one in sequence
Sequence structure
Selection structures
Repetition structures
Control Structures
C++ keywords
break
default
extern
int
signed
typedef
while
case
do
float
long
sizeof
union
char
double
for
register
static
unsigned
const
else
goto
return
struct
void
bool
dynamic_cast
mutable
protected
template
typeid
catch
explicit
namespace
public
this
typename
class
false
new
reinterpret_cast
throw
using
const_cast
friend
operator
true
virtual
Control Structures
Flowchart
Connect exit point of one control structure to entry point of the next
(control-structure stacking).
Makes programs easy to build.
4
if ( grade >= 60 )
cout << "Passed";
grade >=
60
true
print Passed
false
if/else
A different action is performed when condition is true and
when condition is false
Psuedocode
if students grade is greater than or equal to 60
print Passed
else
print Failed
C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
grade >= 60
print Failed
true
print Passed
>= 60 )
"Passed.\n";
"Failed.\n";
"You must take this course again.\n";
Block
Logic errors
12
Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
13
true
product <=
1000
product = 2 * product
false
14
Formulating Algorithms
(Counter-Controlled Repetition)
Counter-controlled repetition
Definite repetition
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.
15
Formulating Algorithms
(Counter-Controlled Repetition)
Pseudocode for example:
16
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Class
grade: 98
grade: 76
grade: 71
grade: 87
grade: 83
grade: 90
grade: 57
grade: 79
grade: 82
grade: 94
average is 81
18
Control Structures - I
Saima Jabeen
19
Sentinel value
20
Initialize variables
Input, sum and count the quiz grades
Calculate and print the class average
21
Initialization
Processing
Termination
for
top-down
Initialize variables
to
Initialize total to zero
Initialize counter to zero
22
Refine
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
ios::showpoint
- forces decimal point and trailing zeros, even
Enter grade, -1 to end:
75
gradeCounter is an int, but
it
gets
promoted
to
if unnecessary:
66 printed
as 66.00
Enter
grade,
-1
to
end:
94
double.
setprecision(2) - prints only two digits
Enter grade, -1 to end: 97
past
decimal point.
| - separates multiple
option.
Enter grade, -1 to end: 88
Enter grade, -1 to end: 70
Programs that use this must include
<iomanip>
Enter grade, -1 to end: 64
Enter grade, -1 to end: 83
Enter grade, -1 to end: 89
Enter grade, -1 to end: -1
Class average is 82.50
26
Initialize variables
Input the ten quiz grades and count passes and failures
Print a summary of the exam results and decide if tuition
should be raised
Refine
Initialize variables
to
Initialize passes to zero
Initialize failures to zero
Initialize student counter to one
27
Refine
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 }
else
failures = failures + 1;
studentCounter = studentCounter + 1;
}
// termination phase
cout << "Passed " << passes << endl;
cout << "Failed " << failures << endl;
if ( passes > 8 )
cout << "Raise tuition " << endl;
return 0;
// successful termination
1
1
1
1
2
1
1
1
1
1
Assignment Operators
Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the
addition assignment operator
can be rewritten as
-=
*=
/=
%=
4
5
3
9
(d
(e
(f
(g
=
=
=
=
d
e
f
g
*
/
%
4)
5)
3)
9)
31
Preincrement
Postincrement
If c = 5, then
32
++c;
cout << c;
and
c++;
cout << c;
have the same effect.
33
References
Dietal and Dietal : How to Program C++
3rd Edition
34
References
Dietal and Dietal : How to Program C++
3rd Edition
35