 # 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.

## Presentation on theme: "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."— Presentation transcript:

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 DO LOOP 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. DO LOOP

PROGRAM MEAN IMPLICIT NONE REAL :: x ! value entered each time by user REAL :: n = 0 ! counts number of values entered REAL :: 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

DO LOOP PROGRAM MEAN IMPLICIT NONE REAL :: x ! value entered each time by user REAL :: n = 0 ! counts number of values entered REAL :: 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 / n END PROGRAM n = n + 1 sum_x = sum_x + x EXIT n = n + 1 sum_x = sum_x + x

DO LOOP PROGRAM test_1 IMPLICIT NONE REAL :: x REAL :: 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: ???

DO LOOP PROGRAM test_1 IMPLICIT NONE REAL :: x REAL :: 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

Problem Write 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_counter IMPLICIT NONE CHARACTER (LEN=8) course_code INTEGER :: 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_counter IMPLICIT NONE CHARACTER (LEN=8) course_code INTEGER :: 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_counter IMPLICIT NONE CHARACTER (LEN=8) course_code INTEGER :: 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_counter IMPLICIT NONE CHARACTER (LEN=8) course_code INTEGER :: 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_counter IMPLICIT NONE CHARACTER (LEN=8) course_code INTEGER :: 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_counter IMPLICIT NONE CHARACTER (LEN=8) course_code INTEGER :: 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 WHILE LOOP

DO LOOP DO WHILE (Logical_expr) … … … END DO DO … … IF ( Logical_expr) EXIT … … END DO Loop exit point Loop entry point

DO WHILE LOOP 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 MEAN IMPLICIT NONE REAL :: x, sum_x = 0 INTEGER :: 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 MEAN IMPLICIT NONE REAL :: x, sum_x = 0 INTEGER :: 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 MEAN IMPLICIT NONE REAL :: x, sum_x = 0 INTEGER :: 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 MEAN IMPLICIT NONE REAL :: x, sum_x = 0 INTEGER :: n=0 DO WRITE (*,*) "Enter value of x = " READ (*,*) x sum_x = sum_x + x n=n+1 IF (n>=10) EXIT END DO WRITE (*,*) "The sum of all values = ", sum_x END PROGRAM PROGRAM MEAN IMPLICIT NONE REAL :: x, sum_x = 0 INTEGER :: 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 / Counting LOOP

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

Iterative / Counting LOOP program COUNTER integer :: i do i = 1, 10 write (*,*) i end do end program Number of Iterations = (10 – 1 + 1 ) / 1 = 10

Iterative / Counting LOOP program COUNTER integer :: i do i = 1, 10, 1 write (*,*) i end do end program Number of Iterations = (10 – 1 + 1 ) / 1 = 10

Iterative / Counting LOOP program COUNTER integer :: i do i = 1, 10, 2 write (*,*) i end do end program Number of Iterations = (10 – 1 + 2 ) / 2 = 5.5 = 5

Iterative / Counting LOOP program COUNTER integer :: i do i = 10, 1, -1 write (*,*) i end do end program Number of Iterations = (1 – 10 + (-1) ) / (-1) = 10

Iterative / Counting LOOP program COUNTER integer :: i do i = 10, 1, -1 i = 5 write (*,*) i end do end program Number of Iterations = ?

Iterative / Counting LOOP program COUNTER integer :: i do i = 3, 2 write (*,*) i end do end program Number 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 test INTEGER :: i Do i = 1, 5 IF (i==3) EXIT WRITE(*,*) i END DO WRITE(*,*) ‘END of loop’ END PROGRAM __________________________ 1 2 4 5 END of loop 1 2 END of loop They can be used inside DO construct only

Named Loops PROGRAM 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 nested INTEGER :: i, j, product DO i = 1, 3 DO j = 1, 2 product = i * j WRITE(*,*) i,’ * ’, j, ‘ = ’, product END DO END 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 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 PROGRAM

Named Nested Loops Good practice so that compiler detects which loop has error PROGRAM nested INTEGER :: i, j, product loop1:DO i = 1, 3 loop2: DO j = 1, 2 product = i * j WRITE(*,*) i,’ * ’, j, ‘ = ’, product END DO loop2 END DO loop1 END 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 IF (expr4) Then END 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 SELECT DO END DO DO END DO DO END 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

Download ppt "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."

Similar presentations