Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to...

29
Chapter 4

Transcript of Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to...

Chapter 4

Loops and Character Manipulation

Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once.

Type of loops:

• While loops

The block of statement is repeated indefinitely as long as a condition is satisfied.

DO, DO/WHILE

• Iterative (counting) loops

The block of statements is repeated specific number of times

DO i=1, 3, 1

What is the difference between a loop and GO TO statement?

DO……

IF ( Logical_expr) EXIT……END DO

StatementStatement

...Statement

Logical_expr

StatementStatement

...Statement

.FALSE.

.TRUE.

DOLOOP

Loop entry point

Loop exit point

Write a program that asks the user to enter values, one by one. When the value entered is negative, the program stops and prints the mean of all values.

X < 0

n ß n + 1sum_x ß sum_x + x

.FALSE.

.TRUE.

READ X

START

Calculate x_mean

Write x_mean

END

DOLOOP

DOLOOP

PROGRAM MEANIMPLICIT NONE

REAL :: x ! value entered each time by userREAL :: n = 0 ! counts number of values enteredREAL :: sum_x = 0 ! accumulates values entered by user

DO

WRITE (*,*) "Enter value of x = " WRITE (*,*)

READ (*,*) x

IF (x < 0) EXIT

n = n + 1 sum_x = sum_x + x

END DO

IF (n/=0) WRITE (*,*) "The mean of all values = ", sum_x / n

END PROGRAM

DOLOOP

PROGRAM MEANIMPLICIT NONEREAL :: x ! value entered each time by userREAL :: n = 0 ! counts number of values enteredREAL :: sum_x = 0 ! accumulates values entered by user

DO

WRITE (*,*) "Enter value of x = " WRITE (*,*)

READ (*,*) x

IF (x >= 0) THEN

n = n + 1 sum_x = sum_x + x

ELSE EXIT END IF

END DO

IF (n/=0) WRITE (*,*) "The mean of all values = ", sum_x / nEND PROGRAM

n = n + 1 sum_x = sum_x + x

EXIT

n = n + 1sum_x = sum_x + x

DOLOOP

PROGRAM test_1IMPLICIT NONE

REAL :: xREAL :: sum_x = 0

DO

WRITE (*,*) "Enter value of x = "

READ (*,*) x

sum_x = sum_x + x

END DO

WRITE (*,*) "The mean of all values = ", sum_x

END PROGRAM

What is the output of this program?

Non-Terminating

Input data:

3 4 5

2 0 -1

0 2 3

3 5 4

Output:

???

DOLOOP

PROGRAM test_1IMPLICIT NONE

REAL :: xREAL :: sum_x = 0

DO

WRITE (*,*) "Enter value of x = "

READ (*,*) x IF (x <= 0) EXIT

sum_x = sum_x + x

END DO

WRITE (*,*) "The mean of all values = ", sum_x

END PROGRAM

What is the output of this program?

Input data:

3 4 5

2 0 -1

0 2 3

3 5 4

Output:

5

ProblemWrite a program that asks the user to enter six courses (course code and its number of credits). The program should be designed using DO loop which is employed to sum total number of credits.

PROGRAM credit_counterIMPLICIT NONE

CHARACTER (LEN=8) course_codeINTEGER :: credits, sum_credits, course_num=0

DO

WRITE (*,*) "Enter course code and credits " READ (*,*) course_code, credits

course_num=course_num+1 sum_credits=sum_credits+credits IF (course_num >= 5) EXIT

END DO

WRITE (*,*) "Total number of credits = ", sum_credits

END PROGRAM

PROGRAM credit_counterIMPLICIT NONE

CHARACTER (LEN=8) course_codeINTEGER :: credits, sum_credits, course_num=0

DO

WRITE (*,*) "Enter course code and credits " READ (*,*) course_code, credits

course_num=course_num+1 sum_credits=sum_credits+credits IF (course_num >= 5) EXIT

END DO

WRITE (*,*) "Total number of credits = ", sum_credits

END PROGRAM

PROGRAM credit_counterIMPLICIT NONE

CHARACTER (LEN=8) course_codeINTEGER :: credits, sum_credits, course_num=0

DO

WRITE (*,*) "Enter course code and credits " READ (*,*) course_code, credits

course_num=course_num+1 sum_credits=sum_credits+credits IF (course_num >= 5) EXIT

END DO

WRITE (*,*) "Total number of credits = ", sum_credits

END PROGRAM

PROGRAM credit_counterIMPLICIT NONE

CHARACTER (LEN=8) course_codeINTEGER :: credits, sum_credits=0, course_num=0

DO

WRITE (*,*) "Enter course code and credits " READ (*,*) course_code, credits

course_num=course_num+1 sum_credits=sum_credits+credits IF (course_num >= 5) EXIT

END DO

WRITE (*,*) "Total number of credits = ", sum_credits

END PROGRAM

PROGRAM credit_counterIMPLICIT NONE

CHARACTER (LEN=8) course_codeINTEGER :: credits, sum_credits=0, course_num=0

DO

WRITE (*,*) "Enter course code and credits " READ (*,*) course_code, credits

course_num=course_num+1 sum_credits=sum_credits+credits IF (course_num >= 5) EXIT

END DO

WRITE (*,*) "Total number of credits = ", sum_credits

END PROGRAM

PROGRAM credit_counterIMPLICIT NONE

CHARACTER (LEN=8) course_codeINTEGER :: credits, sum_credits=0, course_num=0

DO

WRITE (*,*) "Enter course code and credits " READ (*,*) course_code, credits

course_num=course_num+1 sum_credits=sum_credits+credits IF (course_num > 5) EXIT

END DO

WRITE (*,*) "Total number of credits = ", sum_credits

END PROGRAM

DO WHILE (Logical_expr) ………END DO

DO WHILELOOP

Logical_expr

StatementStatement

…Statement

.TRUE.

.FALSE.

StatementStatement

…Statement

DO WHILELOOP

DOLOOP

DO WHILE (Logical_expr) ………END DO

DO……

IF ( Logical_expr) EXIT……END DO

Loop exit point

Loop entry point

DO WHILELOOP

Logical_expr

StatementStatement

…Statement

.TRUE.

.FALSE.

StatementStatement

…Statement

StatementStatement

...Statement

Logical_expr

StatementStatement

...Statement

.FALSE.

.TRUE.

DOLOOP

Write a program that asks the user to enter 10 values, one by one. After entering the tenth value,

the program stops and prints the sum of all values.

PROGRAM MEANIMPLICIT NONE

REAL :: x, sum_x = 0INTEGER :: n=0

DO WHILE (n/=10)

WRITE (*,*) "Enter value of x = " READ (*,*) x sum_x = sum_x + x n=n+1

END DO

WRITE (*,*) "The sum of all values = ", sum_x

END PROGRAM

PROGRAM MEANIMPLICIT NONE

REAL :: x, sum_x = 0INTEGER :: n=0

DO WHILE (n<10)

WRITE (*,*) "Enter value of x = " READ (*,*) x sum_x = sum_x + x n=n+1

END DO

WRITE (*,*) "The sum of all values = ", sum_x

END PROGRAM

PROGRAM MEANIMPLICIT NONE

REAL :: x, sum_x = 0INTEGER :: n=0

DO WHILE (n/=10)

WRITE (*,*) "Enter value of x = " READ (*,*) x sum_x = sum_x + x n=n+1

END DO

WRITE (*,*) "The sum of all values = ", sum_x

END PROGRAM

Can You convert it into DO loop?

Write a program that asks the user to enter 10 values, one by one. After entering the tenth value,

the program stops and prints the sum of all values.

PROGRAM MEANIMPLICIT NONE

REAL :: x, sum_x = 0INTEGER :: n=0

DO

WRITE (*,*) "Enter value of x = " READ (*,*) x sum_x = sum_x + x n=n+1IF (n>=10) EXIT

END DO

WRITE (*,*) "The sum of all values = ", sum_x

END PROGRAM

PROGRAM MEANIMPLICIT NONE

REAL :: x, sum_x = 0INTEGER :: n=0

DO WHILE (n<10)

WRITE (*,*) "Enter value of x = " READ (*,*) x sum_x = sum_x + x n=n+1

END DO

WRITE (*,*) "The sum of all values = ", sum_x

END PROGRAM

DO index = istart, iend, incr…………END DO

Iterative / CountingLOOP

.FALSE.

StatementStatement

…Statement

.TRUE.

index = istart

incr

index ≤ iend * incr

Iterative / CountingLOOP

Number of Iterations = (iend – istart + incr ) / incr

Number of Iterations = (iend – istart) / incr + 1

Iterative / CountingLOOP

program COUNTER

integer :: i

do i = 1, 10

write (*,*) i

end do

end program

Number of Iterations = (10 – 1 + 1 ) / 1 = 10

Iterative / CountingLOOP

