1 / 27

Programming For Nuclear Engineers Lecture 4 Control Structures and Program Design

Programming For Nuclear Engineers Lecture 4 Control Structures and Program Design. *Introduction 1- Top-Down Design Technique 2- Use of Pseudocode and Flowcharts 3- Logical Constants, Variables, and Operations 3.1- Logical Constants and Variables

duer
Download Presentation

Programming For Nuclear Engineers Lecture 4 Control Structures and Program Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming For Nuclear Engineers Lecture 4Control Structures and Program Design

  2. *Introduction 1- Top-Down Design Technique 2- Use of Pseudocode and Flowcharts 3- Logical Constants, Variables, and Operations 3.1- Logical Constants and Variables 3.2- Assignment Statements and Logical Calculations 3.3- Relational Operators 3.4- Combinational Logic Operators 3.5- Logical Values in Input and Output Statements 3.6- The Significance of Logical Variables and Expressions 4- Control Constructs: Branches 4.1- The Block IF Constructs 4.2- The ELSE and ELSE IF Clauses

  3. Introduction Branches are a broad categories of control statements that allow us to control the order in which statements are executed in a program, and they select specific sections of the code to execute. By using branches our program are going to become more complex, and it will get easier to make mistakes. To help avoid programming errors, we have to use a formal program design procedure based on the technique known as top-down design. Also we have to develop the algorithms used before, by using two common algorithm tools: flowcharts and pseudocode. .

  4. 1- Top-Down Design Technique Is the process of starting with a large task and breaking it down into smaller, more easily understandable pieces (subtasks) that perform a portion of the desired task. Each subtask may in turn be subdivided into smaller subtasks if necessary. Once the program is divided into small pieces, each piece can be coded and tested independently. When each of the subtasks has been verified to work properly by itself, we can combine the subtasks to a complete task. The following figure shows the program design used commonly.

  5. State the problem you are trying to solve Start Define required inputs and outputs Decomposition Design the algorithm Stepwise refinement Convert algorithm into Fortran statements Top-down design process Test the resulting Fortran program Finished!

  6. The program design process may be summarized as follows: 1- Clearly state the problems that you are trying to solve. 2- Define the inputs required by the program and the outputs to be produced by the program. 3- Design the algorithm that you intend to implement in the program. 4- Turn the algorithm into Fortran statements. 5- Test the Fortran program. We can best reduce the testing and debugging time by doing a very careful job in the planning phase (steps 1 through 3), and by using good programming practices.

  7. 2- Use of Pseudocode and Flowcharts The description of the algorithm should be in a standard form that is easy for both the programmer and other people to understand, and it should aid you in turning your concepts into Fortran code. The standard forms that we use to describe algorithms are called constructs, and an algorithm described using these constructs is called a structured algorithm. When the algorithm is implemented in a Fortran program, the resulting program is called a structured program. Pseudocode is a hybrid mixture of Fortran and English. It is structured like Fortran, with a separate line for each distinct idea or segment of code, but the descriptions on each line are in English

  8. Each line of the pseudocode should describe its idea in plain, easily understandable English. Pseudocode is very useful for developing algorithms, since it is flexible and easy to modify. For example the pseudocode for the algorithm of converting the temperature from degrees Fahrenheit to Kelvin, is: Prompt user to enter temperature in degree Fahrenheit Read temperature in degrees Fahrenheit (temp_f) temp_k in kelvins ←(5./9.)*(temp_f-32)+273.15 Write temperature in kelvins Notice that a left arrow (←) is used instead of an equal sign (=) to indicate that a value is stored in a variable, since this avoids any confusion between assignment and equality.

  9. Flowcharts are a way to describe algorithms graphically. In a flowcharts, different graphical symbols represent the different operations in the algorithm, and our standard constructs are made up of collections of one or more of these symbols. Flowcharts are very useful for describing the algorithm implemented in a program after it is completed. But they are not very useful during the preliminary stages of algorithm definition when rapid changes are occurring. The most common graphical symbols used in flowcharts are shown below: 1- An oval indicates the start or stop of an algorithm 2- A rectangle indicates a computation, with the result of the computation assigned to a variable 3- A parallelogram indicates an input or output operation

  10. 4- A diamond indicates a point where a choice is made between two alternatives 5- A double-lined rectangle indicates a reference to a subroutine that is documented elsewhere 6- An arrow indicates the direction of program flow between steps in the algorithm 7- When it is inconvenient to connect two points by flowlines, the flowline is connected to a numbered circle and continued from a circle with the same number on another portion of the diagram 8- This shape indicates an iterative or counting loop

  11. Start For the previous example, pseudocode was used, now we could illustrate the same example using flowcharts, as: Tell user to enter temperature in 0Fin Get temp_f Calculate temp_k temp_k=5/9*(temp_f-32)+273.15 Write temperature in kelvins Stop

  12. 3- Logical Constants, Variables, and Operators Most Fortran branching structures are controlled by logical values. 3.1 Logical Constants and Variables Logical data type contains one of only two possible values: TRUE or FALSE. A logical constant can have one of the following values: TRUE. or .FALSE. The following are not valid logical constants: TRUE (No periods-this is a variable name) .FALSE (Unbalanced periods) Logical constants are rarely used, but logical expressions and variables are commonly used to control program execution.

  13. A logical variable is a variable containing a value of the logical data type. A logical variable is declared using the LOGICAL statement: LOGICAL::var1[,var2,var3,…] This type declaration should be placed after the PROGRM statement and before the first executable statement in the program, as shown in the example below: PROGAM example LOGICAL :: test1, test2 … (Executable statements follow) 3.2 Assignment Statements and Logical Calculations Logical calculations are performed with an assignment statement, whose form is: logical_variable_name=logical_expression

  14. The expression to the right of the equal sign can be any combination of valid logical constants, logical variables, and logical operators. A logical operator is an operator on numeric, character, or logical data that yields a logical result. There are two basic types of logical operators: relational operators and combinational operators. 3.3 Relational Operators They are operators with two numerical or character operands that yield a logical result. The result depend on the relationship between the two values being compared, so these operators are called relational. a1op a2 Where a1 and a2 are arithmetic expressions, variables, constants, or character strings, and op is one of the relational logic operators listed in the table below.

  15. There are two forms of each relational operator. The first one is composed of symbols, and the second one is composed of characters surrounded by periods. In the second form, the periods are a part of the operator and must always be present. You may use either form of the operators in your program, but the first form is preferred in new programs.

  16. You have to be so much careful, when trying to do a comparison between two arithmetic expressions, you have to use two equal signs (==) , not = symbol which assigns the value of the expression to the right of the equal sign to the variable on the left of the equal sign. Relational operators are evaluated after all arithmetic operators have been evaluated. If the comparison is between real and integer values, then the integer value is converted to a real value before the comparison is performed. Comparisons between numerical data and character data are illegal and will cause a compile-time error. 3.4 Combinational Logic Operators They are operators with one or two logical operand that yield a logical result. There are four binary operators, .AND. , .OR. , .EQV. , and .NEQV. , and one unary operator, .NOT.

  17. The general form of a binary combinational logic operation is: l1.op.l2 Where l1 and l2 are logical expressions, variables, or constants, and .op. is one of the combinational operators described in the table below.

  18. In the hierarchy of operations, combinational logic operators are evaluated after all arithmetic operations and all relational operators have been evaluated. The order in which the operators in an expression are evaluated is as follows: • All arithmetic operators are evaluated first. • All relational operators (==, /=, >, >=, <, <=) are evaluated, working from left to right. • All .NOT. Operators are evaluated. • All .AND. Operators are evaluated, working from left to right. • All .OR. Operators are evaluated, working from left to right. • All .EQV. and .NEQV. operators are evaluated, working from left to right. The .NOT. operator is evaluated before other combinational logic operators. In the Fortran 95 and Fortran 2003 standards, combinational logic operations involving numerical or character data are illegal and will cause a compile-time error.

  19. In the following example, some combinational logic operators and their results followed: SIN1 = .TRUE. SIN2 = .TRUE. SIN3 = .FALSE. Logical Expressions Results (a) .NOT. SIN1 .FALSE. (b) SIN1 .OR. SIN3 .TRUE. (c) SIN1 .AND. SIN3 .FALSE. (d) SIN2 .NEQV. SIN3 .TRUE. (e) SIN1 .AND. SIN2 . OR. SIN3 .TRUE. (f) SIN1 .OR. SIN2. AND. SIN3 .TRUE. (g) .NOT. (SIN1 .EQV. SIN2) .FALSE.

  20. 3.5 Logical Values in Input and Output Statements If a logical variable appears in a list-directed READ statement, then the corresponding input value must be a character or a group of characters beginning with either a T or an F. If the first character of the input value is T, then the logical variable will be set to .TRUE. If the first character of the input value is F, then the logical variable will be set to .FALSE. Any input value beginning with another character will produce a run-time error. If a logical variable or expression appears in a list-directed WRITE statement, then the corresponding output value will be the single character T if the value of the variable is .TRUE.,and F if the value of the variable is .FALSE.

  21. 3.6 The Significance of Logical Variables and Expressions Logical variables and expressions are absolutely essential to the proper operation of most programs. Most of the major branching and looping structures of Fortran are controlled by logical values, so in order to understand and use Fortran control statements, you must be able to read and write logical expressions. 4. Control Constructs: Branches Fortran statements that permit us to select and execute specific sections of code (blocks) while skipping other sections of code. They are variations of the IF statement, plus the SELECT CASE construct.

  22. 4.1 The Block IF Constructs This construct specifies that a block of code will be executed if and only if a certain logical expression is true. The block IF construct has the form: IF (logical_expr)THEN Statement 1 Statement 2 … END IF If the logical expression is true, the program executes the statements in the block between the IF and END IF statements. If the logical expression is false, then the program skips all of the statements in the block between the IF and END IF statements, and executes the next statement after the END IF.

  23. .FALSE. Statement1 Statement2 … logical_expr .TRUE. As an example of a block IF construct, consider the solution of a quadratic equation of the form: ax2 +bx+c = 0

  24. The solution to this equation is: The term b2 -4ac is known as the discriminant of the equation. If b2 -4ac>0, then there are two distinct real roots to the quadratic equation. If b2 -4ac=0, then there is a single repeated root to the equation, and if b2 -4ac<0, then there are two complex roots to the quadratic equation. If we want to examine the discriminant of the quadratic equation, we have to use the block IF construct to do this (the pseudocode), and it will take the form: IF (b**2 - 4.*a*c)<0. THEN Write message that equation has two complex roots. END of IF In Fortran, the block IF construct is: IF (b**2 – 4.*a*c)<0.) THEN WRITE (*,*) ‘There are two complex roots to this equation.’ END IF

  25. 4.2 The ELSE and ELSE IF Clauses In some cases we may want to execute one set of statements if some condition is true, and different sets of statements if other conditions are true, or there might be many different options to consider. In such cases, an ELSE clause and one or more ELSE IF clauses may be added to the block IF construct. If (logical_expr_1) THEN Statement 1 Statement2 ... ELSE IF (logical_expr_2) THEN Statement1 Statement2 … ELSE Statement1 Statement2 … END IF

  26. There can be any number of ELSE IF clauses in a block IF constructs. The logical expression in each clause will be tested only if the logical expressions in every clause above it are false. Once one of the expressions proves to be true and the corresponding code block is executed, the program skips to the first executable statement following the END IF. Logical_expr_1 Logical_expr_2 .FALSE. .FALSE. .TRUE. .TRUE. Block1 Block2 Block3

  27. For the quadratic equation considered before, suppose we want to examine the discriminant of a quadratic equation and tell whether the equation has two complex roots, two identical real roots, or two distinct real roots. The Fortran statements to do this are: IF ( (b**2 -4.*a*c) < (0.0 ) THEN WRITE (*,*) ‘This equation has two complex roots.’ ELSE IF ( (b**2 -4.*a*c) > 0.0) THEN WRITE (*,*) ‘This equation has two distinct real roots.’ ELSE WRITE (*,*) ‘This equation has two identical real roots.’ END IF

More Related