0% found this document useful (0 votes)
3 views31 pages

Week 6

Chapter 3 discusses structured program development in C, focusing on iteration statements, particularly the 'while' loop, which repeats actions while a condition is true. It also covers formulating algorithms for problems like calculating class averages using counter-controlled and sentinel-controlled iteration, as well as type conversions and assignment operators. Additionally, the chapter explains data types, including integer and floating-point types, and introduces increment and decrement operators.

Uploaded by

baranyardim3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Week 6

Chapter 3 discusses structured program development in C, focusing on iteration statements, particularly the 'while' loop, which repeats actions while a condition is true. It also covers formulating algorithms for problems like calculating class averages using counter-controlled and sentinel-controlled iteration, as well as type conversions and assignment operators. Additionally, the chapter explains data types, including integer and floating-point types, and introduces increment and decrement operators.

Uploaded by

baranyardim3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 3 Structured

Program
Development in C
Part II
Lecture#6

© 2016 Pearson Education, Ltd. All rights reserved. 1


3.7 The while Iteration Statement
• An iteration statement (also called an iteration 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 iteration 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.

© 2016 Pearson Education, Ltd. All rights reserved. 2


3.7 The while Iteration Statement (Cont.)
• As an example of a while statement, consider a program
segment designed to find the first power of 3 larger than
100.
• Suppose the integer variable product has been
initialized to 3.
• When the following while iteration statement finishes
executing, product will contain the desired answer:

product = 3 ;
whil e ( product <= 100 ) {
product = 3 * product;
} / / end while

• The flowchart of Fig. 3.4 illustrates the flow of control in


the while iterationstatement.

© 2016 Pearson Education, Ltd. All rights reserved. 3


© 2016 Pearson Education, Ltd. All rights reserved. 4
3.7 The while Iteration Statement (Cont.)

• When the while statement is entered, the value of


product is 3.
• The variable product is repeatedly multiplied by 3,
taking on the values 9, 27 and 81 successively.
• When product becomes 243, the condition inthe
while statement, product <= 100, becomes false.
• This terminates the iteration, and the final value of
product is 243.
• Program execution continues with the next
statement after the while.

© 2016 Pearson Education, Ltd. All rights reserved. 5


3.8 Formulating Algorithms Case Study 1:
Counter-Controlled Iteration
• To illustrate how algorithms are developed, we solve
several variations of a class-average problem.
• Consider the following problem statement:
– 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.
• The class average is equal to the sum of the grades
divided by the number of students.
• The algorithm for solving this problem on a
computer must
– input each of the grades,
– perform the average calculation,
– print the result.

© 2016 Pearson Education, Ltd. All rights reserved. 6


3.8 Formulating Algorithms Case Study 1:
Counter-Controlled Iteration (Cont.)
• Let’s use pseudocode to list the actions to
execute and specify the order in which these
actions should execute.
• We use counter-controlled iteration to input
the grades one at a time.
• This technique uses a variable called a counter
to specify the number of times a set of
statements should execute.
• In this example, iteration terminates when the
counter exceeds 10.
© 2016 Pearson Education, Ltd. All rights reserved. 7
© 2016 Pearson Education, Ltd. All rights reserved. 8
© 2016 Pearson Education, Ltd. All rights reserved. 9
© 2016 Pearson Education, Ltd. All rights reserved. 10
3.8 Formulating Algorithms Case Study 1:
Counter-Controlled Iteration (Cont.)
• Variables used to store totals should
normally be initialized to zero
before being used in a program;
otherwise the sum would include
the previous value stored in the
total’s memory location.
• Counter variables are normally
initialized to zero or one, depending
on their use.
• An uninitialized variable contains a
“garbage” value—the value last
stored in the memory location
reserved for that variable.

© 2016 Pearson Education, Ltd. All rights reserved. 11


3.9 Converting Between Types Explicitly and Implicitly

• Averages do not always evaluate to integer values.


• Often, an average is a value such as 7.2 or –93.5 that
contains a fractional part.
• These values are referred to as floating-point
numbers and can be represented by the data type
float.
• The variable average is defined to be of type
float to capture the fractional result of our
calculation.
• However, the result of the calculation t o t a l /
counter is an integer because t o t a l and counter
are both integer variables.

© 2016 Pearson Education, Ltd. All rights reserved. 12


3.9 Converting Between Types Explicitly and
Implicitly(Cont.)

• Dividing two integers results in integer


division in which any fractional part of the
calculation is truncated (i.e., lost).
• To produce a floating-point calculation with
integer values, we must create temporary
values that are floating-point numbers.
• C provides the cast operator to
accomplish this task.

© 2016 Pearson Education, Ltd. All rights reserved. 13


3.9 Formulating Algorithms with Top-Down,
Stepwise Refinement Case Study 2:
Sentinel-Controlled Iteration (Cont.)
• Casting:
• average = ( f l o a t ) t o t a l / 10;

• The value stored in t o t a l is still an integer.


• Using a cast operator in this manner is
called explicit conversion.

© 2016 Pearson Education, Ltd. All rights reserved. 14


Optional

© 2016 Pearson Education, Ltd. All rights reserved. 15


3.9 Formatting Floating-Point Numbers

• Example code uses the p r i n t f conversion specifier


%.2f to print the value of average.
• The f specifies that a floating-point value will be printed.
• The . 2 is the precision with which the value will be
displayed—with 2 digits to the right of the decimal
point.
• p r i n t f ( "%.2f\n", 3.446 ) ; / / prints 3.45
p r i n t f ( "%.1f\n", 3.446 ) ; / / prints 3.4

© 2016 Pearson Education, Ltd. All rights reserved. 16


Sentinel-Controlled Iteration

• Let’s generalize the class-average problem.


• Consider the following problem:
– Develop a class-average program that will process an
arbitrary number of grades each time the program is
run.
• In the first class-average example, the number
of grades (10) was known in advance.
• In this example, the program must process an
arbitrary number of grades.
• How can the program determine when to stop
the input of grades? How will it know when to
calculate and print the class average?

© 2016 Pearson Education, Ltd. All rights reserved. 17


Sentinel-Controlled Iteration (Cont.)

• One way to solve this problem is to use a


special value called a sentinel value (also called
a signal value, a dummy value, or a flag value)
to indicate “end of data entry.”
• The user types in grades until all legitimate
grades have been entered.
• The user then types the sentinel value to
indicate “the last grade has been entered.”
• Sentinel-controlled iteration is often called
indefinite iteration because the number of
iterations isn’t known before the loop begins
executing.

© 2016 Pearson Education, Ltd. All rights reserved. 18


Sentinel-Controlled Iteration (Cont.)

• Clearly, the sentinel value must be chosen so that it


cannot be confused with an acceptable input value.
• Because grades on a quiz are normally nonnegative
integers, –1 is an acceptable sentinel value for this
problem.
• Thus, a run of the class-average program might
process a stream of inputs such as 95, 96, 75, 74, 89
and –1.
• The program would then compute and print the
class average for the grades 95, 96, 75, 74, and 89 (–
1 is the sentinel value, so it should not enter into the
average calculation).

© 2016 Pearson Education, Ltd. All rights reserved. 19


© 2016 Pearson Education, Ltd. All rights reserved. 20
© 2016 Pearson Education, Ltd. All rights reserved. 21
3.10 Data Types (Integer Types)
Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

int 2 or 4bytes -32,768 to 32,767 or

-2,147,483,648 to 2,147,483,647
unsigned int 2 or 4bytes 0 to 65,535or

0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647

unsigned long 4 bytes 0 to 4,294,967,295


© 2016 Pearson Educa tion, Ltd. All rights reserved. 22
3.10 Data Types (Floating Point Types)

Type Storage size Value range Precision


float 4 byte 1.2E-38 to 6 decimal
3.4E+38 places

double 8 byte 2.3E-308 to 15 decimal


1.7E+308 places

long double 10 byte 3.4E-4932 to 19 decimal


1.1E+4932 places

© 2016 Pearson Education, Ltd. All rights reserved. 23


3.11 Assignment Operators
• C provides several assignment operators
for abbreviating assignment expressions.
• For example, the statement
• c = c + 3;
• can be abbreviated with the addition
assignment operator += as
c += 3;

• The += operator adds the value ofthe expression on


the right of the operator to the value of the variable on
the left of theoperator and stores the result in the
variable on the left of the operator.

© 2016 Pearson Education, Ltd. All rights reserved. 24


© 2016 Pearson Education, Ltd. All rights reserved. 25
3.12 Increment and Decrement Operators

• C also provides the unary increment operator, ++,


and the unary decrement operator, - - , which are
summarized in Fig. 3.12.
• If a variable c is to be incremented by 1, the increment
operator ++ can be used rather than the expressions c =
c + 1 or c += 1.
• If increment or decrement operators are placed before a
variable (i.e., prefixed), they’re referred to as the
preincrement or predecrement operators, respectively.
• If increment or decrement operators are placed after a
variable (i.e., postfixed), they’re referred to as the
postincrement or postdecrement operators, respectively.

© 2016 Pearson Education, Ltd. All rights reserved. 26


3.12 Increment and Decrement Operators
(Cont.)
• Preincrementing (predecrementing) a variable
causes the variable to be incremented
(decremented) by 1, then the new value of the
variable is used in the expression in which it
appears.
• Postincrementing (postdecrementing) the
variable causes the current value of the
variable to be used in the expression in which
it appears, then the variable value is
incremented (decremented) by 1.

© 2016 Pearson Education, Ltd. All rights reserved. 27


© 2016 Pearson Education, Ltd. All rights reserved. 28
3.12 Increment and Decrement Operators
(Cont.)
• Figure 3.13 demonstrates the difference
between the preincrementing and the
postincrementing versions of the ++ operator.
• Postincrementing the variable c causes it to be
incremented after it’s used in the p r i n t f
statement.
• Preincrementing the variable c causes it to be
incremented before it’s used in the p r i n t f
statement.

© 2016 Pearson Education, Ltd. All rights reserved. 29


© 2016 Pearson Education, Ltd. All rights reserved. 30
© 2016 Pearson Education, Ltd. All rights reserved. 31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy