Review of lecture 3 - PowerPoint PPT Presentation

About This Presentation
Title:

Review of lecture 3

Description:

... the actions and go on, or skip them and go on. IF-THEN-END ... skip the block and continue with the statement following the END IF. Examples of IF-THEN-END IF ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 41
Provided by: socs4
Category:
Tags: lecture | review | skip

less

Transcript and Presenter's Notes

Title: Review of lecture 3


1
Review of lecture 3
  • Data type and declaration
  • INTEGER
  • E.g., a, b, c
  • REAL
  • E.g., x, y, z, w
  • LOGICAL
  • COMPLEX
  • CHARACTER
  • Examples
  • INTEGERa1,b-5,c5
  • REALx2.0

2
Review of lecture 3 (cont.)
  • Arithmetic expressions
  • E.g., 5 2 3
  • E.g., b2 4ac
  • It has a value by itself
  • E.g. expression 5 23 has a value of 11

3
Review of lecture 3 (cont.)
  • Evaluating Complex Expressions
  • Precedence
  • Associativity (For expressions having operators
    with the same precedence).

4
Review of lecture 3 (cont.)
  • Mixed Mode Expressions
  • If one operand of an arithmetic operator is
    INTEGER and the other is REAL
  • the INTEGER value is converted to REAL
  • the operation is performed
  • the result is REAL
  • 1 2.5 ? 3.5
  • 1/2.0 ? 0.5
  • 2.0/8 ? 0.25
  • -32.0 ? -9.0
  • 4.0(1/2) ? 1.0 (since 1/2 ? 0)
  • 3/5 5.0 ? 0.0 (since 3/5 ?0)

5
Review of lecture 3 (cont.)
  • Statements
  • Assignment statement
  • Variable expression
  • e.g., b -5
  • (never the other way around, 5b is not valid!)
  • General statement
  • INTEGER a, b, c
  • write (,) Hello world!

6
Selection in FORTRAN
  • Lecture 4
  • By Nathan Friedman and Yi Lin
  • Jan 16, 2007

7
Roots of a Quadratic (again)
  • ! ------------------------------------------------
    --
  • ! Solve Ax2 Bx C 0 given BB-4AC gt 0
  • ! ------------------------------------------------
    --
  • PROGRAM QuadraticEquation
  • IMPLICIT NONE
  • REAL a, b, c, d, root1, root2
  • ! read in the coefficients a, b and c
  • WRITE(,) "A, B, C Please "
  • READ(,) a, b, c
  • ! compute the square root of discriminant d
  • d SQRT(bb - 4.0ac)
  • ! solve the equation
  • root1 (-b d)/(2.0a) ! first root
  • root2 (-b - d)/(2.0a) ! second root
  • ! display the results

8
Run Time Error
  • We assumed that the discriminant was positive
  • What if it wasnt?
  • We would get an error when we run the program and
    attempt to compute the square root and the
    program would abort
  • What should we do?
  • Avoid trying to perform an illegal operation by
    branching around it -- use a selection control
    structure

9
Quadratics example
dbb 4.0ac
Dgt0.0
.TRUE.
.FALSE.
Calc root1, root2
Ouput no real roots!
end
10
IF-THEN-ELSE-END IF
  • Used to select between two alternative sequences
    of statements.
  • They keywords delineate the statement blocks.
  • Syntax
  • IF (logical-expression) THEN
  • first statement block, s_1
  • ELSE
  • second statement block, s_2
  • END IF

11
IF-THEN-ELSE-END IF
  • Semantics
  • Evaluate the logical expression. It can have
    value .TRUE. or value .FALSE.
  • If the value is .TRUE., evaluate s_1, the first
    block of statements
  • If the value is .FALSE., evlaluate s_2, the
    second block of statements
  • After finishing either s_1 or s_2, execute the
    statement following the END IF

