# Get started

February 2020

Back to index.html.

# Get Started

## Hello world

### 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
 Hello, world!

### Rules of Fortran program

The above program is an only 3-line program, but it has the basic rules in Fortran.

1. The code should be separated by a line.
2. The program starts with the keyword program followed by an arbitrary but unique program name (hello in this case).
3. The program ends with end program with the program name.
• All the codes should stay between program and end program.
4. The keyword print shows a message to an output device.
• The symbol * specifies the default output format (leaving the output to the computer).
• After *, the comma is needed.
• The message comes the last, and it should be enclosed with " and ".

### Some details

In this program, program, end program, and print are keywords to make a program structure. A line with such a keyword is called statement. For example, the first line is a program statement, and the second line is a print statement. Each line should have only 1 statement.

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.

The message "Hello, world!" is literally interpreted as characters. Such an object is called literal. The literal characters should be encircled with " (or ', 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.

### Summary

• One line has one statement.
• The program starts with program + a unique name and ends with end program + the name.
• The print statement shows literals to screen.
• A character literal is encircled with " or '.
• One or more spaces separate keywords and statements.
• If you get en error on compiling, read the error message carefully and remove the issue.

### Exercises

1. Replace the message with your favorite one.
2. Confirm the message starts from the second column of the screen.
3. Replace " with ' for the message and confirm it also works.
4. Remove " and ' from the message and confirm the program does not compile.
5. Make sure each of the following changes works.
1. Change the program name.
2. Insert any number of spaces between keywords.
3. Insert any empty lines into the program.

## Programming style

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

The symbol, !, 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.
!!!!!

### Indents

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, program and 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.

### 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

### Line break

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.

### Summary

• 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.

### Exercises

1. Make sure any comments will be ignored in the program.
2. 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.

The print statement accepts 2 or more messages. Use a comma to separate two messages like the following example.

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 print, it merely combines two messages without any spaces. If you need a space between the messages, you have to put it manually.

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.

### Write statement

Fortran has another output statement, write. It has the same functionality as print but it is a bit more redundant.

! equivalent statements
program hello
print *,"Hello, world!"
write(*,*) "Hello, world!"
end program hello

The 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 print) and the second for the format (equivalent to * in print). The 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 print or write to show the values on the screen. I will use print in this tutorial for this purpose.

### 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

### Multiple statements

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

### Summary

• The program sequentially executes the statements from top to bottom.
• The print statement accepts 2 or more literals, which are separated with a comma (,).
• The character literals are shown without spaces if the literals are simply listed in print.
• The write statement has the same functionality as print.
• The stop statement stops the program, and the code after this will not be executed at all.
• Semi-colon ; separates two statements in the same line.

### Exercises

1. Run the above programs and see the results.

Back to index.html.