Chapter5 - Chapter 5 CONTROL STRUCTURES In all programs we...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 5 CONTROL STRUCTURES In all programs we have examined so far the statements were executed in sequence until the stop was reached, at which time the program execution was terminated. In this chapter we will learn two ways in which the sequence of statements may be altered. One involves the repetitious use of statements; the other involves a selection between alternate sets of statements. The first is called a loop or repetition , the second a selection. We speak of the flow of control since it is the control unit of the computer that determines which statement is to be executed next by the computer. COUNTED LOOPS The normal flow of control in a program is in a straight line. We can, however, give a statement that will cause a set of statements to be repeated. In the last chapter, in the example where we were reading information about boxes of soap flakes, we had to write the statements over and over to get repetition. A statement that will produce repetition is the counted do loop . For our example we could have written do i = 1, 3 read* , cost, weight kgcost = cost/(weight*1.) + .5 print* , kgcost end do The three statements that we had to repeat three times are prefaced by do i = 1, 3 and followed by end . The variable i is an index, is usually an integer variable, and counts the number of repetitions. First the index i is set to 1, then the three statements are executed. When the end is reached, control is sent back to
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 | CHAPTER 5 the do . At this time the index i is increased by 1, making it 2. A test is made to see if this value of the index is greater than the 3 which appears in the do statement. The 3 is called the test value. Since 2 is not greater than 3 we will proceed. The three statements are again executed and, at end , back we go to the do . This time i becomes 3 which is not greater than the test va1ue and a third execution of the three statements in the do loop takes place. When control returns to the do , this time i would become 4 and this is found to be larger than the 3 which is the upper limit of the count. When this happens, control goes out of the loop to the next statement after the end . A counted or indexed do loop is used whenever we know exactly how many repetitions we want to take place. We do not need to count by ones or start the count at 1. We could, have for example, do count = 12, 24, 2 Here we have called the index count and are starting at 12 and going by 2s up to, and to include, 24. If the increment by which you are counting is not 1 it is included in the do following the test value. So far each of the initial value, the test value, and the increment have been positive integers or variables which have positive integer values. We can also count backwards by using a negative increment or have expressions having integer values for any of the initial, test, or increment. As well it is possible, though not advisable, to use real expressions. When a do loop is first entered, the expressions for the initial, test, and increment are
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 20

Chapter5 - Chapter 5 CONTROL STRUCTURES In all programs we...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online