12
Roots of Quadratic v.2
  • ! ------------------------------------------------
    ----------------
  • ! Solve Ax2 Bx C 0 given BB-4AC gt 0
  • ! ------------------------------------------------
    ----------------
  • PROGRAM QuadraticEquation
  • IMPLICIT NONE
  • REAL a, b, c, d, root, root2
  • ! read in the coefficients a, b and c
  • WRITE(,) "A, B, C Please "
  • READ(,) a, b, c
  • !compute the square root of discriminant d
  • d bb - 4.0ac
  • IF (d gt 0.0) THEN ! is it
    solvable?
  • d SQRT(d)
  • root1 (-b d)/(2.0a) ! first root
  • root2 (-b - d)/(2.0a) ! second root
  • WRITE(,) "Roots are ", root1, " and ",
    root2
  • ELSE ! complex roots

13
Logical Expressions
  • Relational operators return result of .TRUE. or
    .FALSE
  • lt, lt, gt, gt, , /
  • Relational operators are of lower precedence than
    all arithmetic operators
  • 2 7 gt 3 3 ? .TRUE.
  • There is no associativity
  • a lt b lt c ? illegal
  • Note that means is equal to but means
    assign the value on the right

14
Data Type Logical
  • Where do .TRUE. and .FALSE. come from?
  • FORTRAN has a LOGICAL data type, just like it has
    INTEGER and REAL types
  • We can declare variables to be of this type and
    assign values to them
  • LOGICAL positive_x, condition
  • condition .TRUE.
  • positive_x x gt 0

15
Is Number Even or Odd?
  • IF (MOD(number, 2) 0) THEN
  • WRITE(,) number, " is even"
  • ELSE
  • WRITE(,) number, " is odd"
  • END IF

MOD(a, b) Intrinsic function, returning the
remainder of a/b
16
Find Absolute Value
  • REAL x, absolute_x
  • x .....
  • IF (x gt 0.0) THEN
  • absolute_x x
  • ELSE
  • absolute_x -x
  • END IF
  • WRITE(,) The absolute value of ,
  • x, is , absolute_x
  • Note the use of to indicate continue on next
    line

17
Which value is smaller?
  • INTEGER a, b, min
  • READ(,) a, b
  • IF (a lt b) THEN
  • min a
  • ELSE
  • min b
  • END IF
  • Write(,) The smaller of , a,
  • and , b, is , min

18
IF-THEN-END IF
  • The IF-THEN-ELSE-END IF form allows us to choose
    between two alternatives
  • There is another simpler selection mechanism that
    allows us to choose whether or not to perform a
    single block of actions
  • We either perform the actions and go on, or skip
    them and go on

19
IF-THEN-END IF
  • Syntax
  • IF (logical expression) THEN
  • block of statements
  • END IF
  • Semantics
  • Evaluate the logical expression
  • If it evaluates to .TRUE. execute the block of
    statements and then continue with the statement
    following the END IF
  • If the result is .FALSE. skip the block and
    continue with the statement following the END IF

20
Examples of IF-THEN-END IF
  • absolute_x x
  • IF (x lt 0.0) THEN
  • absolute_x -x
  • END IF
  • WRITE(,) "The absolute value of ", x, " is ",
    absolute_x
  • -------------------------------------------------
  • INTEGER a, b, min
  • READ(,) a, b
  • min a
  • IF (a gt b) THEN
  • min b
  • END IF
  • Write(,) "The smaller of ", a, " and ", b, " is
    ", min

21
Logical IF
  • An even simpler form is sometimes useful
  • Syntax
  • IF (logical expression) single-statement
  • Semantics equivalent to
  • IF (logical expression) THEN
  • single-statement
  • END IF

22
Examples of Logical IF
  • absolute_x x
  • IF (x lt 0.0) absolute_x -x
  • WRITE(,) "The absolute value of ", x,
  • " is" ,"absolute_x
  • -------------------------------------------------
  • INTEGER a, b, min
  • READ(,) a, b
  • min a
  • IF (a gt b) min b
  • Write(,) "The smaller of ", a, " and ", b,
  • " is ", min

23
IF-THEN-ELSE IF-END IF
  • IF-THEN-ELSE-ENDIF can only handle processes
    which have two options.
  • Sometimes we have more than 2 options for one
    process.
  • For example, in the quadratic equation problem,
    there may be two equivalent roots. We want to
    detect this and complex roots at the same time.
  • Solution put another IF-THEN-ELSE-ENDIF in the
    ELSE block

