chapter 3 program design and branching structures
TRANSCRIPT
![Page 1: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/1.jpg)
Chapter 3Program Design
And
Branching Structures
![Page 2: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/2.jpg)
Design approaches On-the-fly OK with simple programs Simple programs can be debugged easily Top-down design Necessary for large/complex programs Task subtasks Test each subtask individually, then combine all Debugging is easier
![Page 3: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/3.jpg)
Top-down-design steps
1. Clearly state the problem
2. Define required inputs and produced outputs
3. Design algorithm to be used
4. Turn algorithm into Fortran statements
5. Test program
![Page 4: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/4.jpg)
1. Clearly state the problem Write a program which calculates the total
energy of a falling object. Not clear enough Write a program which prompts the user to enter
the parameters of a falling object (height, mass, and velocity), calculates the total energy of the object (potential + kinetic) and prints on the screen for the user the total energy of the object.
Clearer
![Page 5: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/5.jpg)
2. Inputs & Outputs In the previous example:
Inputs: mass height velocity Outputs: Total energy of the falling object
![Page 6: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/6.jpg)
3. Algorithm
Prompt (ask) user for inputs Compute the total energy using the equations: Gravity = 9.8 Potential energy = mass * gravity * height Kinetic energy = 0.5 * mass * velocity2
Total energy = Potential energy + Kinetic energy
![Page 7: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/7.jpg)
4. Algorithm Fortran statementsPROGRAM Energyimplicit none
REAL,parameter :: gravity=9.8REAL :: height, mass, velocityREAL :: Potential_Energy, Kinetic_Energy, Total_Energy
write (*,*) "Please enter the height, mass, and velocity of the object"read (*,*) height, mass, velocity
Potential_Energy = mass * gravity * heightKinetic_Energy = 0.5 * mass * velocity**2Total_Energy = Potential_Energy + Kinetic_Energy
write (*,*) "The toal energy of the object = ", Total_Energy, "Joul."END PROGRAM
![Page 8: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/8.jpg)
5. Testing Test in the LAB For big programs: ALPHA release First complete version Tested by the programmer and close friends All possible ways of using the program are tested BETA release Serious bugs in ALFA release is removed Tested by users who need the program Program is put under many different conditions Released for general use Released for everyone to use
![Page 9: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/9.jpg)
Standard Forms of Algorithms:Pseudocode and Flowcharts
An algorithm is composed of constructs. Constructs can be described using:
Pseudocode Flowcharts
Advantages: Standard form: Easy to understand by others Making changes in the program is easier Debugging is easier
![Page 10: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/10.jpg)
Pseudocode: Describe algorithm using a mix of Fortran
language and English language Example:
Prompt user to enter height, mass, and velocityRead height, mass, and velocityGravity 9.8 Potential energy mass * gravity * heightKinetic energy 0.5 * mass * velocity2
Total energy Potential energy + Kinetic energyWrite Total energy on the screen
Standard Forms of Algorithms:Pseudocode and Flowcharts
![Page 11: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/11.jpg)
Flowcharts: Describe algorithm graphically Standard shapes are used for each type of construct
Standard Forms of Algorithms:Pseudocode and Flowcharts
![Page 12: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/12.jpg)
![Page 13: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/13.jpg)
![Page 14: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/14.jpg)
LOGICAL constants take one of 2 values: true / false
Example: Logical, parameter:: correct = .TRUE. Logical, parameter:: wrong = .FALSE.
LOGICAL constants are rarely used
Logical Variables and Constants
![Page 15: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/15.jpg)
LOGICAL variables are declared like other variables Example: LOGICAL :: var1, var2, var3 LOGICAL :: var4
LOGICAL variables are more used than LOGICAL constants
Logical Variables and Constants
![Page 16: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/16.jpg)
Invalid syntax correct = .TRUE incorrect = FALSE.
ERRORS …
![Page 17: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/17.jpg)
Logical statement form: Logical_variable_name = logical experession
Example: PROGRAM PASS IMPLICIT NONE CHARACTER (len=4) :: PASSWORD LOGICAL :: CHECK WRITE (*,*) “ What is the password? “ READ (*,*) PASSWORD
CHECK = ( PASSWORD == ‘EASY’ ) WRITE (*,*) CHECK END PROGRAM
Logical Statements
![Page 18: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/18.jpg)
Relational operators: compare two operands and produce logical results (T/F) A1 op A2 A1 & A2: can be either numerical or character op: == /= > < >= <= Examples: 3 < 4 .TRUE. 3 <= 4 .TRUE. 4 <= 3 .FALSE. ‘A’ < ‘B’ .TRUE. 4 < ‘A’ ????? ILLEGAL (ERROR)
Logical Statements.. Relational Operators
![Page 19: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/19.jpg)
Example: PROGRAM PASS IMPLICIT NONE INTEGER :: x, y LOGICAL :: compare
WRITE (*,*) “Enter numbers (x, y) to check if “ WRITE (*,*) “x > y “ WRITE (*,*) “ “ READ (*,*) x, y
CHECK = (x > y) WRITE (*,*) “ The statement x > y is “, CHECK END PROGRAM
Logical Statements.. Relational Operators
![Page 20: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/20.jpg)
Combinational operators: compare two operands and produce logical results (T/F) A1 op A2 A1 & A2: logical operands (.TRUE. / .FALSE.) op: .AND. .OR. .EQV. .NEQV. .NOT Truth table for binary combinational logic operators: L1 .FALSE. .FALSE. .TRUE. .TRUE. L2 .FALSE. .TRUE. .FALSE. .TRUE. L1 .AND. L2 .FALSE. .FALSE. .FALSE. .TRUE. L1 .OR. L2 .FALSE. .TRUE. .TRUE. .TRUE. L1 .EQV. L2 .TRUE. .FALSE. .FALSE. .TRUE. L1 .NEQV. L2 .FALSE. .TRUE. .TRUE. .FALSE.
L1 .TRUE. .FALSE. .NOT. L1 .FALSE. .TRUE.
Logical Statements.. Combinational Operators
![Page 21: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/21.jpg)
Exercise:
L1 = .TRUE.
L2 = .TRUE.
L3 = .FALSE. Logical expression .NOT. L1 .FALSE. L1 .OR. L3 .TRUE. L2 .NEQV. L3 .TRUE.
Logical Statements.. Combinational Operators
![Page 22: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/22.jpg)
When evaluating an expression, follow these rules: 1. Arithmetic operations (e.g. (3 + 4 * ( 2 / 5)) 2. Relational logic operations (e.g. ( 3 > 4 ) ) 3. Combinational logic operations, evaluate in this order:
.NOT. (left to right) .AND. (left to right) .OR. (left to right) .EQV. and .NEQV. (left to right)
Parenthesis can change order of evaluation
Logical Statements.. Evaluation order
![Page 23: Chapter 3 Program Design And Branching Structures](https://reader033.vdocuments.us/reader033/viewer/2022061609/56649c775503460f9492cb44/html5/thumbnails/23.jpg)
Exercise:
L1 = .TRUE.
L2 = .TRUE.
L3 = .FALSE. Logical expression .NOT. ( 3 > 4 ) .TRUE. L3 .OR. ( (2 * 5) < 12 ) .TRUE. L2 .NEQV. ( L3 .AND. ( 3 /= 4)) .TRUE.
Logical Statements.. Evaluation order