View
223
Download
3
Embed Size (px)
Citation preview
CHAPTER 4 REPETITIVE EXECUTION
Counter-Controlled DO Loops
DO Number = 1, 9 PRINT *, Number, Number**2 END DO
DO Number = 9, 1, -1PRINT *, Number, Number**2
END DO
Example: Summation
INTEGER :: Number, I, SumREAD *, NumberDO I = 1, Number
Sum = Sum + IEND DO
Example: A Multiplication Table
DO M = 1, Last_MDO N = 1, Last_N
Product = M * NPRINT *, M, “ “, N, “ “, Produ
ctEND DO
END DO
DEPRECIATION (折舊 ) TABLES Depreciation is the decrease in value over time of so
me asset due to wear and tear, declining price, and so on. For example, suppose that a company purchases a new robot for $20,000 that will be used on its assembly line for 5 years. After that time, called the useful life of the robot, it can be sold at an estimated price of $5,000, which is the robot’s salvage value. Thus, the value of the robot will have depreciated $15,000 over the 5-year period. A program is needed to calculate depreciation tables that display the depreciation in each year of the item’s useful life.
A Robotic Arm
DEPRECIATION TABLES
Method = 1 (The straight-line method)Depreciation = Amount / UsefulLife
Method = 2 (The sum-of-the-years’-digits method)Sum = 1 + 2 + … + UsefulLifeDepreciation = (UsefulLife – Year + 1) * Amount / Sum
General DO Loops: DO-EXIT Construct
DOIF (Sum > Limit) EXITNumber = Number + 1Sum = Sum + Number
END DO
Pretest Loop: “test-at-the-top” loop
DOIF (logical-expression) EXITstatement-sequence
END DO
Posttest Loop: “test-at-the-bottom” loop
DOstatement-sequenceIF (logical-expression) EXIT
END DO
DO Loop: Other Type
“Test-in-the-middle” loopQuery-controlled input loops
Query (疑問 ); Control (控制 )
CYCLE Statement
DO (p. 214, Textbook)
READ *, Celsius IF (Celsius < 0.0) PRINT *, “** Temperature must be 0 or above **” CYCLE
END IF (p. 214, Textbook) READ * Response IF (Response == “N”) EXITEND DO
Named DO Constructs
Outer: DO M = 1, Last_MInner: DO N = 1, Last_N
Product = M * NPRINT *, M, “ “, N, “ “, Pro
ductEND DO Inner
END DO Outer
Mean Time to Failure (故障平均時間 )
One important statistic (統計 ) used in measuring the reliability (可靠度 ) of a component in a circuit is the mean time to failure, which can be used to predict the circuit’s life time. This is especially important in situation in which repair is difficult or even impossible, such as a computer circuit in a space satellite.
Mean Time to Failure
Suppose that NASA (National Aeronautics and Space Administration) has awarded an engineering laboratory a contract (合約 ) to evaluate the reliability of particular component for a future space probe (探測 ) to Mars (火星 ). As part of this evaluation, an engineer at this laboratory has test of these circuits and recorded the time at which each failed; now she would like a program to process this data and determine the mean time to failure.
Space Probe (太空探測 )