Download presentation
Presentation is loading. Please wait.
Published byJanel Harvey Modified over 8 years ago
1
CSCE 206 1 Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones
2
CSCE 206 2 General Form of Do Loops DO j = begin_value,end_value,increment PRINT *,j END DO Variable j runs from begin_value to end_value counting by increment
3
CSCE 206 3 Rules The index variables ( j, begin_value, end_value, increment ) must all be INTEGER variables Any variable names are legal, but things like i,j,k, are common since mathematical summations use them… You absolutely may not change the value of the index variables (for example j ) inside the loop DO j = 1,100 j = j+1 PRINT *,j END DO is highly illegal
4
CSCE 206 4 Do Loops Without Index Variable We did these already DO READ *, a IF(a.EQ. Sentinel) THEN EXIT END IF rest of loop END DO
5
CSCE 206 5 EXIT The EXIT is always legal. Consider loan schedules. DO month = 1,number_of_months compute principal, interest, etc. IF(balance.LE. 0.0) THEN EXIT END IF rest of loop END DO Outer loop is the default, but inner test allows for early payment.
6
CSCE 206 6 CYCLE The CYCLE statement cancels execution of the current loop and causes execution to start up again with the next loop iteration
7
CSCE 206 7 CYCLE DO READ *,a,b,c radicand = b**2 – 4.0*a*c IF(radicand.LE. 0.0) THEN CYCLE END IF more computation END DO (A different way to accomplish the same purpose as the good_data variable, provided that you really are done with the loop processing at this point.)
8
CSCE 206 8 Labels on Do Loops Any loop longer than a few lines should be labelled DO j = 1,100 ! Loop on j from 1 to 100 PRINT *,j END DO ! Loop on j from 1 to 100 j_loop: DO j = 1,100 PRINT *,j END DO j_loop
9
CSCE 206 9 Labels on Do Loops Either form of labelling is ok. Using the official label lets the compiler help you (it will make sure that your loop nesting is correct). Using your own comments allows more text and allows for you to describe the meaning of the loop better. DO j = 1,100 !Outer loop on heat values PRINT *,j END DO ! Outer loop on heat values
10
CSCE 206 10 Next Programming Assignment Due Wednesday 18 February 2004 This is problem 17 page 251 of text.
11
CSCE 206 11 The End
12
CSCE 206 12 Formatted Input and Output We will NOT cover all of this chapter, at least not at this time. This is a very detailed subject. There’s a lot of information there that you may someday want to know, but it’s not relevant now.
13
CSCE 206 13 Formatted Output Fortran has lots (too many) ways to print data. PRINT *,list of variables prints in “free format”. That is, the compiler does its best to have the data printed to the maximum precision possible. But then things don’t get lined up nicely in tables, and you may get extra digits that you know are wrong.
14
CSCE 206 14 Formatted Output—Table, page 258 Aw – print character data using w spaces Iw – print integer data using w spaces Fw.d – print real data using w spaces and d decimals Ew.d - print real data using w spaces and d decimals in scientific notation nX – skip over n spaces Also tabs, pagination, etc. Also other formats for binary, hex, and more complicated things.
15
CSCE 206 15 Formatted Output -- Usage PRINT 101,list of variables WRITE(6,101) list of variables PRINT ‘format string’,list of variables WRITE(6,’format string’) list of vars 101 FORMAT(format string) These all accomplish the same purpose. There are good points and bad points to each method. (This is the part where Fortran has too many options.)
16
CSCE 206 16 Formatted Output -- Usage WRITE(6,101) “header”,counter,x_value 101 FORMAT(A12,I6,F10.4) Print the character string “ header ” in a 12-space field; left justified or right justified? Print the INTEGER variable counter in a 6-space field, right justified. Print the REAL variable x_value in a 10-space field with four digits to the right of the decimal point.
17
CSCE 206 17 Formatted Output -- Rules WRITE(6,101) “header”,counter,x_value 101 FORMAT(A12,I6,F10.4) Char strings too long for the format are truncated INTEGER variables printed in integer format that are too large for the width result in asterisks. REAL variables too large for field width result in stars. For floating point: F format lines up decimal points, but E format prints anything. Choose between nice tables and getting more information. (E.g., an effective zero in F gets printed as zero, but in E will be something line 0.1234567E-07)
18
CSCE 206 18 Formatted Output -- Rules You don’t get a free space between the values. I format requires space for the minus sign, if your numbers are going to wind up negative and needing a minus sign. F requires at least one space for the decimal point, one for the minus sign, and spaces to the left of the decimal point. (w >= d+3) E requires at least one for the leading zero, one for the decimal point, one for the ‘E’, one for the sign on the value, one for the sign on the exponent, and two for the exponent. (w >= d+7) If the number doesn’t fit, you get stars and no info.
19
CSCE 206 19 The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.