Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.

Similar presentations


Presentation on theme: "CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the."— Presentation transcript:

1 CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the body of the loop. There are two basic types of repetition: 1. Repetition controlled by a counter 2. Repetition controlled by a logical expression

2 Counter-Controlled DO Loop General Form: DO Index = Initial value, Limit, Increment Statement 1. Statement n END DO -Index is a variable that could be real or integer. -Initial value or limit or increment may be constant, variables, or expressions that could be also real or integer types. -Increment can be either positive or negative, but not zero. -The number of repetitions is calculated as the larger of the value 0 and the value of

3 Example: The following DO-loop DO k=5, 83, 4 would be executed as the following number of times: but for the following DO-loop DO K=10, 2, 4 will not be executed (zero repetition) since the substitution in the formula will give a negative value.

4 Example: Write a DO-loop to execute the following summation: Solution: Sum=0 DO N=1, 50 Sum = Sum + N END DO

5 Nested DO Loop 1.A nested DO Loop cannot use the same index as the Loop that contains it. DOI=1,5DOI=1,5 DOI=1,3  DOK=1,3.. END DO  END DOEND DO 2.DO Loops that are independent of each other may have the same index. 3.When one loop in nested with another, the inside loop is completely executed each pass through the outer one. 4. Each DO Loop should be end by separate END DO statement as shown above.

6 General DO Loops General DO loop  number of iterations not determined DO-Exit Construct General form: DO statement-sequence 1 IF (logical-expression) EXIT statement-sequence 2 END DO statement-sequence 3 ! executed for true condition

7 Example (1) : Write a Fortran program segment to calculate the following summation: 2. Using If statement: Sum = 0.0 Number = 1 10 IF (Number <=50) THEN Sum = Sum + Number Number = Number + 1 GO TO 10 END IF 1. Using DO-Exit construct: Sum = 0.0 Number = 1 DO Sum = Sum + Number Number = Number + 1 IF (Number >50) EXIT END DO

8 Example (2): Write a Fortran program to calculate and print Y, where: Y = 5 Xm – 3 Xm-1 + 2 X-1 ForM=5,X=0.1, 0.2, 0.3,……………, 10 M=4, X=0.1, 0.2, 0.3,……………, 10 M=3,X=0.1, 0.2, 0.3,……………, 10 DO M=5,3,-1 DO I=1,100 X=I/10.0 Y=5.*X**m-3.*X**(m-1)+2.*x-1.0 Open(1,File=‘d:\example2.txt) Write(1,10) M, X, Y 10Format (5X, ‘M=’, I2,2X, ‘X=’, F7.3, 2X, ‘Y=’, F9.4) END DO END

9 Named DO Construct DO construct can be named in this way: Name: DO………………… END DO Name Example: Outer: Do M=1, last_M Inner: Do N=1, Last_N Product=M*N PRINT *, M,” “,N,” “, Product END DO Inner END DO Outer

10


Download ppt "CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the."

Similar presentations


Ads by Google