Fortran_Lecture_4

Fortran_Lecture_4 - Fortran Class Notes Lecture 4 Do Loops...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Fortran Class Notes Lecture 4 – “Do” Loops Now it’s time to learn about the “do” command and the looping effect it creates. Remember that programming is useful for performing repetitive operations very quickly? “Do” loops are a major part of that. They are used to make the computer do something many times, with some change(s) each time through. “Do” loops are powerful tools for applications such as processing large arrays, reading/writing tabular information from/to files, and numerically integrating over “differential” time steps. By using variables, a “do” loop can use the same few lines of code to generate vastly different results every time the computer cycles through the loop. You even have the flexibility to choose whether the loop runs a predefined number of times or stops once a certain condition has been met. Let’s look at a simple “do” loop program to get started: program do1 write(*,*) 'Enter an integer to be POWERED UP.' read (*,*) k ! This next write statement inserts a blank line on the screen. ! It's a good trick for making the output look nicer. write(*,*) ' ' do i=1,5 write(*,*) k**i end do write(*,*) ' ' write(*,*) 'Value of i after the "do" loop: ', i stop end This program produces the first five powers of any integer base number. (A bit of syntax: Fortran uses double asterisks to denote exponents.) In this case, we are using “i” as the counting variable, and we’re also using its value in the looped formula. When the computer gets to the “do” command, it sets i = 1 and performs all the commands between “do” and “end do”. Then it goes back to the “do” line, sets i = 2, and checks to see if this new value of “i” is greater than the end value, which is 5 here. As long as the new value of “i” is less than or equal to the end value, the code within the loop will be run again. So, after k^5 is written to the screen, the computer sets i = 6, sees that i is now greater
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/21/2011 for the course AERO 3970 taught by Professor Doucette during the Spring '11 term at Auburn University.

Page1 / 4

Fortran_Lecture_4 - Fortran Class Notes Lecture 4 Do Loops...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online