How does Fortran Execute this Statement?

  • Fortran
  • Thread starter ecastro
  • Start date
  • Tags
    fortran
In summary, the code snippet shown executes the 'read' statement in a loop, reading from file 5 (or the keyboard) until it reaches the end of the file. If the end is reached, it continues to line 10 without executing the 'if' condition. If there is more to read, the value is stored in 'x' and the 'if' condition is tested. This code does not contain a loop, so in order to continue reading from the file, additional code is needed to return to the 'read' statement. Setting 'x' to zero in this case is not similar to initializing the variable, as it is being set to zero after being initialized
  • #1
ecastro
254
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
  • #2
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.
 
  • #3
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?
 
  • #4
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:
  • #5
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.
 

1. How does Fortran execute a statement?

Fortran executes a statement by following a specific set of rules, known as the execution model. This model dictates the order in which statements are executed and how data is stored and accessed in memory.

2. What is the syntax for writing a Fortran statement?

The syntax for writing a Fortran statement is as follows: variable = expression. This means that a statement consists of a variable on the left side, an equal sign, and an expression on the right side. The expression can include mathematical operations, function calls, and other statements.

3. How does Fortran handle errors or exceptions in statement execution?

Fortran has built-in error handling mechanisms, such as the ERROR STOP statement, that allow the programmer to specify how the program should respond to errors or exceptions. The program will stop executing and display an error message if an error occurs during statement execution.

4. Can Fortran execute multiple statements at once?

Yes, Fortran can execute multiple statements at once by using control structures such as DO loops and IF statements. These structures allow for branching and repetition in the program's execution, allowing for multiple statements to be executed in a specific order.

5. How does Fortran optimize statement execution for efficiency?

Fortran is designed to be an efficient programming language, and statement execution is optimized in several ways. This includes using compiler optimizations, such as loop unrolling and vectorization, to improve performance. Fortran also allows for parallel execution of statements, which can further increase efficiency on multi-core processors.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
622
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top