Control Structures
Control structures Control structures are statements that determine the flow of information through your program. There are not very many of them but they are VERY IMPORTANT They are the logical building blocks of every program you will ever write.
Outline of control structures Sequential Decision (selection) Loops (repetition)
Sequential control structures These are the simplest of all A program with sequential structure Does each statement in order It skips no statements It repeats no statements
Sequential control example
Program example: INTEGER score PRINT*, “Please enter test score” READ*, score PRINT*, “The score entered was”, score END
Flow chart START Prompt for score Read score Print score START
Decision structures A decision, or selection, structure is one that allows you to choose from among several options. Decision structures allow you to skip some statements.
Types of decision/selection structures A. Logical IF B. Block IF 1. Single alternative IF 2. Double alternative IF 3. Multiple alternative IF
Program example: the LOGICAL IF INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) PRINT*, “PASS” END
Characteristics of the logical IF It is all on one line It handles only one alternative It performs only one task Unlike other forms of the IF statement it does not use the keyword THEN or the keyword ENDIF
Algorithm example 1. Declare variables 2. Prompt for score 3. Read score 4. If (score >= 60) then 4.1 print ‘Pass’ 5. End
Flowchart Prompt for score Read score true Score >= 60 Print ‘PASS’ false
The single alternative IF Handles only one alternative (like the logical IF May include many tasks Has block structure (IF…THEN, ENDIF)
Program example: The single alternative block IF INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) THEN PRINT*, “PASS” ENDIF END BLOCK IF
Single alternative IF with multiple tasks INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) THEN PRINT*, “Congratulations!” PRINT*, “You earned a passing grade.” PRINT*, “Time to email mom about” PRINT*, “how well you are doing in” PRINT*, “college this semester.” ENDIF END
Double alternative IF 1. Declare variables 2. Prompt for score 3. Read score 4. Process score 4.1 If (score >= 60) then 4.1.1 print ‘Pass’ 4.2 else 4.2.1 print ‘Fail’ 5. End
alternatives Algorithm numbering 3.1 (first alternative) 3.1.x (associated tasks) 3.2 (second alternative) 3.2.x (associated tasks) The idea is that the program will execute the first alternative that has a true condition. Only 1 alternative is selected
Flowchart Prompt for score Read score true false Score >= 60 Print ‘PASS’ Print ‘FAIL’
Program example INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) THEN PRINT*, “PASS” ELSE PRINT*, “FAIL” ENDIF END 1st alternative 2nd alternative
Parking problem example INTEGER people, days REAL permit, cost, total PRINT*, “Please enter cost of permit” READ*, permit PRINT*, “How many days” READ*, days PRINT*, “Number of people in the car” READ*, people IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE ! Process good data in here ENDIF END
Nested loops Nested IF’s are situations where one IF statement is inside of another one. This is very common Make sure the inner IF is ended before the outer IF is you should not have overlapping structures!
Good nesting IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE calculate cost of daily pass calculate total cost of pay lot IF (total .gt. Permit) THEN PRINT*, “Buy permit” PRINT*, “Pay as you go” ENDIF END
Invalid nesting Intersecting control structures IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE calculate cost of daily pass calculate total cost of pay lot IF (total .gt. Permit) THEN PRINT*, “Buy permit” PRINT*, “Pay as you go” ENDIF END Intersecting control structures
Multiple alternative IF 1. Declare variables 2. Prompt for score 3. Read score 4. Process score 4.1 If (score >= 90) then 4.1.1 print ‘A’ 4.2 else if (score >= 80) then 4.2.1 print ‘B’ 4.3 else if (score >= 70) then 4.3.1 print ‘C’ 4.4 else if (score >= 60) then 4.4.1 print ‘D’ 4.5 else 4.5.1 print ‘F’ 5. End
Read score true Score >= 90 false Print ‘A’ true Score >= 80 false Print ‘B’ true false Score >= 70 Print ‘C’ true false Score >= 60 Print ‘D’ Print ‘F’
Multiple alternative IF IF (score .ge. 90) THEN PRINT*, “A” ELSE IF (score .ge. 80) THEN PRINT*, “B” ELSE IF (score .ge. 70) THEN PRINT*, “C” ELSE IF (score .ge. 60) THEN PRINT*, “D” ELSE PRINT*, “F” ENDIF END
Algorithm, program correspondence 4.1 If (score >= 90) then 4.1.1 print ‘A’ 4.2 else if (score >= 80) then 4.2.1 print ‘B’ 4.3 else if (score >= 70) then 4.3.1 print ‘C’ 4.4 else if (score >= 60) then 4.4.1 print ‘D’ 4.5 else 4.5.1 print ‘F’ IF (score .ge. 90) THEN PRINT*, “A” ELSE IF (score .ge. 80) THEN PRINT*, “B” ELSE IF (score .ge. 70) THEN PRINT*, “C” ELSE IF (score .ge. 60) THEN PRINT*, “D” ELSE PRINT*, “F” ENDIF
Parking problem: Processing good data IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 ELSE cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”
Parking problem example IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”
Parking problem example IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”
Parking problem example IF ((days .le. 0).or.(people .le. 0)) THEN PRINT*, “ERROR - negative number” ELSE IF (people .gt. 2) THEN cost = 0.25 ELSE IF (people .eq. 2) THEN cost = 0.50 cost = 0.75 ENDIF total = days * cost IF (total .gt. permit) THEN PRINT*, “Buy the permit” PRINT*, “Pay as you go”
Read permit, people, days true false data > 0 t f ERROR message people > 2 Cost = .25 t f people = 2 Cost = .50 Cost = .75 Total = days * cost t Total > permit f Permit Paylot
A good test question: What is printed? INTEGER num num = 5 IF (num .lt. 10) THEN PRINT*, “num is less than 10” ELSE IF (num .eq. 5) THEN PRINT*, “num equals 5” ELSE IF (num .gt. 0) THEN PRINT*, “num is greater than 0” ELSE PRINT *, “all of the above” ENDIF
Multiple choice A. num is less than 10 B. num equals 5 C. num is greater than 0 D. All of the above E. num is less than 10 num equals 5 num is greater than 0
Correct answer A. num is less than 10 B. num equals 5 C. num is greater than 0 D. All of the above E. num is less than 10 num equals 5 num is greater than 0
Why? Remember that with multiple alternative structures the first true choice is the one that gets done. All others get skipped. This makes more sense if you see a diagram of the process.
num = 5 true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all
num = 5 true num < 10 false Print < 10 true num = 5 false The only way you can get here is if the first condition if false! true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all
num = 5 true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all
How selection decisions are made Selection decisions are based around comparisons of the relationships between the data values. Comparisons are made using relational operators.
Relational operators OPERATOR f77 f90 Greater than .gt. > Greater than or equal to .ge. >= Less than .lt. < Less than or equal to .le. <= Equal to .eq. == Not equal to .ne. /=
Logical operators OPERATOR f77 and f90 and .and. or .or. not .not.
Conditions and relational operators All IF statements work by evaluating a condition. Example: IF (num .gt. 0) THEN The condition is ‘num .gt. 0’ Every IF statement must have one set of parentheses around the entire condition. The condition evaluates to either true or false.
Conditional evaluations INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN (10 < 20) (true)
Evaluation results INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN PRINT*, “num1 is larger” ENDIF PRINT*, “program over” END If the condition is true the task is performed. If the condition is false the task is not performed. Output if condition is true num1 is larger program over Output if condition is false program over
Evaluation results INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN PRINT*, “num1 is larger” ELSE PRINT*, “num1 is not larger” ENDIF PRINT*, “program over” END If the condition is true the task is performed. If the condition is false the false task is performed. Output if condition is true num1 is larger program over Output if condition is false num1 is not larger program over
Complex expressions Sometimes it is necessary to mix both relational and logical operators in expressions. In this case, it can be very difficult to decide what the computer is going to do.
Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is true ERROR - negative numbers Output if condition is false The area is: 200 square feet
Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((false) .or. (false)) .or. truth table (false) true true true true false true true false true false false false
Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is false The area is: 200 square feet
Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (false)) .or. truth table (true) true true true true false true true false true false false false
Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (false)) .or. truth table (true) true true true true false true true false true false false false
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (true)) .or. truth table (true) true true true true false true true false true false false false
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .or. (height .lt. 0)) THEN ((true) .or. (true)) .or. truth table (true) true true true true false true true false true false false false
Summary of .or. The only way you can get false is if both operands are false. If either one, or both are true, then the whole thing is true. .or. truth table true true true true false true true false true false false false
Evaluation results of .and. INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is true ERROR - negative numbers Output if condition is false The area is: 200 square feet
Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((false) .and. (false)) .and. truth table (false) true true true true false false false false true false false false
Evaluation results INTEGER length, height length = 10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is false The area is: 200 square feet
Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (false)) .and. truth table (false) true true true true false false false false true false false false
Evaluation results INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (false)) .and. truth table (false) true true true true false false false false true false false false
Now we have got a problem INTEGER length, height length = -10 height = 20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN PRINT*, “ERROR - negative numbers” ELSE PRINT*, “The area is: “, length * height, “square feet” ENDIF END Output if condition is false The area is: -200 square feet
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (true)) .and. truth table (true) true true true true false false false false true false false false
Evaluation results INTEGER length, height length = -10 height = -20 IF ((length .lt. 0) .and. (height .lt. 0)) THEN ((true) .and. (true)) .and. truth table (true) true true true true false false false false true false false false
Summary of .and. The only way you can get true is if both operands are true. If either one, or both are false, then the whole thing is false. .and. truth table true true true true false false false false true false false false