Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures.

Similar presentations


Presentation on theme: "Control Structures."— Presentation transcript:

1 Control Structures

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

3 Outline of control structures
Sequential Decision (selection) Loops (repetition)

4 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

5 Sequential control example

6 Program example: INTEGER score PRINT*, “Please enter test score”
READ*, score PRINT*, “The score entered was”, score END

7 Flow chart START Prompt for score Read score Print score START

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

9 Types of decision/selection structures
A. Logical IF B. Block IF 1. Single alternative IF 2. Double alternative IF 3. Multiple alternative IF

10 Program example: the LOGICAL IF
INTEGER score PRINT*, “Please enter test score” READ*, score IF (score .ge. 60) PRINT*, “PASS” END

11 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

12 Algorithm example 1. Declare variables 2. Prompt for score
3. Read score 4. If (score >= 60) then 4.1 print ‘Pass’ 5. End

13 Flowchart Prompt for score Read score true Score >= 60 Print ‘PASS’
false

14 The single alternative IF
Handles only one alternative (like the logical IF May include many tasks Has block structure (IF…THEN, ENDIF)

15 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

16 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 mom about” PRINT*, “how well you are doing in” PRINT*, “college this semester.” ENDIF END

17 Double alternative IF 1. Declare variables 2. Prompt for score
3. Read score 4. Process score 4.1 If (score >= 60) then print ‘Pass’ 4.2 else print ‘Fail’ 5. End

18 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

19 Flowchart Prompt for score Read score true false Score >= 60
Print ‘PASS’ Print ‘FAIL’

20 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

21 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

22 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!

23 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

24 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

25 Multiple alternative IF
1. Declare variables 2. Prompt for score 3. Read score 4. Process score 4.1 If (score >= 90) then print ‘A’ 4.2 else if (score >= 80) then print ‘B’ 4.3 else if (score >= 70) then print ‘C’ 4.4 else if (score >= 60) then print ‘D’ 4.5 else print ‘F’ 5. End

26 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’

27 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

28 Algorithm, program correspondence
4.1 If (score >= 90) then print ‘A’ 4.2 else if (score >= 80) then print ‘B’ 4.3 else if (score >= 70) then print ‘C’ 4.4 else if (score >= 60) then print ‘D’ 4.5 else 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

29 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”

30 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”

31 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”

32 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”

33 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

34 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

35 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

36 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

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

38 num = 5 true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all

39 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

40 num = 5 true num < 10 false Print < 10 true num = 5 false Print = 5 true false num > 0 Print > 0 Print all

41 How selection decisions are made
Selection decisions are based around comparisons of the relationships between the data values. Comparisons are made using relational operators.

42 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 /=

43 Logical operators OPERATOR f77 and f90 and and. or or. not not.

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

45 Conditional evaluations
INTEGER num1, num2 num1 = 10 num2 = 20 IF (num1 .lt. num2) THEN (10 < 20) (true)

46 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

47 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

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

49 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

50 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

51 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

52 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

53 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

54 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

55 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

56 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

57 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

58 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

59 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

60 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

61 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

62 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

63 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

64 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

65 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


Download ppt "Control Structures."

Similar presentations


Ads by Google