12.07.2015 Views

Chapter 4. More Fortran Elements: DO Loops and Input/Output 4.1 ...

Chapter 4. More Fortran Elements: DO Loops and Input/Output 4.1 ...

Chapter 4. More Fortran Elements: DO Loops and Input/Output 4.1 ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Chapter</strong> <strong>4.</strong> <strong>More</strong> <strong>Fortran</strong> <strong>Elements</strong>: <strong>DO</strong> <strong>Loops</strong> <strong>and</strong> <strong>Input</strong>/<strong>Output</strong>1818<strong>4.</strong>1 Iteration: <strong>Fortran</strong> <strong>DO</strong> <strong>Loops</strong>One of the easiest ways to repeat a calculation or series of steps multiple times in a <strong>Fortran</strong>program is to use a construction called a <strong>DO</strong> loop. The basic structure of a <strong>DO</strong> loop is as follows:<strong>DO</strong> xxx I = START, STOP, STEP::calculations to be iterated::xxxCONTINUEEach <strong>DO</strong> loop must begin with a <strong>DO</strong> statement <strong>and</strong> end with a CONTINUE statement. Anynumber of lines of calculations <strong>and</strong> I/O may be included in between. The continue statement musthave a statement number label (xxx in the example above). This statement number must be indexedin the initial <strong>DO</strong> statement.The "I" in the <strong>DO</strong> statement is the counter, which gets incremented on each pass throughthe loop. It can be any integer variable. START <strong>and</strong> STOP in the example above are also integers(either variables or explicit numbers), <strong>and</strong> give the range over which the <strong>DO</strong> loop runs. The counterI starts with the value START <strong>and</strong> continues until the value STOP. Once the loop counter hasexceeded the value STOP, execution of the program will proceed beyond the CONTINUEstatement. At that point the loop counter will have a value of STOP+STEP.Each time through the loop, the counter I is incremented by an amount STEP (STEP is alsoan integer constant or variable). The value of STEP is usually 1, <strong>and</strong> STEP must be given on the<strong>DO</strong> line only if it is a value other than 1. The increment STEP may be any positive or negativeinteger.The following segment of <strong>Fortran</strong> code gives an example of a typical <strong>DO</strong> loop construction.SUM = 0.0<strong>DO</strong> 100 J = 1, 5SUM = SUM + JWRITE(*,*) SUM100 CONTINUENote that no STEP variable is given in the <strong>DO</strong> loop, so the counter J is incremented by one eachtime through the loop. Though not required, it is common practice to indent the lines inside the <strong>DO</strong>loop. Table <strong>4.</strong>1 shows the outcome of each time through the <strong>DO</strong> loop.


<strong>4.</strong>2 Use of Files for <strong>Input</strong> <strong>and</strong> <strong>Output</strong>In addition to reading <strong>and</strong> writing on the screen in <strong>Fortran</strong>, data can be read <strong>and</strong> written to<strong>and</strong> from files. This is particularly useful in order to save the results for later plotting or analysis.To read <strong>and</strong> write data to <strong>and</strong> from files, each file must be assigned a "unit number" that identifiesthe file.To write the results of a calculation to a file instead of to the screen, a WRITE statement isused. In addition, for writing results to a file, the file must be opened before data can be written <strong>and</strong>closed after the writing is complete. Before writing to a file, an OPEN statement is used,2020OPEN(UNIT=xx, FILE='filename', STATUS='unknown')The unit number xx in the example above is associated with the file called "filename". The unitnumber may be any integer value or integer variable (with the exception of 5 <strong>and</strong> 6), but typicallynumbers between 1 <strong>and</strong> 100 are used. The values 5 <strong>and</strong> 6 are reserved for reading from the screen<strong>and</strong> writing to the screen. <strong>More</strong> than one file may be opened at any one time, if data is to be writtento more than one file.The STATUS indicator in the OPEN statement may take three possible values: 'new', 'old',<strong>and</strong> 'unknown'. If the STATUS is 'new', a new file with the name 'filename' is created, <strong>and</strong> if thefile already exists, an error message is generated <strong>and</strong> the program exits. If the STATUS is 'old', it isassumed that the file with the name 'filename' already exists. If it does not exist, an error message isgenerated <strong>and</strong> the program exits. If the STATUS is 'unknown', the program proceeds regardless ofwhether the file 'filename' exists or not. If it does not exist, a new file will be created. If it doesexist, the old file will be overwritten. The use of STATUS= 'unknown' is generally recommendedin most programming situations.To write data to a file that has been OPENed, the WRITE statement has the form:WRITE(xx,*) item1, item2, …where xx denotes the same unit number chosen in the OPEN statement above. This will placewhatever variables are designated in the WRITE statement in the file designated by UNIT xx. Eachseparate WRITE statement will place output on a new line in the file. A blank line can be writtenusing a WRITE statement of the form:WRITE(xx,*)Once all the results have been written to a file, the file must be closed using the statement:CLOSE(UNIT=xx)To read data from a file, the OPEN <strong>and</strong> CLOSE statements also must be used. Data may beread from a file using a READ statement:READ(xx,*) item1, item2, …Each READ statement reads data from a single line of the file. In the case of a READ statement, itis assumed that the file named by the OPEN statement already exists <strong>and</strong> contains some input datato the program. Otherwise, if the file does not exist, the program will attempt to read data from it,


<strong>and</strong> an error message will result. A blank line can be read in a file using a READ statement of theform:2121READ(xx,*)The following example program adds the first 100 integers <strong>and</strong> saves the running total to a file.PROGRAM SUM100IMPLICIT NONEINTEGER SUM, IC Open a file (unit 25) for writing.OPEN(UNIT=25, FILE='sum.dat', STATUS='unknown')C Put a heading on the output.WRITE(25,*) 'Integer', 'Cumulative Sum'C Initialize the sum to zero.SUM = 0C Use a <strong>DO</strong> loop from 1 to 100 to add the first 100C integers. Write out a running total to unit 25.<strong>DO</strong> 100 I = 1, 100SUM = SUM + IWRITE(25,*) I, SUM100 CONTINUEC Close the data file.CLOSE(UNIT=25)STOPEND


<strong>Chapter</strong> 4 Review Problems1. For each example, how many times will the loop be executed?a. <strong>DO</strong> 10 I = 10, 10b. <strong>DO</strong> 10 I = 1, 10, 3c. <strong>DO</strong> 10 I = –2, 2d. <strong>DO</strong> 10 I = 10, 5, –12. After executing the following segment of code, what values will be displayed?INTEGER I, MM = 0<strong>DO</strong> 100 I = 1, 5M = M + 1WRITE(*,*) I, M100 CONTINUE3. Construct a <strong>DO</strong> loop to calculate the value of n!, the factorial. Assume that you are given thevalue of n (a positive integer).<strong>4.</strong> Consider a file containing the following data.1.0 2.22.0 3.93.0 6.1<strong>4.</strong>0 7.85.0 9.56.0 12.1The data consists of six X, Y pairs. Construct a segment of <strong>Fortran</strong> code that employs a <strong>DO</strong>loop to read in the pairs from a file assigned to unit 30.2222

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!