{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Fortran_Lecture_4

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

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

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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}