1

I'm doing a very basic Fortran tutorial to learn it for grad school and I input the codes for conditionals and loops exactly as they were written in the tutorial but I keep getting the "unexpected end of file" error when I try to compile with gfortran.

This is my conditional code:

if (angle < 90.0) then

    print *, 'Angle is acute'

else if (angle < 180.0) then

    print *, 'Angle is obtuse'

else

    print *, 'Angle is reflex'

end if

This is my loop code:

integer :: i

    do i=1,10,2
    
        print *, i ! print odd numbers
    end do

Both of them are completed with end statements, so I'm not sure what else it wants. I've only just started teaching myself today, so I'm still just copying codes verbatim from the tutorial and not sure how to troubleshoot anything.

1
  • Are these your complete programs? If not can you include the complete program, please Commented Feb 5, 2021 at 22:04

2 Answers 2

1

In the tutorial you are following the pieces of code shown are not complete programs. They cannot be compiled as they are.

You will see this frequently, especially in answers on this site.

The code fragments shown miss a lot of context to make clear the parts that are to be taught. That's perhaps a little unfortunate, but Fortran is quite a verbose language, and so there's a trade-off for clarity.

For a complete program you've possibly seen that "all programs must have an end statement to complete them". You may think that the end if and end do statements are suitable for this. They are not: you need an end program statement. The following are two minimal programs:

end

and

end program

(There are also forms with a program statement.)

That is:

if (.true.) then
  print *, "Hello, world!"
end if

end program

is compilable if and only if that last line exists.

Further, things like implicit none and variable declarations and definitions would be part of the implied context of example fragments.

Sign up to request clarification or add additional context in comments.

Comments

1

If you are just learning and want to try code start with the following skeleton

program test
implicit none

<variable declarations>

<program code>

stop
contains

<function definitions>

end program

The above structure should be enough for most one use programs (write a program to do one thing). The stop above isn't required, it just makes it clear to see where the program instructions actually end.


If you want to use standard numeric types include the iso_fortran_env before the implicit none statement

program test
use, intrinsic :: iso_fortran_env
implicit none

the above will allow you to define integer(int32), integer(int64), real(real32), real(real64), etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.