Fortran77 expressions and assignment conditions and loop statements

preface

Fortran77 expressions and assignments

1, Expression

1. Constant expression

Constants are the simplest expressions. There are six constants, corresponding to six data types.

constantdata type
Integer1, 0, -100, 32767, +15( ± 2 ∗ 1 0 9 \pm2*10^9 ±2∗109)
Real (real 32-bit)1.0, - 0.25, 2.0E6, 3.33E-1 (E6 is multiplied by 1 0 6 10^6 106)
Double precision (64 bit double precision floating point number)2.0D-1, 1D99, (D99, i.e ∗ 1 0 99 *10^{99} ∗1099)
Complex (complex)(2, - 3), (1., 9.9E-1) (only Integer or real in parentheses) (real part, imaginary part)
Logical constant.TRUE. .FALSE. (there are only two values and they should be enclosed by two...)
Character (symbol constant)‘ABC’, ‘Anything goes!’, ‘ It's a nice day

2. Non constant expression

The simplest non constant expression is:
operand operator operand
x + y
**It's power 2 * * 3, which is the third power of 2
In addition, you should pay attention to the division operation, If two integers are divided, it is integer division, removing the decimal. For example: 3 / 2 = 1 But if it's 3/ 2 = 1.5 (because 3. Is a decimal)

2, Assignment

Assignment consists of the following types:
variable_name = expression
area = pi * r**2

3, Type conversion

When different types of data types appear in an expression, type conversion occurs, implicit or explicit.
For example:

real x
x=x+1

This program will increase x by 1 and turn 1 into a decimal. This is implicit conversion.
However, it is recommended to use cast in key computing parts. Fortran provides the following cast functions.

int
real
dble
ichar
char

ichar converts a character to an integer, while char converts an integer to a character

4, Logical expression operator

.LT. // <
.LE. // <=
.GT. // >
.GE. // >=
.EQ. // =
.NE. // NOT Equal

5, Logical variables and assignment

Commonly used in IF statement

.AND.
.OR.
.NOT.
Logical a, b
a=.TRUE.
b = a .AND 3 .LT. 5/2

6, if statement

Conditional statements play an important role in any programming language. The most common conditional statement in Fortran is the IF statement. IF statements take several forms.

1. Form 1

There is only one expression statement after if. This needs to be written on one line, which is in the position of columns 7-72 (each space is a column)

if (logical expression) executable statement
if (X .LT. 0) X=-X

2. Form 2

If you need to execute many statements after if, you have to use the following if expression statement.

if (logical expression) then
	executable statement
endif
if (logical expression) then
	statements
elseif (logical expression) then
	statements
	:
	:
else
	statements
endif

3,Nested if statements. Nested IF statement

if statements can be nested at several levels. In order to ensure readability, you need to use appropriate indentation.

if (x .GT. 0) then
       if (x .GE. y) then
           write(*,*) 'x is positive and x >= y'
        else 
             write(*,*) 'x is positive and x <= y'
       

IF statements can be nested, but you should avoid using a lot of nesting.

7, Circular statement

There is only one do loop statement in Fortran, which is similar to for loop. There is another one that looks very low, that is, if and go to statements.

1.do-loop

Do loop can be used for simple counting.

c2345678
      integer i, n, sum
      sum = 0
      do 10 i = 1, n
          sum = sum +i
          write(*,*) 'i = ', i
          write(*,*) 'sum = ', sum
  10  continue      

This 10 is the statement label, which is generally an integer multiple of 10
The general do loop template is:

c2345678
      do label  var =  expr1(start), expr2(end), expr3(step)
         statements
label continue

The VaR of do loop is generally increasing by 1. This loop variable var should not be changed by other statements in the loop, otherwise there will be great misunderstanding. Many Fortran compilers allow the use of enddo statements. The advantage is that do and enddo are a closed loop.
Unlike other loops, Fortran loop loops evaluate, start, end, and step only once. For example, the following loop is not an infinite loop.

c2345678
      integer i,j
 
      read (*,*) j
      do 20 i = 1, j
         j = j + 1
  20  continue
      write (*,*) j

2.while-loop

c2345678
      while (ligical exp) do
          statements
      enddo
c2345678
      do while (ligical expr)
          statements
      enddo

Still, the above program still needs to become an if and goto statement

label if (logical expr) then
         statements
         goto label
      endif 

3.if and goto loop

c2345678
label if (logical expr) then
          statements
          goto label
      endif
c2345678
      Integer n
      n=1
10    if (n .le. 100) then
          write (*,*) n
          n = n*2
          goto 10
       endif

4.until-loops

c2345678
      do
         statements
      until (logical expr)

The above program is the combination of if and goto

c2345678
label continue
         statements
      if (logical expr) goto label

Keywords: fortran

Added by Isomerizer on Thu, 17 Feb 2022 19:42:17 +0200