Presentation is loading. Please wait.

Presentation is loading. Please wait.

27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples.

Similar presentations


Presentation on theme: "27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples."— Presentation transcript:

1 27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples

2 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

3 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

4 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

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

6 27 March, 2000 Output of Example1 CELS FAHR 032 541 1050 1559 2068. 100212

7 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

8 27 March, 2000 Output of Example2 CELS FAHR 100212 95203 90194. 1050 541 032

9 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

10 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: 151 241 331 421 511

11 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 !

12 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 : 1 + 2 +... + UsefulLife ! (for sum-of-years'-digits method)

13 27 March, 2000 Details 1 !Sum : 1 + 2 +... + UsefulLife ! (for sum-of-years'-digits method) Sum = 0 DO Year = 1, UsefulLife Sum = Sum + Year END DO

14 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

15 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

16 27 March, 2000 For Last_M, Last_N equal 3 and 3, output is MNM * N ================ 11 1 12 2 13 3 21 2 22 4 23 6 31 3 32 6 33 9

17 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

18 27 March, 2000 For Last_M equals 3, output is MNM * N ================ 11 1 21 2 22 4 31 3 32 6 33 9

19 27 March, 2000 Paired Words PROGRAM... END IF THEN... END IF DO... END DO


Download ppt "27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples."

Similar presentations


Ads by Google