Back to index.html.
The 3-line program
Let’s write the first program in Fortran. The following is the program prints a phrase, “Hello, world!”, on your screen.
program hello print *,"Hello, world!" end program hello
Rules of Fortran program
The above program is an only 3-line program, but it has the basic rules in Fortran.
- The code should be separated by a line.
- The program starts with the keyword
programfollowed by an arbitrary but unique program name (
helloin this case).
- The program ends with
end programwith the program name.
- All the codes should stay between
- All the codes should stay between
- The keyword
- The symbol
*specifies the default output format (leaving the output to the computer).
*, the comma is needed.
- The message comes the last, and it should be enclosed with
- The symbol
In this program,
end program, and
The output massage always has a while space at the beginning of the line on the screen. This behavior is the default setting (putting a space preceded by the message), and it was chosen due to a historical reason. You can change the output format by replacing
* by a format descriptor, which we see in the later section.
"Hello, world!" is literally interpreted as characters. Such an object is called literal. The literal characters should be encircled with
', see the following exercises).
You can put white spaces among keywords and literals as many as possible. Spaces at the beginning of the line are ignored. Also, an empty line is ignored.
Erros in program
When compiling your program, you may get an error from the compiler, and the compilation stops. For example, with GFortran, you may see the following error message (the content would be different by GFortran version).
hello.f90:2:9: 2 | print *,"Hello, world! | 1 Error: Unterminated character constant beginning at (1)
The error occurs because you forget to put
" at the end of characters in line 2 of the file
hello.f90. A bug is a mistake or some logical errors in the program. It is the simplest case that you can find the bug quickly. However, even though the bug is silly, you may overlook it because you believe the code is absolutely correct. Therefore, finding and removing the errors (debug) can be frustrating and taking the time. Here is some advice to debug your program.
- Please read the error message, and go to the line number that the compiler tells you.
- Look around the line and related code from an objective point of view.
- If the code is short, rewrite the same code from scratch.
- Create a minimal set of code that does not (or does) produce the issue.
- Ask somebody else to review your code; it is * incredibly* useful in some cases.
- One line has one statement.
- The program starts with
program+ a unique name and ends with
end program+ the name.
- A character literal is encircled with
- One or more spaces separate keywords and statements.
- If you get en error on compiling, read the error message carefully and remove the issue.
- Replace the message with your favorite one.
- Confirm the message starts from the second column of the screen.
'for the message and confirm it also works.
'from the message and confirm the program does not compile.
- Make sure each of the following changes works.
- Change the program name.
- Insert any number of spaces between keywords.
- Insert any empty lines into the program.
The above program is small, and you can immediately understand what this program performs. However, as it becomes complicated, the program needs to be clear for the programmer to understand what the code means. Generally, indents and comments will help you to increase the readability of the code. See the following code, which behaves the same as previous.
! ! showing some messages ! program hello print *,"Hello, world!" end program hello
!, and the following characters will be ignored; this is called a comment. The compiler removes the comments, so you don’t have to encircle the messages within the comment. In the above program, the first 3 lines will be ignored.
You can write a comment at the end of the line. The following is an extreme example that works as the previous program. You should not abuse the comments because of losing the readability.
! ! showing some messages ! program hello ! you can write a comment here. print *,"Hello, world!" ! it is also valid. end program hello ! this is the end of program. !!!!! !!!!! This is also valid. !!!!!
The indent is a series of white spaces at the beginning of a line. It visually emphasizes a program block in the program. In the above example,
end program define the unit, which can be aligned for readability.
The number of spaces used in indent is arbitrary. Many programmers use either 2, 3, 4, or 8 spaces. Some people use a tab character, but it is not always right because the width of the tab may represent differently dependent on environments. Some editors and IDEs convert the tab-key input to appropriate spaces.
You can insert extra spaces between keywords. Some spaces may improve the readability of the code.
! This is an extreme example. program hello print * , "Hello, world!" end program hello
If a line is very long, you can break it with
& into another line. You put
& at the end of the line, and you can continue the program in the next line.
program hello print *, & "Hello, world!" end program hello
Upper- vs lower-case characters
Fortran equates upper-case and lowercase characters for keywords. However, the characters in a literal will be differentiated. See the following example.
! mixed characters PROGRAM hello prinT *,"Hello, world!" End PrOgRaM hello
In the 1990s with Fortran 90, there was a recommendation using uppercase characters for statements and lowercase for other symbols. Still, now, some textbooks keep this tradition. However, if you don’t have to follow this rule, never use it because of poor code-readability and slow typing. Please use the syntax highlight equipped in any text editors if you need the keyword emphasis. In this tutorial, I will use the lowercase characters in the source code.
- The symbol,
!, starts a comment which the compiler ignores.
- The indent is recommended because it improves the readability of code. You can also insert extra spaces between keywords.
- The symbol,
&, connects two adjacent lines.
- Fortran equates upper-cases with lower-cases except for character literals.
- Make sure any comments will be ignored in the program.
- Set the indent in your program and see it works.
Playing with some more statements
More on printing
You can write any lines of code in your program. The following example shows 2 lines of messages. Each message will be shown in a separate line.
program hello print *,"Hello, world!" print *,"Yes, I have a number." end program hello
Hello, world! Yes, I have a number.
The program flows from the top to the bottom sequentially. The program never skips any statements, and the order of statements will be kept. If a statement takes time to finish, the program waits for it gets finished, then moves to the next.
program hello print *,"Hello, world!","Hello, guys!" print *,"Yes, I have a number.","This means Pi." end program hello
Hello, world!Hello, guys! Yes, I have a number.This means Pi.
When you put two messages in
program hello print *,"Hello, world!"," ","Hello, guys!" print *,"Yes, I have a number."," ","This means Pi." end program hello
Hello, world! Hello, guys! Yes, I have a number. This means Pi.
Fortran has another output statement,
write. It has the same functionality as
! equivalent statements program hello print *,"Hello, world!" write(*,*) "Hello, world!" end program hello
write statement has a form
write(*,*) followed by the items to be shown. It has two asterisks, the first for the unit number for output (not in
write is used to save the values to a file, and the unit number is a unique number to identify the file. You can use either
write to show the values on the screen. I will use
Stop the program
There is a statement that stops the program immediately. The
stop statement can be used as a single command to do this.
program hello print *,"Hello, world!"," ","Hello, guys!" stop print *,"Yes, I have a number."," ","This means Pi." end program hello
This program prints the first message and stops, and it does never show the second one. The statement optionally accepts character literals which will be shown when the program stops there.
program hello print *,"Hello, world!" stop "Stop here." end program hello
You can write two or more statements in the same line by a semi-colon (
;). Do not abuse this feature because it makes the code hard to read.
! equivalent statements program hello print *,"First"; print *,"Second"; print *,"Third" end program hello
- The program sequentially executes the statements from top to bottom.
- The character literals are shown without spaces if the literals are simply listed in
writestatement has the same functionality as
stopstatement stops the program, and the code after this will not be executed at all.
;separates two statements in the same line.
- Run the above programs and see the results.
Back to index.html.