Chapter6 - Chapter 6 COMPLEX CONTROL STRUCTURES In the last...

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

View Full Document Right Arrow Icon
Chapter 6 COMPLEX CONTROL STRUCTURES In the last chapter we introduced the two kinds of statements that cause an alteration from the linear flow of control in a program. One type caused looping, the counted loop or the conditional loop; the other caused selective execution, the if then ... else . Learning to handle these two kinds of instructions is absolutely essential to programming. And learning to handle them in a systematic way is essential to structured programming. BASIC STRUCTURE OF LOOPS It is hard to appreciate, when you first learn a concept like loops, that all loops are basically the same. They consist of a sequence of statements in the program that: 1. initialize the values of certain variables that are to be used in the loop. These consist of assigning values to: (a) variables that appear in the condition of a do while ( condition ) and in the if ( condition ) exit (b) variables that appear in the body of the loop on the right-hand side of assignment statements. 2. indicate that a loop is to commence and give the information that is to control the number of repetitions. Both types of loops contain a control phrase. The control phrase may be of two types: (a) for the counted do, it is, for example i = 1, 20, 1 (b) for the conditional loops, it is, for example do while (i .le. 20) or if (i .gt. 20) The condition should contain at least one variable.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 | COMPLEX CONTROL STRUCTURES 3. give the list of statements, called the body of the loop, that are to be executed each time the loop is repeated. If the loop is controlled by a do while ( condition ) or an if ( condition ), then within the body of the loop there must be some statements that assign new values to the variables appearing in the condition. Usually there is only one variable and its value may be changed by either (a) an assignment or (b) a read statement. 4. indicate the end of the loop. This is the end do for a counted do and the conditional loops. At this point control is returned to the beginning of the loop with its control phrase. 5. give the next statement to be executed once the looping has been carried out the required numbers of times. Control goes from the beginning of the loop to this statement when (a) the condition in the do while ( condition ) is found to be false or the if ( condition ) exit is found to be true, or (b) the value of the index controlling the counted do plus the incre- ment is beyond the test value indicated after the first comma in the do . It is to be noted very carefully that there is no exit from either kind of loop, except from the beginning of the loop itself or from the exit statement that may appear somewhere within the body of the loop, and this exit goes to the statement immediately after the end do . In this way we keep track of control flow and never have the possibility of getting confused about its path. The complete Fortran 77 language offers unrestricted use of the go to statement for altering the path of control. It permits you to send control anywhere in your program. Since computer
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.

Page1 / 12

Chapter6 - Chapter 6 COMPLEX CONTROL STRUCTURES In the last...

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