1 / 19

Fortran: Expressions and Assignments

ICoCSIS. Fortran: Expressions and Assignments. Session Two. Outline. Introduction: language elements Scalar Numeric Expressions Defined and Undefined Variables Scalar Relational Expressions Scalar Logical Expressions and Assignments Scalar Character Expressions and Assignments

altessa
Download Presentation

Fortran: Expressions and Assignments

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. ICoCSIS Fortran: Expressions and Assignments Session Two

  2. Outline • Introduction: language elements • Scalar Numeric Expressions • Defined and Undefined Variables • Scalar Relational Expressions • Scalar Logical Expressions and Assignments • Scalar Character Expressions and Assignments • Array Expressions and Assignments • Pointers in Expressions and Assignments

  3. Language Elements • Letters: character set • Words: constants, keywords, names (tokens) • Phrases: expressions • Sentences: statements

  4. Expressions Expressions describe a computation that is to be carried out by the computer. During program execution every expression is evaluated and the value can be assigned to a variable. An expression in Fortran is formed of operands and operators, combined in a way that follows the rules of Fortran syntax. A simple expression involving a dyadic (or binary) operator has the form operand operator operand x+y and a unary or monadic operator has the form operator operand  -y Parts of expressions without parentheses are evaluated successively from left to right for operators of equal precedence a/b/c  a/(b*c). The operands may be constants, variables, or functions and an expression may itself be used as an operand. The type and kind of the result are determined by the type and kind of the operands and do not depend on their values.

  5. Scalar Numeric Expressions (1) A numeric expression is an expression whose operands are one of the three numeric types: integer, real, complex whose operators are ** exponentiation * / multiplication, division + - addition, subtraction These operators are known as numeric intrinsic operators, and are listed here in their order of precedence. Minus sign (-) and the plus sign (+) can be used as unary operators. Note: In integer division, result is truncated: 8/3 is 2; 2**3 is 8, but 2**(-3) is 1/(2**3) is zero.

  6. Scalar Numeric Expressions (2) Mixed-mode expressions: The object of the weaker (or simpler) of the two data types will be converted, or coerced, into the type of the stronger one. The result will also be that of the stronger type. Type of result a .op. b

  7. Defined and Undefined Variables real :: speed The variable speed has no value, it is undefined and cannot be referenced. To assign a value assignment statement is needed speed = 2.997 which defines it. For a compound object, all of its sub-objects that are not pointers must be individually defined before the object as a whole is regarded as defined. The pointer association status may be undefined, associatedwith a target, or disassociated.

  8. Scalar Numerical Assignments variable = expression Type conversion: i = 7.3 ! i of type default integer a = (4.01935, 2.12372) ! a of type default real To i is assigned a value of 7 and to a the value of 4.01935. If a literal constant is assigned to a variable of greater precision, the result will have the accuracy of the constant: Given a variable a of kind long, the result of a = 1.7 will be less precise than that of a = 1.7_long

  9. Scalar Relational Expressions • The relational operators are: • < or .lt. less than • <= or .le. less than or equal • == or .eq. equal • /= or .ne. not equal • > or .gt. greater than • >= or .ge. greater than or equal • If either or both of the expressions are complex, only the operators == and /= (or .eq. and .ne.) are available.

  10. Scalar Logical Expressions and Assignments Logical constants, variables, and functions may appear as operands in logical expressions. The logical operators, in decreasing order of precedence, are: unary operator: .not. logical negation binary operators: .and. logical intersection .or. logical union (inclusive) .eqv. logical equivalence .neqv. logical non-equivalence (allows exclusive or) (a.and..not.b .or. .not.a.and.b) logical :: i,j,k,l .not.j(unary) j .and. k i .or. l .and. .not.j(precedence) ( .not.k .and. j .neqv. .not.l) .or. i

  11. Scalar Character Expressions and Assignments The only intrinsic operator for character expressions is the concatenation operator //. ’AB’//’CD’ is ’ABCD’ word1(4:4)//word2(2:4)

  12. Consider Example 2 • implicit none means that all variables must be explicitly declared. Optional, but helps to avoid bugs • (problems the “parameter” label indicates that these things have a fixed value that cannot be altered later • “!” indicates a comment: everything after it is ignored • The do...end do loop construct • The if...else if...else...end if construct • Comparison operators are <, >, ==, /=, >=, <= Note: in f77 these are .lt. .gt. .eq. .ne. .ge. .le. • Indentation helps see the structure Write your program to do loops: Calculate the sum of all even numbers between 12 and 124.

  13. Array Expressions and Assignments (1) Given the array declarations real, dimension(10,20) :: a,b real, dimension(5) :: v the following are examples of array expressions: a/b ! Array of shape (10,20), with elements a(i,j)/b(i,j) v+1. ! Array of shape (5), with elements v(i)+1.0 5/v+a(1:5,5) ! Array of shape (5), with elements 5/v(i)+a(i,5) a == b ! Logical array of shape (10,20), with elements ! .true. if a(i,j) == b(i,j), and .false. Otherwise Two arrays of the same shape are said to be conformable.

  14. Array Expressions and Assignments (2) The correspondence is by position in the extent and not by subscript value. For example, a(2:9,5:10) + b(1:8,15:20) has element values a(i+1,j+4) + b(i,j+14), i=1,2,...,8, j=1,2,...,6 a = a + 1.0 Replaces a(i,j) by a(i,j)+1.0 for i = 1, 2, . . . , 10 and j = 1, 2, . . . , 20. a(1,11:15) = v ! a(1,j+10) is assigned from v(j), j=1,2,...,5 If the expression includes a reference to the array variable or to a part of it, the expression is interpreted as being fully evaluated before the assignment commences. For example, the statement v(2:5) = v(1:4) results in each element v(i) for i = 2, 3, 4, 5 having the value that v(i-1) had prior to the commencement of the assignment.

  15. Pointers in expressions and assignments A pointer may appear as a variable in the expressions and assignments, provided it has a valid association with a target. The target is accessed without any need for an explicit dereferencing symbol.In case the left-handpointer to point to another target, the descriptor to be altered. This is called pointer assignment: pointer => target x => null() If the target in a pointer assignment statement is a variable that is not itself a pointer or a sub-object of a pointer target, it must have the target attribute. For example, the statement real, dimension(10), target :: y declares y to have the target attribute. Any non-pointer sub-object of an object with the target attribute also has the target attribute. The target attribute is required for the purpose of code optimization by the compiler. It is very helpful to the compiler to know that a variable that is not a pointer or a target may not be accessed by a pointer target.

  16. Examples Example3 – miscellanies • Continuing lines: – f95 use ‘&’ at the end of the line – f77: put any character in column 6 on next line • Formats of constants: – Use ‘.’ to distinguish real from integer (avoid 2/3=0 !) – 1.234x10-13is written as 1.234e-13 • logical variables have 2 values: .true. or .false. (T or F) Variable naming rules: – start with letter – mix numbers, letters and _ – no spaces

  17. Example 3 Example3 – miscellanies Some intrinsic mathematical functions • abs (absolute value, real or integer); sqrt(square root) • sin, cos, tan, asin, acos, atan, atan2: assume radians • expand log : log is natural log, use log10 for base 10. Some conversion functions • int, nint, floor, ceiling, float, real • mod(a,b) is remainder of x-int(x/y)*y • max(a,b,c,….), min(a,b,c,…)

  18. I/O read(*,*) and write(*,*) read(*,*) and write(*,*) do the same thing as read* and print* but are more flexible: – The 1st * can be changed to a file number, to read or write from a file – The 2nd * can be used to specify the format (e.g., number of decimal places)

  19. Exercises Write your programs to: • Convert a real number to the nearest integer • Calculate the remainder after a is divided by b

More Related