27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples
27 March, 2000 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
27 March, 2000 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
27 March, 2000 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
27 March, 2000 DO with Counter-Controller : Specifics Calculate initial_value, limit,step_size Set control_variable = initial_value Check control_variable Statement_sequence Increment control_variable by step_size false true Check: control_variable <= limit if step_size positive control_variable >= limit if step_size negative body
27 March, 2000 Output of Example1 CELS FAHR
27 March, 2000 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
27 March, 2000 Output of Example2 CELS FAHR
27 March, 2000 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
27 March, 2000 DO with Counter-Controller : Specifics 2 The initial values of the 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:
27 March, 2000 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 !
27 March, 2000 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 : UsefulLife ! (for sum-of-years'-digits method)
27 March, 2000 Details 1 !Sum : UsefulLife ! (for sum-of-years'-digits method) Sum = 0 DO Year = 1, UsefulLife Sum = Sum + Year END DO
27 March, 2000 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
27 March, 2000 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
27 March, 2000 For Last_M, Last_N equal 3 and 3, output is MNM * N ================
27 March, 2000 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
27 March, 2000 For Last_M equals 3, output is MNM * N ================
27 March, 2000 Paired Words PROGRAM... END IF THEN... END IF DO... END DO