190 likes | 279 Views
CS1001 Lecture 9. Lab 2 Complete Lecture 8 DO Loops Counter-controlled DO loop Depreciation example -- details Nested loops Examples. Repetitive Execution. Repetition controlled by a counter DO control_variable = initial_value, limit, step_size statements to be executed END DO
E N D
CS1001 Lecture 9 • Lab 2 • Complete Lecture 8 • DO Loops • Counter-controlled DO loop • Depreciation example -- details • Nested loops • Examples
Repetitive Execution • Repetition controlled by a counter DO control_variable = initial_value, limit, step_size statements to be executed END DO • Repetition controlled by logical expression DO statements to be executed IF (logical_expression) EXIT statements to be executed END DO
Repetition With Counter • Control_variable is the variable to be used by the loop • initial_value is the starting point • limit is the end point • step_size has a default of 1 if not specified; must be non-zero. • Initial_value, limit, and step_size can be integer or integer expressions
DO Loop Example1 ! This program will print out a ! table of temperature conversions ! for every 5 degrees INTEGER :: iCels, iFahr PRINT *, “CELS FAHR” DO iCels = 0, 100, 5 iFahr = (9 * iCels / 5) + 32 PRINT *, iCels, iFahr END DO Control_variable is iCels initial_value is 0 limit is 100 step_size is 5
DO with Counter-Controller : Specifics Calculate initial_value, limit ,step_size Set control_variable = initial_value Check: control_variable <= limit if step_size positive control_variable >= limit if step_size negative true Check control_variable body false Statement_sequence Increment control_variable by step_size
Output of Example1 CELS FAHR 0 32 5 41 10 50 15 59 20 68 . . . 100 212
DO Loop Example2 ! This program will print out a ! table of temperature conversions ! for every 5 degrees INTEGER :: iCels, iFahr PRINT *, “CELS FAHR” DO iCels = 100, 0, -5 iFahr = (9 * iCels / 5) + 32 PRINT *, iCels, iFahr END DO step_size < 0
Output of Example2 CELS FAHR 100 212 95 203 90 194 . . . 10 50 5 41 0 32
DO with Counter-Controller : Specifics 1 • If check control is true the first time, the body is not executed DO number = 1,0,1 statements END DO initial_value=1, limit=0, step_size=1 first time around: control_variable number =1 check control_variable: control_variable >=limit so loop is not executed
DO with Counter-Controller : Specifics 2 • The initial values ofthe control_variable, initial_value, limit, step_size are determined before the DO repetition begins and cannot be changed in the body. K=5 istep=1 DO I = 1,K,istep PRINT *, I, K, istep K=K-1 END DO Output: 1 5 1 2 4 1 3 3 1 4 2 1 5 1 1
DO with Counter-Controller : Specifics 3 • One can use the control_variable in the body but can not modify the value K=5 istep=1 DO I = 1,K,istep PRINT *, I, K, istep K=K-1 I=I+1 END DO ERROR !
Calculating Depreciation Example PROGRAM Depreciation_Table !---------------------------------------------------- ! Program to calculate and display a depreciation table ! using one of two methods of depreciation: ! (1) straight-line ! (2) sum-of-the-years'-digits ! Variables used are: ! Price : purchase price of item ! SalvageValue : and its salvage value ! Amount : amount to be depreciated ! (Price - SalvageValue) ! UsefulLife : its useful life ! Method : method of depreciation (1 or 2) ! Depreciation : amount of depreciation ! Year : number of year in depreciation table ! Sum : 1 + 2 + ... + UsefulLife ! (for sum-of-years'-digits method)
Details 1 !Sum : 1 + 2 + ... + UsefulLife ! (for sum-of-years'-digits method) Sum = 0 DO Year = 1, UsefulLife Sum = Sum + Year END DO
Details 2 ! Generate the depreciation table PRINT * PRINT *, "Year Depreciation" PRINT *, "===================" DO Year = 1, UsefulLife IF (Method == 2) THEN Depreciation = (UsefulLife - Year + 1) & * Amount / REAL(Sum) END IF PRINT *, " ", Year, Depreciation END DO
Nested DO Loops • The body of a DO loop may contain other DO loop(s). • For each iteration of the outer loop, the inner loop control variable goes through its range of values DO M = 1, Last_M DO N = 1, Last_N Product = M * N PRINT *, M, " ", N, " ", Product END DO END DO
For Last_M, Last_N equal 3 and 3, output is M N M * N ================ 1 1 1 1 2 2 1 3 3 2 1 2 2 2 4 2 3 6 3 1 3 3 2 6 3 3 9
What would be the output of these nested loops ? DO M = 1, Last_M DO N = 1, M Product = M * N PRINT *, M, " ", N, " ", Product END DO END DO
For Last_M equals 3, output is M N M * N ================ 1 1 1 2 1 2 2 2 4 3 1 3 3 2 6 3 3 9
Paired Words • PROGRAM ... END • IF THEN ... END IF • DO ... END DO