0% found this document useful (0 votes)
65 views4 pages

UNIT-II Repetition in Programs

Here are potential loop types for the problems: a. Counting loop, since we know beforehand there are 35 students. b. Sentinel-controlled loop, since input is ended by a special ID value of 0. c. Endfile-controlled loop, since input comes from a single data file of unknown length.

Uploaded by

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

UNIT-II Repetition in Programs

Here are potential loop types for the problems: a. Counting loop, since we know beforehand there are 35 students. b. Sentinel-controlled loop, since input is ended by a special ID value of 0. c. Endfile-controlled loop, since input comes from a single data file of unknown length.

Uploaded by

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

Repetition and Loop Statements CHAPTER

CHAPTER OBJECTIVES
• To understand why repetition is an important control
structure in programming
5
• To learn about loop control variables and the three steps
needed to control loop repetition
• To learn how to use the C for, while, and do-while
statements for writing loops and when to use each
statement type
• To learn how to accumulate a sum or a product within a
loop body
• To learn common loop patterns such as counting loops,
sentinel-controlled loops, and flag-controlled loops
• To understand nested loops and how the outer loop con-
trol variable and inner loop control variable are changed
in a nested loop
• To learn how to debug programs using a debugger
• To learn how to debug programs by adding diagnostic
output statements
I n your programs so far, the statements in the program body execute only once.
However, in most commercial software that you use, you can repeat a process many
times. For example, when using an editor program or a word processor, you can move
the cursor to a program line and perform as many edit operations as you need to.
Repetition, you’ll recall, is the third type of program control structure
(sequence, selection, repetition), and the repetition of steps in a program is called a
loop a control loop. In this chapter we describe three C loop control statements: while, for, and
structure that repeats do-while. In addition to describing how to write loops using each statement, we
a group of steps in a
program describe the advantages of each and explain when it is best to use each one. Like if
statements, loops can be nested, and the chapter demonstrates how to write and use
nested loops in your programs.

5.0 Repetition in Programs


Just as the ability to make decisions is an important programming tool, so is the abil-
ity to specify repetition of a group of operations. For example, a company that has
seven employees will want to repeat the gross pay and net pay computations in its
loop body the payroll program seven times, once for each employee. The loop body contains the
statements that are statements to be repeated.
repeated in the loop
Writing out a solution to a specific case of a problem can be helpful in prepar-
ing you to define an algorithm to solve the same problem in general. After you solve
the sample case, ask yourself some of the following questions to determine whether
loops will be required in the general algorithm:
1. Were there any steps I repeated as I solved the problem? If so, which ones?
2. If the answer to question 1 is yes, did I know in advance how many times to
repeat the steps?
3. If the answer to question 2 is no, how did I know how long to keep repeating
the steps?
Your answer to the first question indicates whether your algorithm needs a loop
and what steps to include in the loop body if it does need one. Your answers to the
other questions will help you determine which loop structure to choose for your
solution. Figure 5.1 diagrams the relationship between these questions and the type
of loop you should choose. Table 5.1 defines each of the kinds of loops you may
need and refers you to the sections(s) of this chapter where you will find implemen-
tations of these loops.
5.1 • Repetition in Programs 237

FIGURE 5.1

Flow Diagram
of Loop Choice
Process Any steps No No loop
repeated? required

Yes

Use one of the conditional loops:


sentinel-controlled
Know in advance No
endfile-controlled
how many times input validation
to repeat?
general conditional

Yes

Use a counting
loop

TABLE 5.1 Comparison of Loop Kinds


C Section
Implementation Containing
Kind When Used Structures an Example

Counting loop We can determine before loop execution while 5.2


exactly how many loop repetitions will be for 5.4
needed to solve the problem.
Sentinel-controlled loop Input of a list of data of any length ended while, for 5.6
by a special value
Endfile-controlled loop Input of a single list of data of any length while, for 5.6
from a data file
Input validation loop Repeated interactive input of a data value do-while 5.8
until a value within the valid range is entered
General conditional loop Repeated processing of data until a desired while, for 5.5
condition is met
EXERCISES FOR SECTION 5.1

Self-Check
1. Choose an appropriate kind of loop from Table 5.1 for solving
each of the fol- lowing problems.
a. Calculate the sum of the test scores of a class of 35
students. (Hint: Initialize sum to zero before
entering loop.)
b. Print weekly paychecks for a list of employees. The
following data are to be entered interactively for each
employee: ID, hours worked, and hourly pay rate. An ID of
zero marks the end of the data.
c. Process a data file of Celsius temperatures. Count how
many are above 100°C.

You might also like