Skip to main content

3.2) Reading User Input From The Keyboard


As an example, look at the following code. On line 4, the program will pause and wait for you to enter a value on the keyboard. Once you enter a value and hit return then the program will proceed to line 5 and then 6. When the program is running and reaches line 5 it will pause and wait for you to enter a value on the keyboard.   When you enter it, the value will be assigned to the variable a.   On line 6, the program prints to the screen the value of a.

1      program example3
2      implicit none
3      ! example of reading from the keyboard
4      real :: a
5      read *, a
6      print *, a
7      end program example3

Note that on line 2 of example 3 I have included a statement called implicit none. When this is written into a program (and it must appear at the top of the program), then you are required to declare all variables that you use. It is a safety measure. If I don’t declare a variable then it will cause an error at the compilation stage (when you use f95 or f77). Implicit none is useful for catching typing errors when you write variables. Say a variable is called aer and later in the code you write in the program that aef instead by mistake. Then when you compile the program it will be identified that this variable has not been declared. Without the implicit none statement it would be assumed you were introducing a new variable. So it is very good practice to include this statement. Implicit none must be placed at the top of the program before the declarations.