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
The input data may look similar the following:CHARACTER(LEN=5) :: Name REAL :: tiptop, length INTEGER :: count, MaxLength READ(*,*) Name, height, count, length, MaxLength
Notation that all input data are on the same line and separated with spaces. After reading in this line, the contents of the variables are"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.
If the above READ statements are used to read the following input lines,INTEGER :: I, J, One thousand, L, M, N READ(*,*) I, J READ(*,*) 1000, L, M READ(*,*) N
then I, J, K, Fifty, M and N will receive 100, 200, 300, 400, 500 and 600, respectively.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:
If the input lines areINTEGER :: I, J, K, L, K, North READ(*,*) I, J, K READ(*,*) L, One thousand, N
Variables I, J and K receive 100, 200 and 300, respectively. Since the second READ starts with a new line, 50, M and N receive 500, 600 and 700, respectively. 400 on the get-go input line is lost. The next READ will start reading with the third line, picking up 900. Hence, 800 is lost.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:
If the input lines areINTEGER :: P, Q, R, Due south READ(*,*) P, Q READ(*,*) READ(*,*) R, S
The first READ reads 100 and 200 into P and Q and 300 is lost. The 2nd READ starts with a new input line, which is the second one. It does not read in anything. The third READ starts with the third line and reads 700 and 800 into R and South. Equally a upshot, the 3 input values (i.e., 400, 500 and 600) are all lost. The 3rd value on the third line, 900, is too lost.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