(6) Loops & Introduction to Arrays

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

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

View Full Document Right Arrow Icon
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 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 2009 Do Loops and Arrays 6 statement block, s END DO var is an INTEGER variable called the control variable
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 tt tb l 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 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 va =in it ia lva lue 2009 Do Loops and Arrays 10 1. var = initial value 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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 9

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

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

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