program COUNTER

integer :: i

do i = 1, 10, 1

write (*,*) i

end do

end programNumber of Iterations = (10 – 1 + 1 ) / 1 = 10

Iterative / CountingLOOP

program COUNTER

integer :: i

do i = 1, 10, 2

write (*,*) i

end do

end programNumber of Iterations = (10 – 1 + 2 ) / 2 = 5.5 = 5

Iterative / CountingLOOP

program COUNTER

integer :: i

do i = 10, 1, -1

write (*,*) i

end do

end programNumber of Iterations = (1 – 10 + (-1) ) / (-1) = 10

Iterative / CountingLOOP

program COUNTER

integer :: i

do i = 10, 1, -1

i = 5

write (*,*) i

end do

end programNumber of Iterations = ?

Iterative / CountingLOOP

program COUNTER

integer :: i

do i = 3, 2

write (*,*) i

end do

end programNumber of Iterations = (2 – 3 + 1 ) / 1 = 0

EXIT vs. CYCLE

PROGRAM test

INTEGER :: i

Do i = 1, 5

IF (i==3) CYCLE

WRITE(*,*) i

END DO

WRITE(*,*) ‘END of loop’

END PROGRAM

__________________________

PROGRAM testINTEGER :: iDo i = 1, 5IF (i==3) EXITWRITE(*,*) iEND DO

WRITE(*,*) ‘END of loop’END PROGRAM

__________________________

1245END of loop

12END of loop

They can be used inside DO construct only

Named LoopsPROGRAM test

INTEGER :: i

myLoop: Do i = 1, 5

IF (i==3) CYCLE myLoop

WRITE(*,*) i

END DO myLoop

WRITE(*,*) ‘END of loop’

END PROGRAM

PROGRAM test

INTEGER :: i

name: Do i = 1, 5

IF (i==3) EXIT name

WRITE(*,*) i

END DO name

WRITE(*,*) ‘END of loop’

END PROGRAM

Name must be unique and up to 31 alphanumeric

If the loop is given a name then END DO must have that name but EXIT and CYCLE can be optionally assigned that name

Nested Loops• Loops can appear inside other loops• Indexes of the nested loops must be independent• It is not possible to change the index inside the loop and hence it

will not be possible to use same index in nested loops

PROGRAM nestedINTEGER :: i, j, productDO i = 1, 3

DO j = 1, 2product = i * jWRITE(*,*) i ,’ * ’, j , ‘ = ’, product

END DOEND DOEND PROGRAM______________________________Write the output:

Inner loop must lie completely inside the outer loop

EXIT and CYCLE in Nested Loops

PROGRAM nested

INTEGER :: i, j, p

DO i = 1, 3

DO j = 1, 3

if (j == 2) CYCLE

p = i * j

WRITE(*,*) i ,’ * ’, j , ‘ = ’, p

END DO

END DO

END PROGRAM

PROGRAM nested

INTEGER :: i, j, p

DO i = 1, 3

DO j = 1, 3

if (j == 2) EXIT

p = i * j

WRITE(*,*) i ,’ * ’, j , ‘ = ’, p

END DO

END DO

END PROGRAM

Named Nested Loops

• Good practice so that compiler detects which loop has error

PROGRAM nestedINTEGER :: i, j, product

loop1: DO i = 1, 3loop2: DO j = 1, 2

product = i * j WRITE(*,*) i ,’ * ’, j , ‘ = ’, product

END DO loop2END DO loop1END PROGRAM

______________________________

What will happen if this line is missing?

Review Nesting Constructs

• Nested IF

• Nested CASE

• Nested DO loop

IF (expr1) Then IF (expr2) Then IF (expr3) Then END IF END IF IF (expr4) Then END IFEND IF

SELECT CASE (var1) CASE (0) SELECT CASE (var2) CASE (1) CASE DEFAULT END SELECT CASE DEFAULT SELECT CASE (var3) CASE (0) CASE DEFAULT END SELECTEND SELECT

DO DO DO DO END DO END DO

END DO DO END DO DO END DOEND DO

Mixed Nesting Constructs

IF (expr1) Then DO WHILE (expr3) IF (expr4) Then … END IF .. END DO … Elseif (expr2) Then … DO i=1, 10, 0.5 … SELECT CASE (var1) CASE (value) DO END DO … CASE DEFAULT … END SELECT END DO ELSE SELECT CASE(var2) CASE (value) IF (expr5) Then … END IF CASE DEFAULT END SELECT … END IF