Next: Runtime errors
Up: Fortran Programs
Previous: A Fortran Program

Syntax Errors

Try running the program. Remember to first compile and then run the program. (Since the name of the file is ``sample2.f'', the name of the executable will be ``sample2''.) When the program asks for your input, be sure to type the Enter key at the end of your input.

It's nice if you can write a correct program on the first try, but few of us can. In the rest of this lesson we'll consider ways in which you can make mistakes.

One common kind of mistake is a syntax error, which is the Fortran equivalent of a misspelled word, a punctuation mistake, or a grammatical mistake in written English. If you make a small mistake, the Fortran compiler will be able to give you a fairly precise explanation of what you did wrong. For example, go to the sample program and change ``READ'' to be ``RED''. What happens when you try to compile it?

Click here for the answer

Fix up the mistake you just introduced, and then change the line reading ``Z = X / Y'' to read ``Z = X /'' instead. What happens when you try to compile it?

Click here for the answer

Fix that mistake and then add spaces to the beginning of the comments so that they are indented just like the result of the program. You might make this mistake if you forget that comments must begin in the first column. What happens when you compile it?

Click here for the answer

Remember---the bigger the mistake, the more obscure the error message is likely to be. Put the comments back where they belong, and then delete one space from the beginning of each program statement. You might make this mistake if you forget that program statements must begin in the seventh column. What happens when you compile it?

Click here for the answer

Fix up the problem on line 6 and try compiling again. The error messages become ever more bizarre! Try adding the spaces back one line at a time and see what kinds of errors the compiler reports.

The moral of the story: compiler error message can be helpful, but they can also be confusing. When you get one something is definitely wrong---but it may be a little tricky to figure out exactly what is wrong!


Next: Runtime errors
Up: Fortran Programs
Previous: A Fortran Program

Hamlet Project
hamlet@cs.utah.edu