How does Fortran Execute this Statement?

  • Context: Fortran 
  • Thread starter Thread starter ecastro
  • Start date Start date
  • Tags Tags
    fortran
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
ecastro
Messages
249
Reaction score
8
How does Fortran execute this statement?

x = 0

read(5, *, end = 10) x

if (x.eq.1) then
...
endif

10 ...

The ellipsis stands for a list of commands, i.e the 'if' condition and line 10 have several lines. I have looked in Google and if my understanding is correct, the 'read' statement will be in a loop, i.e. it will be reading the value in file 5 (the keyboard) until it reaches the end of the file, and then it continues to line 10. My question is if this is so, then will Fortran execute the 'if' condition?
 
on Phys.org
Hello again. Doing OK?
x will have the first value in the file. When it read that x the if was executed and things proceeded.

If it then comes again to the read statement (because of an outer loop we don't see here) and reads an EOF it jumps to 10 and does not execute the if.

In such a case x has the last value as read from the file in the preceding pass. In that pass the if was executed.

In other words: reading the EOF is a separate read.
 
Thank you for your help last time.

So at the initial run, the value of 'x' is primarily from the keyboard input, not zero? That is, setting 'x' to zero in this case is similar to initializing the variable?
 
ecastro said:
if my understanding is correct, the 'read' statement will be in a loop, i.e. it will be reading the value in file 5 (the keyboard) until it reaches the end of the file, and then it continues to line 10.

BvU said:
If it then comes again to the read statement (because of an outer loop we don't see here) and reads an EOF it jumps to 10 and does not execute the if.

I wanted to emphasize something that BvU wrote. The code that you presented does not contain a loop. As it stands, it does the following:

  1. It sets x to zero.
  2. It tries to read one line from the file, into x.
  3. If there is nothing to read, because it was at the end of the file already, it skips ahead to statement 10, i.e. does not test the if-statement or perform its contents. At this point x contains zero.
  4. Otherwise, it reads a number into x. Then it tests the if-statement, performs its contents if the condition is true, and finally reaches statement 10. At this point x contains whatever was read from the file.

In order to continue reading more lines from the file, there has to be additional code that takes the flow of execution back to the 'read' statement.
 
Last edited:
ecastro said:
Thank you for your help last time.

So at the initial run, the value of 'x' is primarily from the keyboard input, not zero?
Not sure what you mean by this. x is first set to 0 (which happens via an assignment statement), and then is later set to whatever value is read from the keyboard.
ecastro said:
That is, setting 'x' to zero in this case is similar to initializing the variable?
Not really. Here are two variable declarations.
Fortran:
real :: x
real :: r = 1.5
.
.
.
x = 2.0
In this code x is uninitialized, meaning that its value is whatever happens to be in the memory allocated for this variable. r is initialized to 1.5. Later in the code, the value 2.0 is assigned to x. x was originally initialized to some unknown value.