Você está na página 1de 18

Lecture 5 1

Example Problem
1. We have force vs. time data that has been obtained from
experimental measurements (
, , 1, 2,...,
i i
F t i N =
). We need to
determine the impulse represented by the force:
F
t
measurement
curve fit
I Fdt =

he data is provided via a Windo!s text file called


"forcetime.dat" and each line of the file contains t and # for
one time point (separated by a comma). he data is provided
Lecture 5 2
se$uentially (!ith time). he number of data points % is
un&no!n, ho!ever the last line in the data file has a ne'ative
value 'iven for the time (as a si'nal indicatin' the end of the
data file and # vs. t measurements).
2. (pproach. We can either inte'rate the data numerically or
curve fit the data and inte'rate the curve fit analytically.
#or a numerical inte'ration, !e could use the trape)oidal
rule to approximate the inte'ral (area under the curve)
bet!een
1
t
and
2
t
:
Lecture 5 *
F
t
1
t
2
t
2
F
1
F
1 2
2
F F
area t
+

t
Lecture 5 +
Repetitive Execution
,n many applications, it is desired to control the execution of the pro'ram
by repeatin' a 'roup of statements in a 'iven !ay. he are t!o basic
types of repetition:
1. Repetition controlled by a counter in !hich the body of the loop is
executed once for each value of some control variable in a specified
range of values.
2. Repetition controlled by a logical expression in !hich the decision to
continue or ton terminate repetition is determined by the value of
lo'ical expression.
Lecture 5 5
Counter-Controlled DO Loops
he counter-controlled ./ loop has the follo!in' form:
./ control-variable = initial-value, limit, step-size
statement se$uence to be repeated
0%. ./
1estrictions:
the $uantities initial-value, limit, step-size are inte'er expressions
step-size must be non-)ero, but can be ne'ative or positive
if step-size is omitted, it is assumed to be 1
he initial-value, limit, step-size of the control-variable are
determined before repetition be'ins and cannot be chan'ed durin'
execution of the ./ loop.
2tatements !ithin a loop may use the value of the control variable,
but they must not modify its value.
Lecture 5 3
When the loop is executed,
1. he control variable is assi'ned the initial value.
2. he control variable is compared !ith the limit to see if it is
less than or e$ual to the limit, for a positive step si)e
'reater than or e$ual to the limit, for a ne'ative step si)e.
*. ,f so, the se$uence of statements, called the body of the loop
(bet!een the ./ and 0%. ./), is executed.
+. he step si)e is added to the control variable, and step 2 is
repeated. /ther!ise, repetition terminates and execution be'ins
!ith the executable statement belo! the 0%. ./ statement.
#or some compilers, after the loop is completed, the value of the control
variable is e$ual to its test value (from step 2). But some compilers leave the
control variable set to the last value for !hich the statement se$uence !as
executed. 4ecause you can never be sure, %0501 use the value of the
control variable outside the loop !ithout settin' it to some value first.
he value of the control variable may be used in subse$uent assi'nment
statements outside the loop, or as a control variable for another loop.
Lecture 5 6
(n example,
./ , 7 1, 18, 2
statement 'roup
0%. ./
he statement 'roup !ill be repeated !ith values of , 7 1, *, 5, 6, and 9.
When the loop is completed, the value of , is 11 (/% /%L: 2/;0
</;=,L012)>>> ./%? </@% /% :/@1 A%/W,%B WC( C0
5(@0 , ,2 0D@(L />>>
(nother example,
./ , 7 -18, 18
statement 'roup
0%. ./
he statement 'roup !ill be repeated !ith values of , 7 -18, -9, -E, F, 9,
and 18. 4ecause the step si)e !as omitted, it is assumed to be one.
Lecture 5 E
(nother example,
2@; 7 8.8 > initiali)e 2@; to )ero
./ , 7 1,18
2@; 7 2@; G , > add , to 2@;
0%. ./
he loop !ill be repeated 18 times. When the loop is completed is, the
value of 2@; is 55.
(nother example,
2@; 7 8.8
./ , 7 1,99
,# (,H5) 2@; 7 2@; G ,
0%. ./
he loop !ill be repeated 99 times. When the loop is completed is, the
value of 2@; is 15.
Lecture 5 9
General DO Loops (controlled by logical test
#ortran 98 provides a ./-0I, construct for a do loop !hich has the
follo!in' 'eneral construct:
./
2tatement-se$uence J1
,# (logical-expression) 0I,
2tatement-se$uence J2
0%. ./
0ither statement se$uence may be omitted.
he statements that ma&e up the body of the loop (bet!een the ./ and
0%. ./ statements) !ill be executed repeatedly until the logical-
expression in the ,# is true.
%ote that there is no counter that is used hereK instead, somethin' must
occur durin' (or even before) the execution of the body of the loop to
cause the logical-expression in the ,# to become true. ,f the lo'ical-
expression is never true, then the loop continues indefinitely (called an
infinite loop) L %/ B//.>>>
Lecture 5 18
0xample:
he previous example !ith an ordinary ./ loop:
2@; 7 8.8
./ , 7 1,18
,# (, H 5) 2@; 7 2@; G ,
0%. ./
With the ./-0I, construct:
2@; 7 8.8 > initiali)e 2@; to )ero
, 7 1
./
2@; 7 2@; G ,
, 7 , G 1
,# (, 7 5) 0I, > or, ,# (,M75) 0I,
0%. ./
(fter the ./-0I, construct is completed, the value of 2@; is 18.
@nli&e the ordinary ./, !e do &no! that the value of , is e$ual to 5
(because it !as not used as a control variable).
Lecture 5 11
./ Loops may be Nnested O !hen needed. #or example,
./ ; 7 1, lastPm
./ % 7 1, lastPn
=roduct 7 ; Q %
=1,% Q , ;, N times O, %, N e$uals O, =roduct
0%. ./
0%. ./
,n the above ; and % are implicitly typed inte'er. he inner loop (%) is
completed once for each value of ;. ,f lastPm is 6 and lastPn is 12, the
=1,% !ill be executed E+ times and !ill produce E+ lines of output.
o easily identify each loop, pro'rammers fre$uently indent each inner
loop and then indent the lines !ithin a loop. ,n hand!ritten form, you
!ill often see pro'rammers put hori)ontal and vertical borders for each
loop (as done above) to help them &eep thin's or'ani)ed.
Lecture 5 12
! Pre-"ortran #$ "orm o% t&e DO LOOP
(nother form of the .o Loop that you !ill see very often that has been
around since #/11(% !as invented has the follo!in' form.
./ statement-number control-variable = initial-value, limit, step-size
statement se$uence to be repeated
statement-number </%,%@0
his form of the ./ !or&s exactly the same !ay as the ordinary ./
discussed above. C0 </%,%@0 statement serves the same function
as the 0%. ./ statement. ,n order to use it, you must assi'n a uni$ue
statement number to the </%,%@0 statement. he statement number
must be an inte'er and:
in free-format compilers (an option in #ortran 98), have one or more
blan& spaces bet!een it and the </%,%@0 statement,
in fixed-format compilers, must be placed any!here in columns 1-5.
Lecture 5 1*
0xample, old #/11(%:
./ 26 , 7 1, 188
2tatements to be repeated
26 </%,%@0
,n #ortran 98, it !ould be !ritten as
./ , 7 1, 188
2tatements to be repeated
0%. ./
Lecture 5 1+
'&e GO 'O (tatement (! Pre-"ortran #$ statement
he B/ / statement !as included !ith the very first #/11(%
compilers but has fallen out of favor !ith snobby #ortran 98 advocates as
bein' a trashy unsophisticated !ay to program. :ou !ill still see it used
a lot.
,ts 'eneral form is
B/ / statement-number
he statement-number is uni$ue inte'er number associated !ith an
executable statement. he statement number must be an inte'er and:
in free-format compilers (an option in #ortran 98), have one or more
blan& spaces bet!een it and the </%,%@0 statement,
in fixed-format compilers, must be placed any!here in columns 1-5.
<onsider the follo!in' example:
Lecture 5 15
<onsider usin' the ./-0I, construct:
2@; 7 8.8
, 7 1
./
2@; 7 2@; G ,
,# (2@; M 188.8) 0I,
, 7 , G 1
0%. ./
he above can also be !ritten !ith a B/ / statement:
, 7 1
2@; 7 8.8
*+5 2@; 7 2@; G ,
,# (2@; M 188.8) B/ / 26
, 7 , G 1
B/ / *+5
26 any executable statement
Lecture 5 13
(nother pre-#ortran 98 statement !as the </%,%@0 statement. ,t is
sort of an executable statement, but its only purpose is to be used !ith a
statement number to mar& a place at !hich to continue pro'ram
execution. he last example could have been !ritten as:
, 7 1
2@; 7 8.
*+5 2@; 7 2@; G ,
,# (2@; M 188.8) B/ / 26
, 7 , G 1
B/ / *+5
26 </%,%@0
or
, 7 1
2@; 7 8.
*+5 </%,%@0
2@; 7 2@; G ,
,# (2@; M 188.8) B/ / 26
, 7 , G 1
B/ / *+5
26 </%,%@0
Lecture 5 16
What is the functional difference in these t!o #ortran 'roups of
statements (in terms of executin' statement 'roup J1)R
=ro'ram 1
./ , 7 -5, 25,6
2tatement 'roup J1 repeated for , 7 -5, 2, 9, 13, 2*
0%. ./
2tatement 'roup J2
=ro'ram 2
, 7 -5
+2 ,# (, H7 25) C0%
2tatement 'roup J1 repeated for , 7 -5, 2, 9, 13, 2*
, 7 , G 6
B/ / +2
0L20
2tatement 'roup J2
Lecture 5 1E
he ans!erR %/C,%B

Você também pode gostar