Chapter 3 Program Design And Branching Structures Design

Chapter 3 Program Design And Branching Structures

Design approaches n n n n 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

Top-down-design steps 1. 2. 3. 4. 5. Clearly state the problem Define required inputs and produced outputs Design algorithm to be used Turn algorithm into Fortran statements Test program

1. Clearly state the problem n n 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

2. Inputs & Outputs n In the previous example: n n n Inputs: mass height velocity Outputs: Total energy of the falling object

3. Algorithm n n n 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 * velocity 2 Total energy = Potential energy + Kinetic energy

4. Algorithm Fortran statements PROGRAM Energy implicit none REAL, parameter : : gravity=9. 8 REAL : : height, mass, velocity REAL : : 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 * height Kinetic_Energy = 0. 5 * mass * velocity**2 Total_Energy = Potential_Energy + Kinetic_Energy write (*, *) "The toal energy of the object = ", Total_Energy, "Joul. " END PROGRAM

5. Testing n n n 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

Standard Forms of Algorithms: Pseudocode and Flowcharts An algorithm is composed of constructs. Constructs can be described using: n n Pseudocode Flowcharts Advantages: n n Standard form: Easy to understand by others Making changes in the program is easier Debugging is easier

Standard Forms of Algorithms: Pseudocode and Flowcharts n n n Pseudocode: Describe algorithm using a mix of Fortran language and English language Example: Prompt user to enter height, mass, and velocity Read height, mass, and velocity Gravity 9. 8 Potential energy mass * gravity * height Kinetic energy 0. 5 * mass * velocity 2 Total energy Potential energy + Kinetic energy Write Total energy on the screen

Standard Forms of Algorithms: Pseudocode and Flowcharts n n n Flowcharts: Describe algorithm graphically Standard shapes are used for each type of construct



Logical Variables and Constants n LOGICAL constants take one of 2 values: true / false n n Example: Logical, parameter: : correct =. TRUE. Logical, parameter: : wrong =. FALSE. n LOGICAL constants are rarely used n

Logical Variables and Constants n LOGICAL variables are declared like other variables Example: LOGICAL : : var 1, var 2, var 3 LOGICAL : : var 4 n LOGICAL variables are more used than LOGICAL constants n n n

ERRORS … n n n Invalid syntax correct =. TRUE incorrect = FALSE.

Logical Statements Logical statement form: n n Logical_variable_name = logical experession Example: n n n n PROGRAM PASS IMPLICIT NONE CHARACTER (len=4) : : PASSWORD LOGICAL : : CHECK WRITE (*, *) “ What is the password? “ READ (*, *) PASSWORD n n CHECK = ( PASSWORD == ‘EASY’ ) WRITE (*, *) CHECK END PROGRAM

Logical Statements. . Relational Operators n n n n Relational operators: compare two operands and produce logical results (T/F) A 1 op A 2 A 1 & A 2: 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 Example: n n n PROGRAM PASS IMPLICIT NONE INTEGER : : x, y LOGICAL : : compare n n n WRITE (*, *) “Enter numbers (x, y) to check if “ WRITE (*, *) “x > y “ WRITE (*, *) “ “ READ (*, *) x, y n n CHECK = (x > y) WRITE (*, *) “ The statement x > y is “, CHECK END PROGRAM

Logical Statements. . Combinational Operators n n n n Combinational operators: compare two operands and produce logical results (T/F) A 1 op A 2 A 1 & A 2: logical operands (. TRUE. /. FALSE. ) op: . AND. . OR. . EQV. . NOT Truth table for binary combinational logic operators: L 1. FALSE. . TRUE. L 2. FALSE. . TRUE. L 1. AND. L 2. FALSE. . TRUE. L 1. OR. L 2. FALSE. . TRUE. L 1. EQV. L 2. TRUE. . FALSE. . TRUE. L 1. NEQV. L 2. FALSE. . TRUE. . FALSE. L 1. NOT. L 1 . TRUE. . FALSE. . TRUE.

Logical Statements. . Combinational Operators n Exercise: L 1 =. TRUE. L 2 =. TRUE. L 3 =. FALSE. n n n n Logical expression. NOT. L 1. FALSE. L 1. OR. L 3. TRUE. L 2. NEQV. L 3. TRUE.

Logical Statements. . Evaluation order 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: n n n . NOT. (left to right) n . AND. n . OR. . EQV. and. NEQV. (left to right) n n (left to right) Parenthesis can change order of evaluation

Logical Statements. . Evaluation order n Exercise: L 1 =. TRUE. L 2 =. TRUE. L 3 =. FALSE. n n n n Logical expression. NOT. ( 3 > 4 ). TRUE. L 3. OR. ( (2 * 5) < 12 ). TRUE. L 2. NEQV. ( L 3. AND. ( 3 /= 4)). TRUE.
- Slides: 23