Loop
body
Label CONTINUE
Example:
DO
10 I = 99, 0,-3
PRINT
*,I
10
CONTINUE
99 = I 的起始值
0 = I
的極限值
-3 = 代表每次執行遞增或遞減(-3)值
SCAN
99
96
.
.
0
共三十四次
如果
DO 10 I = -3 ,10
STEP不寫
自動跳1
DO 100 I
= J+3, 2*JMAX
………
100
CONTINUE
其中100表示編號 可用任意數代替
INITIAL:
J+3
LIMIT:
2*JAMX
STEP :
1
label
= 行號
lcv
= local variable 專作計數用的
C
For each integer in the range 1 to MAXINT ,prints each integer,
C
its square ,square root ,and its exponential functions value.
C
Declarations 宣告
INTEGER
MAXINT
PARAMETER
(MAXINT = 4)
INTEGER
SQUARE
REAL
ROOT ,IPOWER
C
Display a heading
Print
,
‘
I I**2
Square root
Exponential ‘
C
Display each integer ,its square,squareroot,exponential,
DO
20 I = 1,MAXINT
SQUARE= I**2
ROOT=SQRT(REAL(I))
IPOWER = EXT (REAL (I))
PRINT *, I,
SQUARE,
ROOT,
IPOWER
20 CONITINUE
STOP
END
註解
IPOWER 指數函數
注意 REAL 一定要寫(
平方根函數中需為實數!!)
P.157
Case
Study :Physics of Falling
PROBLEM
STATEMENT
Your
skydiving club wants a program that will compute and display the distance a
skydiver falls from a plane at 1sec intervals from time=1 sec until some
predetermined maximum time.
From
physics, we know that the distance an object falls from an airplane (ignoring
air resistance)is given by equation
Distance = (1/2)
acceleration*time^2
主旨: 寫一程式列表每秒從飛機上自由落體下來物體的距離。
REAL
GACCEL
PARAMETER (GACCEL = 9.80665)
INTEGER TIME, MAXTIME ,DISTNC
PRINT *, ‘Enter maximum freefell time (in seconds)’
READ *,MAXTIME
PRINT
*, ‘ Time (secs) Distance(meters)’
DO 10 TIME = 1, MAXTIME
DISTNC = 0.5 *GACCEL*TIME**2
PRINT *, ‘TIME (secs) =’ ,TIME,
+
‘Distance(meters) = ‘ , DISTNC
10 CONTINUE
STOP
END
SCAN
TIME(secs)
DISTANCE(meters)
1
4.903325
2
19.613300
3
44.129920
4
78.453200
5
122.583100
6
176.519700
7
240.262900
8
313.812800
9
397.169300
10
490.332500
Section
4-3 Accumulating a Sum
(累積總和)
等比級數:
1+X+X²+………..
首項=
1
公比=
X
部分和:
以Sum表示
兩項部分和:
1+x
三項部分和;
1+x+x²
各單項
以item 表示
DO
10 I = 1 , 99
ITEM
= ITEM *X
SUM=SUM+ITEM
10
CONTINUE
作業提示
Ex.
1+x+x²+……..
if x=0.9
1/1-0.9=1/0.1=10
誤差需小於0.1% 10*0.1%=0.01
以下
試問10-0.01=9.99
要第幾項才能9.99
p.169
DO LOOP 例題
Trace the execution of
following Do loop for the data 5,
3, -5,
7,
0 and -9.
What is read into N?
What values are read into NEXT ?
What are printed?
read
*,N
sum=0
DO
30 I = 1,N
READ *, NEXT
IF (NEXT
.GE. 0) THEN
SUM=SUM+NEXT
ELSE
SUM=SUM-NEXT
END IF
30
CONTINUE
PRINT
*,SUM
流程:
N
NEXT
5 3
第一次
NEXT
SUM
3
第二次
NEXT
SUM
-5
3-(-5)=8
第三次
NEXT SUM
7
15
第四次
NEXT SUM
0
15
第五次
NEXT
SUM,
-9
15-(-9)=24
Section
4-7 NESTED
CONTROL STRUCTURE
DO LOOP
可以內括DO LOOP 及IF
DO
10 I =1,99
DO 77 J=2,13
……
…….
77
CONTINUE
10
CONTINUE