Parte I
Ing. Nicola Forgione
Dipartimento di Ingegneria Meccanica, Nucleare e della Produzione
E-mail: nicola.forgione@ing.unipi.it; tel. 0502218057
Development of Fortran Standard
Syntax in FORTRAN 95
• Form 1
DO var = in-val, fin-val, step
statements
END DO
• Form 2 (if the step is = to 1)
name: DO var = in-val, fin-val
statements
END DO name
where: var is a variable of type INTEGER, called
loop variable or loop index;
in-val specify the initial value of var;
fin-val specify the final value of var;
step specify the step-size value of var.
• The do-loop variable must never be changed by other statements within the loop
DO – WHILE Statement
• The DO-WHILE statement executes the block of statements while a specified
condition remains true
Syntax
DO lab WHILE (expr)
statements
lab statement (or END DO)
IF (THETA-MU) 50,50,100
• The following example transfers control to statement 400 if the value of the integer
variable N is even. It transfers control to statement 200 if the value is odd.
IF (N/2*2-N) 200,400,200
• The following statement transfers control to statement 100 for N<5, to statement
200 for N=5, and to statement 300 for N>5:
• Executes one statement based on the value of a logical expression. (This statement
was called a logical IF statement in FORTRAN 77)
Syntax
IF (expr) stmt
Logical false
expression
where: expr is a scalar logical expression
(enclosed in parentheses); true
Examples:
IF((x>=0).AND.(y>=0)) THEN
IF (x.GT.0) THEN z=sqrt(x)+sqrt(y)
y=sqrt(x) w=z+5
ENDIF ENDIF
IF-Construct
• IF-THEN-ELSE-ENDIF: executes one block of statements if the logical expression
is true otherwise executes another block of statements .
Syntax
IF (expr) THEN
block1 Logical false
expression
ELSE
block2 true
ENDIF
Block 1 Block 2
where: expr is a scalar logical expression
(enclosed in parentheses);
block1 is the first sequence of statements.
block2 is the second sequence of statements.
IF (x>=0) THEN
y=sqrt(x)
ELSE
y=exp(x)-1
ENDIF
IF-Construct
• IF-THEN-ELSEIF-ELSE-ENDIF: executes one block of statements (block1) if the
logical expression (expr1) is true otherwise executes another block of statements
(block 2) if the corresponding logical expression (expr2) is true otherwise executes
another block of statements (block3).
Syntax
IF (expr1) THEN
block1 Logical false
expression
ELSE IF (expr2) THEN 1
block2
true
ELSE Logical false
block3 expression
ENDIF 2