Fortran has several different types of variables. The two most commonly used types for computations are integer variables and real variables.
Important! For an integer variable, the fractional part is not merely zero. The fractional part does not exist:
The type of each variable should be declared at the beginning of the program, right after the program is named. The type declarations have to appear before the first executable statement. Additionally, your variable names should adhere to the following conventions:
Following is a program that illustrates some of the differences between real and integer variables:
PROGRAM MAIN INTEGER I, INTPI, INTE REAL X, REALPI, REALE I = 1 X = 1 PRINT *, 'THE VALUE OF 1 AS A REAL IS', X PRINT *, 'THE VALUE OF 1 AS AN INTEGER IS', I REALPI = 3.1416 INTPI = REALPI PRINT *, 'THE VALUE OF PI AS A REAL IS', REALPI PRINT *, 'THE VALUE OF PI AS AN INTEGER IS', INTPI REALE = 2.71828 INTE = REALE PRINT *, 'THE VALUE OF E AS A REAL IS', REALE PRINT *, 'THE VALUE OF E AS AN INTEGER IS', INTE STOP END
Notice that the PRINT statement is a little different from the one in the previous exercise. Any text that you want to print out is inside single quotes, but when printing the value of a variable you don't put it in quotes. If you want to print multiple things (whether text or values) separate them with a comma.
Assignment:
(1) Type the program as listed above, compile it and run it.
(2) What happens to the value of pi when it gets converted to an integer?
(3) What happens to the value of e when it gets converted to an integer? Look closely: Is the result rounded to the nearest integer?
(4) Change the values of pi and e to negative numbers (that is, -3.1416 and -2.71828), then re-compile and re-run the program. What happens when the negative values are converted to integers?
Back to main page for programming tutorial