In Chapter 3, we learned that Fortran has six intrinsic data types: integer, real, double precision, complex, logical, and character. We used these data types to declare variable names in declaration statements.
This chapter describes how to fill in values (constants) for each one of these variable names. Also described in this chapter are: (1) assignment statements, (2) arithmetic, logical, and character operations, and (3) a few commonly used intrinsic functions. Definitions
The arithmetic variable can be of integer, real, complex, or double precision type (see Chapter 3). Operations with arithmetic and logical variables are based on rules of rank of data types, and order of precedence of operators (Section 5.3).
5.1 CONSTANTS
A constant is a value that can be stored in the computer's memory. The value is stored in a specific location, the location is referred to by its address, and the address is referred to by its variable name. The programmer declares the variable name and assigns a value to it; the computer keeps track of the location and address.
Integer Constants An integer constant is a whole number with no decimal point. It has the form: si where s is the sign (+ or ) and i is the integer number. For example, 135 is an integer constant. The following rules apply for integer constants:
0 92 345 The following are examples of invalid integer constants: 3.1416 31,860 9753845210 Periods or commas are not allowed, and the last example (9753845210) is outofrange (it is too large, even for a 4byte integer constant). In unformatted input, integer constants should have no decimal point. If the decimal point is present, with or without a fractional part, the decimal point is ignored and the fractional part, if any, is disregarded. Unformatted integer output appears with no decimal point.
A real constant is a number that is written with a decimal point and may have a fractional part following the decimal point. A real constant has one of the following forms: s.r sr.r sr. where s is the sign (+ or ) and r is a string of one or more decimal digits; for example, 132.764 is a real constant. The decimal point precedes or follows a string r, or is embedded between two r's. As with an integer constant, a real constant can be negative, zero, or positive. Although the plus sign (+) is optional, the minus sign () is required preceding a negative real constant. Real constants may also be written in exponential notation. Exponential notation uses the letter E, followed by a sign (+ or ) and an integer exponent, to represent "... times 10 to the power ..." A real constant can contain only the numerals 0 through 9, algebraic signs, a decimal point, and the letter E. In exponential notation, a real constant can take any of the following forms: s.rEsk sr.rEsk sr.Esk iEsk where k is the integer exponent and all other symbols have been previously defined. An example is: 132.764E2 The following rules apply for exponential notation:
The following are examples of valid real constants: 3.14159 0.56 3.56E3 123.456E0 56E4 The following are examples of invalid real constants: 3,1416 $33560 123._67 Commas or special characters such as $ or _ are not allowed as part of real constants. In unformatted input, a real constant should have a decimal point and, optionally, a fractional part. If a real constant has no decimal point and therefore, no fractional part, a decimal point is added to the right of the constant and the fractional part is taken to be a string of zeros. Unformatted real output has a decimal point and a fractional part, which may be a string of zeros.
A doubleprecision constant is written in exponential notation by using the letter D instead of E. A doubleprecision constant has one of the following forms: s.rDsk sr.rDsk sr.Dsk iDsk where D denotes "... times 10 to the power ..." and all other symbols have been previously defined. For example: 13257.3619D3 As with real exponential notation, plus signs are optional and minus signs are required. The exponent k is required even if it happens to be zero. There are two types of doubleprecision constants: Dtype and Gtype. In Dtype, the exponent k can have at most two digits. In Gtype, the exponent k can have at most three digits. The type of doubleprecision constant is determined during compilation time in a processordependent manner. There is no limit to the number of digits of the string r, but only the leftmost n digits (the value of n is processor dependent) are significant. For an 8byte doubleprecision wordsize, n= 16 and k= 2 for a Dtype constant; and n= 15 and k= 3 for a Gtype. It can be seen that a Gtype constant has a slightly lesser accuracy than Dtype, but it allows for a larger integer exponent. The following are examples of valid doubleprecision constants: .123D3 0.31D25 35071.D+3 56D299 The last example (56D299) is valid only as Gtype. In unformatted input, a doubleprecision constant should be specified using an exponential notation with the letter D. Unformatted doubleprecision output is written in exponential notation using the letter E.
In Fortran, a complex constant represents the complex number a + bi. A complex constant consists of a pair of real or integer constants, separated by a comma and enclosed in parentheses. The first constant represents the real part of the complex number (a) and the second represents the imaginary part (b). It takes the form: (a,b) where a and b are real or integer constants, as shown in the following examples: (2,3) (3.5,5.6) (123.45E3,5.43E1) In unformatted input, a complex constant should be enclosed in parentheses, with its real and imaginary parts separated by a comma. Unformatted complex output appears in the same way. Logical Constants A logical constant defines a logical value as either true or false. It takes one of the following forms: .TRUE. .FALSE. The surrounding (delimiting) periods are required. In unformatted input, a logical constant can be read as either .TRUE. or .FALSE., or any other character string beginning with the letters T (true) or F (false). Either of the surrounding periods is optional. Unformatted logical output appears as a single T (true) or a single F (false).
A character constant is a string of valid characters en closed within apostrophes. It takes the following form: 'abc&xyz'
The character string abc&xyz enclosed within apostrophes stands for any string of characters of the Fortran character set (Section 1.1). The value of the character constant is the string of characters contained between the required apostrophes. This value does not include the apostrophes, but it does include all blank spaces and tabs, if any, within them. The length of a character constant must be in the range of 1 to 2000 characters, including blank spaces. Within a character constant, the apostrophe character is represented by two consecutive apostrophes, with no blank spaces between them, as shown below: 'MCDONALD''S HAMBURGERS' This character constant would be stored and printed as follows: MCDONALD'S HAMBURGERS In unformatted input, a character constant should be surrounded by apostrophes. In unformatted output, a character constant appears as a character string without apostrophes. 5.2 ASSIGNMENT STATEMENTS
The three types of assignment statements are:
Arithmetic Assignment Statement The arithmetic assignment statement is perhaps the most common Fortran statement. It defines the value of a variable, be it integer, real, double precision, or complex. It has an equal (=) symbol preceded by a variable name and followed by a constant or arithmetic expression whose value is determined at execution time. The arithmetic assignment statement takes the form: v= e where v is a variable name and e is a constant or arithmetic expression. The symbol = does not mean "equal to" as in conventional mathematics. Rather, it means "is replaced by". For example, in the following statement: KCOUNT= KCOUNT + 1 the integer KCOUNT is incremented by one at execution time. The old value is replaced by the new value; the old value is lost and the new value remains in storage. The following statement defines the value of Z: Z= X + Y The values of X and Y should be defined prior to the execution of this statement. It is important to realize that some processors may assign initial values of 0. to all real variables. Then, the absence of a definition of X or Y will not stop execution. In the above case, if neither X nor Y were defined prior to this statement, the processor would add X = 0. to Y = 0. to give Z = 0.
In the assigment statement v = e:
In certain cases, the conversion may result in the loss of a portion of the data, as shown in the following statements: X= 3.5 J= X + 1 in which X is real and J is integer. Upon execution of these two statements, the value of X + 1 is 4.5; however, only its integer portion can be stored in J. Therefore, J= 4. Examples of valid arithmetic assignment statements are: I= J/K*M PI= 3.1416 B= 1.3D2**3.5 ALPHA= (3.2*X*Y)/(2.5*Z) ZT= Z1 + (Z2*Z3)  (Z4/Z5) COM= (12.5,34.7) + (9.6,3.5) Examples of invalid arithmetic assignment statements: 25.= A ALPHA= (3.2*X*Y)/(2.5*Z PI= 3.1416. ZT= Z1 + Z2*/Z3 Regarding these examples:
Logical Assignment Statement The logical assignment statement defines the value of a logical variable. It consists of an equal (=) symbol preceded by a logical variable name and followed by a logical constant (or logical expression) whose value is determined at execution time. The logical assignment statement takes the form: v = e where v is a logical variable name and e is a logical constant (or logical expression). The following are examples of valid logical assignment statements: A= .TRUE. BL= CL.AND.DL P= X.LT.3.5.AND..NOT.Q BIGV= V.GT.X.AND.V.GT.Y.AND.V.GT.Z Examples of invalid logical assignment statements: A= TRUE BL= CL.AND..DL P= X.LT.3.5.NOT..AND.Q BIGV= V.GT.X.AND.V.GT.Y.AND.GT.Z In these examples:
The character assignment statement defines the value of a character variable. It consists of an equal (=) symbol preceded by a character variable name and followed by a character constant (or character expression) whose value is determined at execution time. The character assignment statement takes the form: v = e where v is a character variable name and e is a character constant (or character expression). The following rules apply:
For example: CHARACTER*15 PROJECT,DATE PROJECT= 'SOLAR DEVELOPMENT NO. 2' DATE= 'MAY 10, 1994' The first statement declares PROJECT and DATE as character variables, each of maximum length equal to 15 spaces. The second statement assigns the character constant SOLAR DEVELOPMENT NO. 2 to PROJECT. The third statement assigns the character constant MAY 10, 1994 to DATE. Upon execution, the variable PROJECT is completely filled with the first 15 characters of SOLAR DEVELOPMENT NO. 2 The remaining characters are lost. The value of PROJECT is then SOLAR DEVELOPME
Likewise, the variable DATE is partially filled, starting from the left, with all the characters of MAY 10, 1994. The remaining three spaces to the right are filled with blank spaces denoted by b. The value of CHAR2 is then MAY 10, 1994bbb
Character constants are not initialized at execution time, remaining undefined until an assignment is made by a DATA or character assignment statement, or read from an input file. Following are valid character assignment statements: FRUIT= 'ORANGE' LABEL= 'THE VALUES ARE: ' ERROR_35= 'PLEASE EXAMINE VALUE OF Y' Following are invalid character assignment statements: FRUIT= 'APPLE NAME= 'MCDONALD'S' ERROR_45= IMPROPER VALUE OF Y Regarding these examples, note:
5.3 OPERATIONS
Arithmetic Operations Table 5.1 lists arithmetic operators. Table 5.2 shows examples of the order of precedence of arithmetic operations. Within one arithmetic expression, execution follows a preestablished order of precedence:
You may use parentheses to force an order of evaluation different from the preestablished order of precedence. Expressions within parentheses are evaluated first, working outwards to the expression within the outermost set. The expression contained within each set of parentheses is subject to the preestablished order of precedence. Nonessential parentheses do not affect the evaluation of the expression.
Arithmetic data types rank in this order, from highest to lowest:
When evaluating an arithmetic expression that contains two elements of different data type, the result is a variable of data type equal to that of the element of highest rank. For example, if two variables, one real and one integer, appear in one arithmetic expression, the result of the operation is of real type, as in A = 1.6 I = 2 C = A * I In this case, C is equal to 3.2. When an operation involves two integers, the result is also an integer. Therefore, any fraction resulting from the division of two integers is automatically truncated (lost). For example, in the following sequence of statements: I= 7 J= 2 K= I/J the value of K is stored as 3, not 3.5. Disregarding the possibility of this truncation is a common source of error in programming. In the following sequence of statements: I= 5 J= 2 X= 4. Y= I/J*X the expression for Y is evaluated from left to right. First, I/J= 2; second, Y is equal to 2 times 4, resulting in Y= 8. Parentheses may be used to override the established order of precedence. For instance: Y= I/(J*X) This results in first multiplying J times X, to yield 8., and then Y= 5/8. = 0.625. Note that two similar expressions (one with and another without parentheses) yield completely different answers. Logical Operations Table 5.3 lists the preestablished order of precedence of combined relational and logical expressions. Practical examples are shown in the box immediately following the table. As stated previously, when operators are of equal precedence, they are evaluated from left to right, except for exponentiation, which is evaluated from right to left. As with arithmetic operations, parentheses can be used to force an order of evaluation different from the preestablished order of precedence.
Character Linking Character strings can be linked together (i.e., concatenated) using the double slash operator (//), as shown in the following example: ALPHA='ABC'//'DEFGHI'//'JKLMNOPQRS'//'TUVWYZ' The character variable ALPHA is stored in memory as: ABCDEFGHIJKLMNOPQRSTUVWXYZ Embedded blank spaces, if any, are incorporated as part of the character string. 5.4 INTRINSIC FUNCTIONS
The Fortran library contains a set of intrinsic (builtin) functions. These functions perform common mathematical computations such as the square root, the logarithm, or a trig function. An example of the intrinsic function that calculates an absolute value is shown in the following sequence of statements: X= 3.2 Y= 10.5 Z= ABS(X) + Y In this example, the value of Z is evaluated as 13.7, i.e., the absolute value of X (3.2) plus the value of Y (10.5). A few commonly used intrinsic functions are described in Table 5.4.
The MOD function is used when action needs to be taken at regular intervals, as shown below. K= 010 K= K + 1 IF (MOD(K,5).EQ.0) THEN WRITE(6,*) 'FIFTH K VALUE' END IF IF (K.LE.100) GO TO 10 CONTINUE This loop executes 100 times. The message FIFTH K VALUE will be printed when the counter K is a multiple of 5 (i.e., every fifth passage through the loop). 5.5 EXAMPLE PROGRAMS
Example Program 1: Assignment Statements
This program defines a series of variables of different data types, including integer (I,J), real (A,B), double precision (D1, D2, D3), complex (C1, C2, C3), logical (L1, L2), and character (LAB, LABEL). The program proceeds to do a series of valid operations on these variables, and writes the output directly to the screen. The first line of output from this program (unformatted output) is: 17 11.45000 (5.000000,10.00000) 2203703683870369. T The second line is: X_X_X_ ABCDEFGHIJKLMNOPQRSTUVWXYZ Example Program 2: TicTacToe Board
The output from this program is: X XO O X X X0X0X0XOXOXOXOXOX X X O O X X X0X0X0XOXOXOXOXOX X X O O X X Note the following features of this program:
5.6 SUMMARY
This chapter describes constants, assignment statements, operations on arithmetic, logical and character variables, and a few commonly used intrinsic functions.
PROBLEMS

Documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view; download Adobe Acrobat Reader. 
FORTRAN FOR SCIENTISTS AND ENGINEERS VICTOR M. PONCE • ONLINE EDITION •  
Copyright © 2014 • Victor M. Ponce • All rights reserved. 