How does Fortran Execute this Statement?

  • Context: Fortran 
  • Thread starter Thread starter ecastro
  • Start date Start date
  • Tags Tags
    fortran
Click For Summary

Discussion Overview

The discussion centers around the execution of a Fortran statement involving reading input from a file (or keyboard) and the implications of end-of-file (EOF) conditions on variable assignment and control flow. Participants explore the behavior of the 'read' statement, particularly in relation to the variable 'x' and the subsequent 'if' condition.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant suggests that the 'read' statement will loop until EOF is reached, questioning whether the 'if' condition will execute in that case.
  • Another participant clarifies that 'x' will hold the first value read from the file, and if EOF is encountered, the program will jump to line 10 without executing the 'if' condition.
  • A different participant emphasizes that the initial value of 'x' is set to zero, and it is only updated when a value is read from the file, challenging the idea that it is initialized from keyboard input.
  • One participant points out that the provided code does not contain a loop and explains the flow of execution, detailing how 'x' is set and how the program behaves upon reaching EOF.
  • Another participant discusses the difference between initialized and uninitialized variables, asserting that 'x' is initially set to zero, while another variable 'r' is explicitly initialized to a value.

Areas of Agreement / Disagreement

Participants express differing views on the execution flow of the Fortran code, particularly regarding the handling of EOF and the initialization of 'x'. There is no consensus on the interpretation of how the 'read' statement operates in this context.

Contextual Notes

Some participants note the absence of a loop in the provided code, which affects the understanding of how multiple reads would occur. There are also discussions about the implications of variable initialization and the state of 'x' at different points in execution.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K