From the Keyboard in a Read Statement
Listed-Directed Input: The READ Argument
Listing-directed input is carried out with the Fortran READ statements. The READ statement tin read input values into a prepare of variables from the keyboard.
The READ statement has the following forms:
READ(*,*) var1, var2, ..., varn READ(*,*)
The starting time form starts with READ(*,*), followed past a list of variable names, separated by commas. The calculator volition read values from the keyboard successively and puts the value into the variables. The second form but has READ(*,*), which has a special meaning.
- The post-obit example reads in four values into variables Cistron, N, Multiple and tolerance in this lodge.
INTEGER :: Factor, N Existent :: Multiple, tolerance READ(*,*) Factor, N, Multiple, tolerance
- The post-obit case reads in a string into Title, followed by three existent numbers into Pinnacle, Length and Expanse.
CHARACTER(LEN=10) :: Title Real :: Height, Length, Area READ(*,*) Championship, Elevation, Length, Area
Preparing Input Data:
Preparing input information is elementary. Here are the rules:
- If a READ argument needs some input values, start a new line that contains the input. Make sure the blazon of the input value and the type of the corresponding variable are the same. The input data values must be separated by space or commas.
For the post-obit READ
CHARACTER(LEN=5) :: Name REAL :: tiptop, length INTEGER :: count, MaxLength READ(*,*) Name, height, count, length, MaxLength
"Smith" 100.0 25 123.579 10000
Proper name "Smith" meridian 100.0 count 25 length 123.579 MaxLength 100000
- Input values can be on several lines. As long every bit the number of input values and the number of variables in the corresponding READ agree, the computer volition search for the input values. Thus, the following input should produce the same event. Annotation that fifty-fifty blank lines are allowed in input.
"Smith" 100.0 25 123.579 10000
- The execution of a READ always starts searching for input values with a new input line.
INTEGER :: I, J, One thousand, L, M, N READ(*,*) I, J READ(*,*) 1000, L, M READ(*,*) N
100 200 300 400 500 600
- Consequently, if the number of input values is larger than the number of variables in a READ statement, the extra values volition exist ignored. Consider the post-obit:
INTEGER :: I, J, K, L, K, North READ(*,*) I, J, K READ(*,*) L, One thousand, N
100 200 300 400 500 600 700 800 900
- A express type conversion is possible in a READ statement. If the input value is an integer and the corresponding variable is of Real blazon, the input integer volition exist convert to a real number.
But, if the input value is a existent number and the corresponding variable is of INTEGER type, an error volition occur.
The length of the input string and the length of the corresponding CHARACTER variable do not have to be equal. If they are non equal, truncation or padding with spaces will occur as discussed in the PARAMETER attribute page.
- Finally, a READ without a listing of variables just skips a line of input. Consider the post-obit:
INTEGER :: P, Q, R, Due south READ(*,*) P, Q READ(*,*) READ(*,*) R, S
100 200 300 400 500 600 700 800 900
Source: https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap02/read-1.html
0 Response to "From the Keyboard in a Read Statement"
Postar um comentário