What kind of Loops does Fortran have (For loops, While Loops, Repeat Loops)?
Older Fortrans had a fairly simple DO loop statement. Fortran 90 has enhanced these capabilities with DO WHILE constructs, the WHERE, CYCLE and EXIT statements. WHERE is a standalone loop type structure for use with arrays, and CYCLE and EXIT are statements that are used in conjunction with DO and DO WHILE loops. We'll get to this stuff later.
What is the difference between IF, THEN and DO WHILE statements.
IF THEN combined with GO TO statements will let you do anything you want. The DO WHILE and other DO constructs allow you to loop through certain portions of code many times without ever writing GO TO statements. This makes coding slightly simpler, definitely clearer, and computer science types much happier.
What is the difference between DO, DO WHILE, and IF ( ) GOTO Loops.
In terms of what the computer actually does, there is generally no difference. You can structure all three so that they do the same thing. When properly indented, the DO structures tend to be easier to follow. The DO WHILE structure can produce slightly more compact coding, combining a straight DO with the option for some extra comparison logic. In a vector and/or parallel computer, DO's send strong hints to the compiler that it should be looking for ways to feed a pipeline or spread calculations over multiple processors.
How do I change the DO loop increment to something other than 1?
"DO 100 I=1,10,2" will do the loop for I=1,3,5,7,9.
What happens if a DO WHILE statement doesn't stop running?
Very good question! Sometimes you aren't careful enough with your logical expression in the DO WHILE line, or with other options to exit the loop, and this does happen. On most Unix computers, you will be sitting around half of forever waiting for the program to be killed. If you believe that it has been running too long, hold the Control key and hit the "c". If you don't see an obvious error, turn on dbx, set a breakpoint on the next statement after the DO WHILE, and watch the evolution of variables that contribute to you logical test that keeps the loop running. When you see what is wrong, type "quit" at the dbx prompt, to end the session.
When is it better to use a DO WHILE instead of an IF statement.
Avoid the use of IF statements combined with GOTO to repeatedly loop over the same block of code. This is a job for DO WHILE or simple DO statements. IF statements are for simple decisions to do one thing or another (or another...) then to move on to other code.
I don't understand what the 3 arguments in DO loops are or say. For instance "do 100 i=1,10,2"
Best way to do that is to look at what happens to a sample loop. Try the following:
do 100 i=1,10,2 print *, ' In loop i = ',i 100 continue print *, 'After loop i=', i The printed results will be: In loop i = 2 In loop i = 4 In loop i = 6 In loop i = 8 In loop i = 10 After loop i = 12Your DO statement said to do the loop the first time with i=2, do the next time with i=2+2=4, do the 3rd time with i=4+2=6, and keep doing the statements in the loop with i incremented by 2 until i >10. When i>10, don't do anything in the loop again and move on to the next statement after the end of the loop.
Is it possible to use the values generated in the middle of a do loop?
Yes, look at the example above. The variables y and dfdx are generated in the middle of the loop, but also "used" in the print statement below the loop.
Is it possible to lay out a two dimensional array with the double DO loops:
yes, if you have a Fortran 90 compiler.
DO 10 J=1,4 DO 20 K=1,4 A(J,K)=SUM(T(1:N)**(J+K-2)) 20 CONTINUE 10 CONTINUE
Does it matter if you use DO WHILE or loops like DO 100? Does it make any difference?
You can do any looping task with either form. The difference is in the level of convenience. If you are going to be stepping some INTEGER as the loop proceeds (counting), use the "do 100 i=imin,imax,istep" form. If it is easier to do your conditional tests somewhere in the middle of the loop, go with the do 100 type loops again.
What is a SELECT CASE statement?
It is really just another (often more convenient form) of an IF, THEN, ELSE. For the lecture notes example:
select case (i) default case cond = 0 case (1:5) cond=condUO2(temp) case (7) cond=condZr(temp) end selectYou could also write:
if (i.ge.1.and.i.le.5) then cond=condUO2(temp) else if (i.eq.7) then cond=condZr(temp)
else cond = 0 endif
Can you use <, >, = in your CASE statements?
Yes as part of logical expressions, but the CASE becomes more complex than using IF, THEN, ELSE. For example I can do the following:
select case (i>1) case (.true.) print *, 'i>1' case (.false.) print *, 'i<= 1' end selectIs the DEFAULT CASE widely used in programming? What type of programs?
Fortran 90 is too new for any of its new constructs to be "widely used". However, DEFAULT CASE is a variation of the simple ELSE statement. ELSE is very widely used. IF, THEN, ELSE IF, ELSE Anytime, you have a simple logical decision to do one thing (IF, THEN), or another(ELSE IF), and a totally different thing to do if your simple tests fail (ELSE). One fairly universal use of this logic, is in subroutines for processing input. A given user response is compared to a series of accepted responses and appropriate action is taken in each case. An ELSE (or DEFAULT CASE) takes care of sending an error message when the user provides unrecognized input. You will discover many more ways to use this in Fortran or another language as you continue to program.
How does the compiler know how to use the "i" in "go to (100,200,400,600 ) i". In general the GOTO statement is confusing.
Whenever a "go to" is followed by a set of integers in parentheses, the compiler only knows of one thing to do with the "i". The compiler actually converts this structure to a bunch of machine code IF branch statements. The effect of this is that if "i" has a value 2, then the computer branches to execute the statement with label 200. If "i" has a value 3, then the computer branches to execute the statement with label 400. If "i" has a value greater than the number of integers (labels references) within the parentheses, then the next statement after the "go to (100,200,400,600) i".
All a GOTO does is to change the flow of the program. When a GOTO is reached, it generally tells the CPU to execute an instruction somewhere in memory other than the next location after the GOTO. Look at an example with GOTOs or write one of your own, and watch it execute with dbx, using the "step" command to see each statement as it is executed.
Why are GO TO statements bad, and what can we do to avoid them?
GO TO is bad only to the extent that it makes coding difficult to read or significantly increases the possibility for undetected programming errors. I think they are an important part of the language, but should be used sparingly. Use of DO loops and IF, THEN, ELSE constructs (along with good indentation habits) whenever practical will take care of most of the problem. In extreme cases where you have many nested IF statements, with many lines contained in each THEN/ELSE option set, even good indentation habits will not improve code clarity. Consider breaking code off into subroutines, or go back to GOTO constructs where you can at least trace label numbers with an editor.
Maintained by John Mahaffy : firstname.lastname@example.org