24
IF-THEN-ELSE IF-END IF
  • Syntax
  • IF (logical-expression1) THEN
  • first statement block, s_1
  • ELSE
  • IF (logical-expression2) THEN
  • second statement block, s_2
  • ELSE
  • third statement block, s_3
  • END IF
  • END IF

25
IF-THEN-ELSE IF-END IF
  • Semantics
  • Evaluate the logical expression 1. It can have
    value .TRUE. or value .FALSE.
  • If the value is .TRUE., evaluate s_1, the first
    block of statements
  • If the value is .FALSE.,
  • evaluate the logical expression 2. It can have
    value .TRUE. Or value .FALSE.
  • If the value is .TRUE., evaluate s_2, the second
    block of statements
  • If the value is .FALSE., evaluate s_3, the third
    block of statements
  • After finishing either s_2 or s_3, execute the
    statement following ENDIF
  • After finishing either s_1 or s_2 or s_3, execute
    the statement following the END IF

26
Roots of Quadratic v.3
  • ! ------------------------------------------------
    ---
  • ! Solve Ax2 Bx C 0 given BB-4AC gt 0
  • ! Detect complex roots and repeated roots.
  • ! ------------------------------------------------
    ---
  • PROGRAM QuadraticEquation
  • IMPLICIT NONE
  • REAL a, b, c, d, root1, root2
  • ! read in the coefficients a, b and c
  • READ(,) a, b, c
  • ! comute the discriminant d
  • d bb - 4.0ac
  • IF (d gt 0.0) THEN ! distinct
    roots?
  • d SQRT(d)
  • root1 (-b d)/(2.0a) ! first root
  • root2 (-b - d)/(2.0a) ! second root
  • WRITE(,) 'Roots are ', root1, ' and ',
    root2
  • ELSE

27
IF-THEN-ELSE IF-END IF
  • You can put IF-THEN-ELSE IF-ENDIF within ELSE
    block repeatedly.
  • IF (logical-expression1) THEN
  • first statement block, s_1
  • ELSE
  • IF (logical-expression2) THEN
  • second statement block, s_2
  • ELSE
  • IF-THEN-ELSE IF-ENDIF
  • END IF
  • END IF
  • There is a concise way to do this.

28
IF-ELSEIF-ELSE-ENDIF
  • syntax
  • IF (logical expression 1) THEN
  • block 1
  • ELSEIF (logical expression 2) THEN
  • block 2
  • ...
  • ELSEIF (logical expression N-1) THEN
  • block N-1
  • ELSE
  • block N
  • ENDIF

29
IF-ELSEIF-ELSE-ENDIF
  • Semantics
  • First evaluate the logical expression 1. It can
    have value .TRUE. or value .FALSE.
  • If the value is .TRUE., evaluate the first block
    of statements.
  • If the value is .FALSE., evaluate the logical
    expression 2. It can have value .TRUE. or value
    .FALSE.
  • If the value is .TRUE., evaluate the second block
    of statements.
  • Repeat this until all logical expressions have
    been evaluated.
  • If none of them are .TRUE., evaluate block N
    within ELSE and ENDIF. After that, execute the
    statement following the ENDIF

30
Roots of Quadratic v.4
  • --------------------------------------------------
    -
  • ! Solve Ax2 Bx C 0 given BB-4AC gt 0
  • ! Detect complex roots and repeated roots.
  • ! ------------------------------------------------
    ---
  • PROGRAM QuadraticEquation
  • IMPLICIT NONE
  • REAL a, b, c, d, root1,root2
  • ! read in the coefficients a, b and c
  • READ(,) a, b, c
  • ! compute the discriminant d
  • d bb - 4.0ac
  • IF (d gt 0.0) THEN ! distinct
    roots?
  • d SQRT(d)
  • root1 (-b d)/(2.0a) ! first root
  • root2 (-b - d)/(2.0a) ! second root
  • WRITE(,) 'Roots are ', root1, ' and ',
    root2
  • ELSEIF (d 0.0) THEN ! repeated
    roots?
  • WRITE(,) 'The repeated root is ',
    -b/(2.0a)

31
Example in previous midterms
  • PROGRAM X
  • IMPLICIT NONE
  • LOGICAL A,B,C
  • INTEGER I,J,K
  • I 5/22.5
  • J 13.0/3.0 0.66
  • K 5
  • A (IJ)
  • B (K gt J)
  • C (KI)
  • WRITE (,) A, B, C
  • END PROGRAM
  • 1- T T T
  • 2- T F T
  • 3- T F F
  • 4- F T F
  • 5- None of the above

