How to Use the Arithmetic Assignment Statement

There are different types of assignment statements in Fortran. The arithmetic assignment statement gets confusing especially when you see statements such as i=i+1. Let us see how to use this statement correctly.

The arithmetic assignment statement has the form:

variable = arithmetic expression

Variable can be a number or array of any of the following types:
  •  integer
  •  real
  • double precision
  • complex 
 Why the Confusion
The result of the arithmetic expression is of the type assigned to the variable. It is stored with the variable name. This is really straightforward if you have a statement such as a=b+c, C=(Matmul,A,B), etc. The problem arises when you see the variable name on the right hand side and start looking at the statement as an equation.
e.g.
  • i=i+1
  • n=2**n
  • name= name*10
Making Things Clear
You should first understand that the assignment statement is not an equation. Whereas, sum=sum+1, count=count+1, s=5*(s-1), etc. can be meaningless in arithmetic, they are not so in the assignment statement.
This is because what we use on the left side of the statement is merely a name. The right side says how to evaluate that name. Hence, the right side may include the name. If it does, it will hold the value that the name possessed before the execution of the statement. Once, the statement is executed the name will hold the new value of the name.

How it Helps
This is extremely helpful if we need to perform a series of operations for different values of the same variable or find cumulative results.

2 comments:

  1. As an engineer, I used to use Fortran77 by Watcom getting a master's in mechanical engineering and just wondered what Fortran is being used here, 66,77,95,08? Probably downward compatible to
    Fortran 66, no? Since I have programmed a lot in C and Pascal, but miss the old Fortran.

    ReplyDelete
  2. Wow, thanks for the comment. This is a really old blog. It was F95 I think.

    ReplyDelete