Fortran How does Fortran Execute this Statement?

  • Thread starter Thread starter ecastro
  • Start date Start date
  • Tags Tags
    fortran
AI Thread Summary
In the discussed Fortran code, the statement `x = 0` initializes the variable `x` to zero. The subsequent `read(5, *, end = 10) x` attempts to read a value from file 5 (the keyboard). If a value is successfully read, `x` is updated, and the `if` condition is evaluated. If the end of the file (EOF) is reached during the read operation, the program skips to line 10, bypassing the `if` statement and retaining the last value read into `x`. The conversation clarifies that the code does not contain an explicit loop; additional code would be needed to repeatedly execute the read statement. It is emphasized that `x` starts as zero but is later assigned the value read from the keyboard, distinguishing between initialization and assignment.
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?
 
Technology news 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
1K
Replies
8
Views
4K
Replies
5
Views
5K
Replies
3
Views
3K
Replies
19
Views
6K
Replies
8
Views
4K
Replies
1
Views
4K
Back
Top