(6) Loops & Introduction to Arrays

# (6) Loops & Introduction to Arrays - Repetition Over...

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

9/17/2009 1 Over and Over and Over Again Repetition Nathan Friedman Repetition To fully take advantage of the speed of a computer, we must be able to instruct it to do a lot of work 2009 Do Loops and Arrays 2 The program must be relatively short or it would take us too long to write To get the computer to do a lot of work, we must be able tell it to do some computations many times, perhaps with different data values each time A Table of Values Problem: Output a table of numbers from 1 to 100 with their squares and cubes 1 1 1 2 4 8 2009 Do Loops and Arrays 3 3 9 27 4 16 64 . . . We want to be able to repeat a computation over and over for the different numbers without writing 100 WRITE statements A Table of Values INTEGER :: Num 2009 Do Loops and Arrays 4 DO Num = 1, 100 WRITE(*,*) Num, Num*Num, Num*Num*Num END DO Loops that Count The syntax of a definite iterator (often called a counted DO loop) is: DO var = initial, final, step-size t t t bl k 2009 Do Loops and Arrays 5 statement block, s END DO Loops that Count The syntax of a definite iterator (often called a counted DO loop) is: DO var = initial, final, step-size t t t bl k 2009 Do Loops and Arrays 6 statement block, s END DO var is an INTEGER variable called the control variable

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

View Full Document
9/17/2009 2 Loops that Count The syntax of a definite iterator (often called a counted DO loop) is: DO var = initial, final , step-size t t t bl k 2009 Do Loops and Arrays 7 statement block, s END DO var is an INTEGER variable called the control variable initial and final are INTEGER expressions Loops that Count The syntax of a definite iterator (often called a counted DO loop) is: DO var = initial, final, step-size t t t bl k 2009 Do Loops and Arrays 8 statement block, s END DO var is an INTEGER variable called the control variable initial and final are INTEGER expressions step-size is an optional INTEGER expression. If omitted the default value is 1 Semantics of Counted DO Initialization 1. Evaluate the initial, final and step-size expressions. These can be any expressions that give an integer 2009 Do Loops and Arrays 9 These can be any expressions that give an integer value They are evaluated only once before the loop is entered 2. The step-size can be any non-zero integer. The default value if the step size is omitted is 1 Semantics of Counted DO (Counting Up) 3. If the step-size is > 0, the loop counts up 1 var = initial value 2009 Do Loops and Arrays 10 1. 2. If (var <= final value) then 1. Execute the statement block, s 2. var = var + step-size 3. Repeat step 2 3. When var > final value, the loop ends and the statement after the END DO is executed Semantics of Counted DO (Counting Down) 3. If the step-size is < 0, the loop counts down 1 var = initial value 2009 Do Loops and Arrays 11 1. var initial value 2. If (var >= final value) then Execute the statement block, s var = var + step-size (negative) Repeat step 2 3. When var < final value, the loop ends and the statement after the END DO is executed Table of Odd Numbers Output the odd numbers between 1 and 100, their squares and cubes.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern