Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran How does Fortran Execute this Statement?

  1. Mar 15, 2017 #1
    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?
     
  2. jcsd
  3. Mar 15, 2017 #2

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.
     
  4. Mar 16, 2017 #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?
     
  5. Mar 16, 2017 #4

    jtbell

    User Avatar

    Staff: Mentor

    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: Mar 16, 2017
  6. Mar 16, 2017 #5

    Mark44

    Staff: Mentor

    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.
    Not really. Here are two variable declarations.
    Code (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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted