
DO WHILE Loop Outline Repetition 1. DO WHILE Loop Outline Repetition means performing the same set of statements over and 2. Repetition/ over. The most common way to perform repetition is via looping. Looping 3. The DO WHILE Loop Looping 4. DO WHILE Loop Flowchart 5. DO WHILE Loop Example A loop is a sequence of statements to be executed, in order, over and 6. DO WHILE Loop Example’s Flowchart over, until some condition is reached. 7. How Many Times Will the Body of a DO WHILE Loop Be Executed? Fortran 90 has a loop construct known as the DO WHILE loop: 8. An Infinite Loop 9. What Kind of Statements Can Go Inside a DO WHILE Loop? DO WHILE ( condition ) 10. Jargon: Compound Statement statement1 11. Another DO WHILE Loop Example statement2 12. Another DO WHILE Loop Example (continued) ... 13. Yet Another DO WHILE Loop Example END DO 14. States and Traces Here, the condition is a LOGICAL expression completely enclosed 15. Tracing the Loop in parentheses, just like in an IF block. See Programming in Fortran 90/95, 1st or 2nd edition, Chapter 13, The sequence of statements between the DO WHILE statement and section 13.4. the END DO statement is known as the loop body. 1 2 The DO WHILE Loop DO WHILE Loop Flowchart . DO WHILE ( condition ) . statement1 statement2 ... statement_before END DO When a DO WHILE statement is encountered, the following se- statement_before DO WHILE ( condition ) quence of events occurs, in order: False statement_inside1 condition 1. The condition (a LOGICAL expression completely enclosed in statement_inside2 parentheses) is evaluated, resulting in a value of either .TRUE. statement_inside_last or .FALSE. True END DO 2. If the condition evaluates to .FALSE., then the statements in- statement_after side the loop body are skipped, and control is passed to the state- statement_inside1 ment immediately after the END DO statement. 3. If the condition evaluates to .TRUE., then the statements inside the loop body are executed in sequence. statement_inside2 4. When the END DO statement is encountered, the program jumps back up to the associated DO WHILE statement and starts over . with Step 1. A DO WHILE loop is similar to an IF block, except that: statement_inside_last unlike an IF block, when a DO WHILE loop encounters its END DO statement, it jumps back up to the associated DO WHILE statement; statement_after unlike an IF block, a DO WHILE loop can have only one clause, which is analogous to the IF clause; a DO WHILE . loop cannot have anything analogous to an ELSE IF clause . or an ELSE clause. 3 4 DO WHILE Loop Example DO WHILE Loop Example’s Flowchart READ *, number_of_chickens % cat chickenidiot_dowhile.f90 DO WHILE (number_of_chickens < no_chickens) PROGRAM chicken_idiotproof_do_while PRINT *, "ERROR: you can't think of", & IMPLICIT NONE & "negative chickens!" PRINT *, "So really, how many chickens ", & INTEGER,PARAMETER :: no_chickens = 0 & "did you think of?" INTEGER :: number_of_chickens READ *, number_of_chickens PRINT *, "How many chickens did you think of?" END DO !! WHILE (number_of_chickens < no_chickens) READ *, number_of_chickens PRINT *, "The number of chickens is valid." DO WHILE (number_of_chickens < no_chickens) PRINT *, "ERROR: you can't think of ", & Prompt for # chickens. & "negative chickens!" PRINT *, "So really, how many chickens ", & & "did you think of?" Input # chickens. READ *, number_of_chickens END DO !! WHILE (number_of_chickens < no_chickens) PRINT *, "The number of chickens is valid." END PROGRAM chicken_idiotproof_do_while % f95 -o chickenidiot_dowhile chickenidiot_dowhile.f90 False chickens < 0 % chickenidiot_dowhile How many chickens did you think of? -1 ERROR: you can't think of negative chickens! True So really, how many chickens did you think of? -22 Output error. ERROR: you can't think of negative chickens! So really, how many chickens did you think of? 0 Prompt again. The number of chickens is valid. % chickenidiot_dowhile How many chickens did you think of? Input # chickens again. 12 The number of chickens is valid. Output valid. 5 6 How Many Times Will the Body of An Infinite Loop a DO WHILE Loop Be Executed? An infinite loop is a loop that never reaches its termination condition. For example: Recall: % cat infiniteloop.f90 PROGRAM infinite_loop DO WHILE ( condition ) IMPLICIT NONE statement1 INTEGER,PARAMETER :: my_number = 5 INTEGER :: your_number statement2 PRINT *, "Enter an integer:" ... READ *, your_number PRINT *, "I had ", my_number, "." END DO DO WHILE (your_number < my_number) PRINT *, "Your number is less than mine!" 1. The condition (a LOGICAL expression completely enclosed in END DO !! WHILE (your_number < my_number) END PROGRAM infinite_loop parentheses) is evaluated, resulting in a value of either .TRUE. % f95 -o infiniteloop infiniteloop.f90 or .FALSE. % infiniteloop Enter an integer: 2. If the condition evaluates to .FALSE., then the statements in- 8 side the loop body are skipped, and control is passed to the state- I had 5 . % infiniteloop ment immediately after the END DO statement. Enter an integer: 3. If the condition evaluates to .TRUE., then the statements inside 3 I had 5 . the loop body are executed in sequence. Your number is less than mine! 4. When the END DO statement is encountered, the program jumps Your number is less than mine! Your number is less than mine! back up to the associated DO WHILE statement and starts over Your number is less than mine! with Step 1. Your number is less than mine! Your number is less than mine! Your number is less than mine! Suppose that, the first time that the DO WHILE statement is en- ... Ctrl - C countered, its condition evaluates to .FALSE. On most Unix systems, including roosevelt, lincoln and In that case, the loop body will be skipped completely. kennedy, you can quit out of a program that is currently executing by typing So, it may be the case that the loop body is not executed at all, or it may be executed once, or twice, or many times. Ctrl - C 7 8 What Kind of Statements Can Go Inside Jargon: Compound Statement a DO WHILE Loop? A compound statement is a sequence of statements with a well- Between the DO WHILE statement and the END DO statement, defined beginning and a well-defined end that are executed, in order, there can be any kind of executable statements, and any number of under certain circumstances. them. For example: Examples of compound statements include IF blocks and DO WHILE PRINT statements; loops. READ statements; assignment statements; Although an IF block is actually a sequence of statements, we can STOP statements; think of it as a single “super” statement in some contexts. The same IF blocks; is true of a DO WHILE loop. DO WHILE loops. Compound statements are sometimes called blocks. Thus, we speak There are other kinds of executable statements that can occur inside of an IF block. A DO WHILE loop is also a kind of block. a DO WHILE loop, some of which we’ll learn later in the semester. In the event that the DO WHILE condition evaluates to .TRUE., these statements will be executed one by one, in the order in which they appear in the DO WHILE loop. Notice that a DO WHILE loop CANNOT contain declaration state- ments, because the DO WHILE statement is an executable state- ment, and ALL declarations MUST come before ANY executable statements. 9 10 Another DO WHILE Loop Example Another DO WHILE Loop Example PROGRAM warmer_colder (continued) IMPLICIT NONE INTEGER,PARAMETER :: minimum_number = 1 INTEGER,PARAMETER :: maximum_number = 100 % f95 -o warmercolder warmercolder.f90 INTEGER,PARAMETER :: my_number = 32 % warmercolder INTEGER,PARAMETER :: close_distance = 5 I'm thinking of a number between 1 and 100 . INTEGER,PARAMETER :: very_close_distance = 1 INTEGER,PARAMETER :: negative_distance = -1 What number am I thinking of? INTEGER,PARAMETER :: no_distance = 0 -22 INTEGER :: your_number, your_distance Hey! That's not between 1 and 100 ! INTEGER :: your_last_distance = negative_distance I'll pretend you didn't say that. LOGICAL :: correct_number_hasnt_been_input = .TRUE. What number am I thinking of? PRINT *, "I'm thinking of a number between ", & 50 minimum_number, " and ", maximum_number, "." Not bad for your first try. DO WHILE (correct_number_hasnt_been_input) What number am I thinking of? PRINT *, "What number am I thinking of?" 44 READ *, your_number You're getting warmer .... IF ((your_number < minimum_number) .OR. & & (your_number > maximum_number)) THEN What number am I thinking of? PRINT *, "Hey! That's not between ", & 22 & minimum_number, " and ", maximum_number, "!" You're getting warmer .... PRINT *, "I'll pretend you didn't say that." What number am I thinking of? ELSE IF (your_number == my_number) THEN 11 PRINT *, "That's amazing!" correct_number_hasnt_been_input = .FALSE. Ouch! You're getting colder. ELSE !! (your_number == my_number) What number am I thinking of? your_distance = ABS(your_number - my_number) 35 IF (your_distance == very_close_distance) THEN You're getting warmer .... PRINT *, "You're incredibly hot!" ELSE IF (your_last_distance < no_distance) THEN What number am I thinking of? PRINT *, "Not bad for your first try." 37 ELSE IF (your_distance < your_last_distance) THEN Ouch! You're getting colder. PRINT *, "You're getting warmer ...." What number am I thinking of? ELSE IF (your_distance > your_last_distance) THEN 33 PRINT *, "Ouch! You're getting colder." ELSE !! (your_distance > your_last_distance) You're incredibly hot! PRINT *, "Uh oh. You made no progress." What number am I thinking of? END IF !! (your_distance > your_last_distance)...ELSE 32 your_last_distance
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-