Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.

Similar presentations


Presentation on theme: "Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding."— Presentation transcript:

1 Chapter 3 Applications & Logical Constructs

2 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding Fahrenheit temperature. SOLUTION: Assume that the temperature converting formula is given by F = (9/5) C + 32 INPUT: A temperature in degrees Celsius OUTPUT: A temperature in degrees Fahrenheit Programs T_conv_1.f and T_conv_2.f BASIC STEPS FOR ALGORITHM STEP1 --> Enter C STEP2 --> Calculate F STEP3 --> Display F

3 3 Application 2 Area of a Triangle: Write a program to read values for the three sides a, b, c of a triangle and then calculate its perimeter and its area. These should be displayed together with the values of a, b, c using appropriate labels. (for the area, you might use Hero’s formula for the area of a triangle: where s is one-half the perimeter.) SOLUTION: See the source code area_tri.f for the solution

4 4 Selective Execution Three basic methods of control are used in designing algorithms and programs: 1) Sequence: Each step are performed exactly once and steps are performed in a strictly sequential manner. 2) Selection: One of a number of alternative actions is selected and executed 3) Repetition: One or more steps are performed repeatedly.

5 5 Logical Expressions Logical expressions may be either simple or compound. Simple logical expressions are logical constants or variables or relational expressions of the form. expression 1 relational-operator expression 2 where both expression 1 and expression 2 are numeric or character (or logical) expressions, and the relational-operator may be any of the following:

6 6 Simple Logical Expressions

7 7 Examples: x < 6.7 number == 1999 ASCII uses codes in the range 0 through 255 (i.e. A=65, B=66, …Z=90). (See complete table of ASCII characters in Appendix D of Ellis’s book) ”A” < ”G” is true logical expression ”cat” < ”dog” true ”June” < ”July” true

8 8 Compound Logical Expressions They are formed by combining logical expressions by using the logical operators.NOT. (negation).AND. (conjunction).OR. (disjunction).EQV. (equivalence).NEQV. (nonequivalence) all possible values are displayed in truth tables (next page)

9 9 Compound Logical Expressions

10 10 IF Constructs  A simple IF construct is as follows if (logical-expression) then statement-sequence end if EXAMPLE: if (X >= 0) then Y=X * X Z=sqrt(X) end if

11 11 Logical IF Statement  Fortran also provides a simpler IF construct called Logical IF statement if (logical-expression) statement if (1.5 <= X.AND. X<= 2.5) print *, X

12 12 General form of the IF construct if (logical-expression) then statement-sequence 1 else statement-sequence 2 end if If the logical expression is true, statement-sequence 1 is executed and statement-sequence 2 is bypassed.

13 13 Applications  (Problem #1) Solve the quadratic equation using an IF construct. SOLUTION: Read : The coefficients A, B, and C Output : The roots of the equation Check the source code Quad_eq_1.f !

14 14 Applications  (Problem #2) Rank two numbers Two real data values, X and Y are given. Write a program that finds the ranking between two entered numbers. Use If construct for the solution in your program. Check the source code rank_two.f !

15 15 IF-ELSE IF Constructs  It is possible to use the IF construct to design selection structures that contain more than two alternatives. if (logical-expression 1 ) then statement-sequence 1 else if (logical-expression 2 ) then statement-sequence 2 else if (logical-expression 3 ) then statement-sequence 3. else statement-sequence n end if

16 16 IF-ELSE IF Constructs When an if-else if construct is executed, the logical expressions are evaluated to determine the first expression that is true; the associated sequence of statements is executed, and execution then continues with the next statement following the next construct. If none of the logical expressions is true, the statement sequence associated with the else statement is executed, and execution then continues with the statement following the construct. This if construct thus implements an n-way selection structure in which exactly one of statement-sequence 1, statement-sequence 2, statement-sequence 3,…, statement-sequence n is executed.

17 17 Named Constructs if and if-else if constructs may be named by attaching a label at the beginning and end of the construct name: if (logical-expression) then end if name For example: update: if (x > largest) then largest=x position=N end if update

18 18 Named Constructs Naming prevents the possible mixing within the nested if’s. The name used in an if or if-else if construct may also be attached following the keyboard then in any else if part of the construct and following the keyboard else in an else part. For example: EmpType: if (EmployeeType == ”S”) then ! Salaried employee print *,”Enter employee’s annual salary” read *, Salary pay = salary / 52 else EmpType ! Hourly employee print *,”Enter hours worked and hourly rate:” read *, HoursWorked, HourlyRate Overtime: if (HoursWorked > 40.0) then

19 19 Named Constructs pay = 40.0 * HourlyRate & + Multiplier * HourlyRate*(HoursWorked-40.0) else Overtime pay=HoursWorked*HourlyRate end if Overtime end if EmpType

20 20 The CASE Construct The case construct is not as general as the if-else if construct. But it is useful for implementing some selection structures. The form of a case construct is as follows: select case (selector) case (label-list 1 ) statement-sequence 1 case (label-list 2 ) statement-sequence 2. case (label-list n ) statement-sequence n end select

21 21 The CASE Construct Where the selector is an integer, character, or logical expression; each of the (label-list 1 ) is a list of one or more possible values of the selector, enclosed in parenthesis. The values in this list may have any of the forms value value 1 : value 2 value 1 : : value 2 to denote a single value, the range of values from value 1 through value 2, the set of all values greater than or equal to value 1, or the set of all values less than or equal to value 2, respectively. When a case construct is executed, the selector is evaluated; if this value is in label-list 1, statement-sequence 1 is executed, and execution continues with the statement following the end select statement.

22 22 Named CASE Construct If the value is not in any of the lists of values, the sequence of statements associated with default is executed. Named case constructs A name may be attached to a case construct: name: select case (selector). end select name

23 23 Named CASE Construct For example: Class: select case (ClassCode) case (1) print *, ”Freshman” case (2) print *, ”Sophomore” case (3) print *, ”Junior” case (4) print *, ”Senior” case (5) print *, ”Graduate” default print *, ”Illegal class code”,Class code end select Class

24 24 Exercises & Reading (Ellis’s Book)  Study examples 5.1, 5.3, 5.4 and 5.5 on pages 112, 118, 124 and 126 respectively.  Read summary on page 131.  Study Chapter 5: Pages between 103 and 131.

25 25 The LOGICAL data type  Two logical constants in Fortran are.TRUE. and.FALSE.  A logical variable is declared using a logical type statement logical :: list where list is a list of variables being typed as logical. An assignment statement of the form logical-variable = logical-expression can be used to assign a value to a logical variable For example: logical :: RootExists, End_of_Data End_of_Data=.true. RootExists=Discriminant >= 0 are valid assignment statements and assigns.TRUE. to RootExists if Discriminant is nonnegative and assigns.FALSE. otherwise.

26 26 The LOGICAL data type Display of logical variables are as follows: print *, A,B,C,.true.,.false. produces T F F F T F as output.


Download ppt "Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding."

Similar presentations


Ads by Google