Chapter 3 - Structured Program Development in C
Chapter 3 - Structured Program Development in C
Chapter 3
Structured Program
Development in C
if ( grade >= 90 )
puts( "A" );
else if ( grade >= 80 )
puts( "B" );
else if ( grade >= 70 )
puts( "C" );
else if ( grade >= 60 )
puts( "D" );
else
puts( "F" );
19
END OF
LESSON ONE
20
3.7 While Repetition Statement
A repetition statement (also called an iteration or looping
statement) allows you to specify that an action is to be
repeated while some condition remains true.
The pseudocode statement
While there are more items on my shopping list
Purchase next item and cross it off my list
describes the repetition that occurs during a shopping trip.
The condition, “there are more items on my shopping list”
may be true or false.
If it’s true, then the action, “Purchase next item and cross
it off my list” is performed.
This action will be performed repeatedly while the
condition remains true.
21
3.7 While Repetition Statement (Cont.)
When the following while repetition statement finishes executing,
product will contain the desired answer:
int product = 3;
while ( product <= 100 )
{
product = 3 * product;
}
The flowchart of Fig. 3.4 illustrates the flow of control in the while
repetition statement.
22
3.8 Assignment Operators
C provides several assignment operators for abbreviating assignment
expressions.
The += operator adds the value of the expression on the right of the
operator to the value of the variable on the left of the operator and
stores the result in the variable on the left of the operator.
Any statement of the form
variable = variable operator expression;
in which the same variable appears on both sides of the assignment
operator and operator is one of the binary operators +, -, *, /, or %
(or others we’ll discuss later in the text), can be written in the form
variable operator= expression;
Thus the assignment c += 3 adds 3 to c.
Figure 3.11 shows the arithmetic assignment operators, sample
expressions using these operators and explanations.
23
3.8 Assignment Operators (Cont.)
24
3.9 Increment and Decrement Operators
C also provides two unary operators for adding 1 to or
subtracting 1 from the value of a numeric variable.
These are the unary increment operator, ++, and the unary
decrement operator, --, which are summarized in Fig. 3.12.
25
THANKS!
Any Questions?
26
ASSIGNMENT 2