32
Complex Logical Expressions
  • In addition to relational operators, more complex
    logical expressions can be formed using logical
    operators
  • The Logical Operators listed in order of
    decreasing precedence are
  • .NOT.
  • .AND.
  • .OR.
  • .EQV., .NEQV.
  • The precedence of all logical operators is lower
    than all relational operators
  • They all associate from left to right except .NOT.

High
Low
33
Logical operator .AND. ()
ltexp1gt ltexp2gt
  • Examples
  • (2gt1) .and. (3lt4) .TRUE.
  • (2gt1) .and. (3gt4) .FALSE.
  • (2lt1) .and. (3lt4) .FALSE.
  • (2lt1) .and. (3gt4) .FALSE.

ltexp1gt .AND. ltexp2gt
exp2
exp1
.TRUE.
.FALSE.
.FALSE.
.FALSE.
34
Logical operator .OR. ()
ltexp1gt ltexp2gt
  • Examples
  • (2gt1) .OR. (3lt4) .TRUE.
  • (2gt1) .OR. (3gt4) .TRUE.
  • (2lt1) .OR. (3lt4) .TRUE.
  • (2lt1) .OR. (3gt4) .FALSE.

ltexp1gt .OR. ltexp2gt
exp2
exp1
35
Logical operator .EQV.
  • Examples
  • (2gt1) .EQV. (3lt4) .TRUE.
  • (2gt1) .EQV. (3gt4) .FALSE.
  • (2lt1) .EQV. (3lt4) .FALSE.
  • (2lt1) .EQV. (3gt4) .TRUE.

ltexp1gt .EQV. ltexp2gt
exp2
exp1
36
Logical operator .NEQV.
  • Examples
  • (2gt1) .NEQV. (3lt4) .FALSE.
  • (2gt1) . NEQV. (3gt4) . TRUE.
  • (2lt1) . NEQV. (3lt4) .TRUE.
  • (2lt1) . NEQV. (3gt4) .FALSE.

ltexp1gt .NEQV. ltexp2gt
exp2
exp1
37
Logical operator .NOT.
  • Examples
  • .NOT. (2lt1) .TRUE.
  • .NOT. (2gt1) .FALSE.

.NOT. ltexpgt
exp
38
Examples
  • Suppose we have the declaration
  • INTEGER age34, old92, young16
  • What is the value of the following expressions?
  • age / old
  • age gt young
  • age 62
  • age56 .and. old/92
  • age56 .or. old/92
  • age56 .or. .not.(old/92)
  • .not. (age56 .or. old/92)

39
Another Example
  • Suppose the integer variable, n has value 4.
  • n2 1 gt 10 .AND. .NOT. n lt 3
  • ? 42 1 gt 10 .AND. .NOT. 4 lt 3
  • 42 1 gt 10 .AND. .NOT. 4 lt 3
  • 16 1 gt 10 .AND. .NOT. 4 lt 3
  • 16 1 gt 10 .AND. .NOT. 4 lt 3
  • 17 gt 10 .AND. .NOT. 4 lt 3
  • 17 gt 10 .AND. .NOT. 4 lt 3
  • .TRUE. .AND. .NOT. 4 lt 3
  • .TRUE. .AND. .NOT. 4 lt 3
  • .TRUE. .AND. .NOT. .FALSE.
  • .TRUE. .AND. .NOT. .FALSE.
  • .TRUE. .AND. .TRUE.
  • .TRUE.

40
Example in the final of Fall 2006
  • What is the output of the following Fortran
    program?
  • PROGRAM exam
  • IMPLICIT NONE
  • LOGICAL X,Y
  • INTEGER I2, J3
  • REAL A5.0,B9.0
  • X (A/I) lt (B/J)
  • Y (J/I) (B/A)
  • X X .AND. Y
  • Y X .OR. Y
  • WRITE (,) X, Y
  • END PROGRAM exam
  • a). T F
  • b). T T
  • c). F T
  • d). F F
  • e). The program does not compile
Write a Comment
User Comments (0)
About PowerShow.com