View
216
Download
0
Embed Size (px)
Citation preview
Outline of control structures Sequential Decision (selection)
A. Logical IF B. Block IF
1. Single alternative IF2. Double alternative IF3. Multiple alternative IF
Loops (repetition)
Types of loop
A. Interdetminate 1. Entrance controlled
WHILE loops
2. Exit controlledREPEAT…UNTIL
B. Determinate 1. Counter controlled
DO loops
Indeterminate loopsAn indeterminate loop is one that
repeats the statements in it’s body some undetermined number of times.
The number of repetitions is not set before the the loop begins.
These loops are controlled by a logical expression while num > 0
Example program session
This program adds up positive numbersA negative number will stop it.Enter a number 5Enter a number7Enter a number23Enter a number-5The sum is: 35
Sentinel value(used only to signalthat the loop shouldend. Not valid data!
WHILE LoopFlowchart
Prompt for num
Read num
num>= 0
Add num to sum
true false
Prompt for num
Read num
Algorithm
1. Declare variables2. Set sum to 03. Prompt for num4. Read num5. Do while num > 05.1 add num to sum5.2 prompt for next num5.3 read num6. Print sum7. End
Trace
num sum1. Declare variables ? ?2. Set sum to 0 04. Read num 55. Do while num > 05.1 add num to sum 55.3 read num 75.1 add num to sum 125.3 read num 235.1 add num to sum 355.3 read num -5 6. Print sum 357. End
Sentinel value
Example f77 program
INTEGER num, sumsum = 0PRINT*, “This program adds up positive numbers”PRINT*, “A negative number will stop it.”PRINT*, “Enter a number”READ*, numDO WHILE (num .gt. 0)
sum = sum + numPRINT*, “Enter a number”READ*, num
ENDDOPRINT*, The sum is: “, sumEND
1. Declare variables2. Set sum to 03. Prompt for num
4. Read num5. Do while num > 05.1 add num to sum5.2 prompt for next num5.3 read num
6. Print sum7. End
Example sentinel values
DO WHILE (num .ne. -999)DO WHILE (num .gt. 0)DO WHILE (num .lt. 0)DO WHILE (name .ne. ‘Xerxys’)DO WHILE ((num .gt. 0) .and. (num .lt.
100))
F90 Example programWHILE structure
INTEGER num, sumsum = 0PRINT*, “This program adds up positive numbers”PRINT*, “A negative number will stop it.”PRINT*, “Enter a number”READ*, numDO
if (num .le. 0) EXITsum = sum + numPRINT*, “Enter a number”READ*, num
ENDDOPRINT*, The sum is: “, sumEND
1. Declare variables2. Set sum to 03. Prompt for num
4. Read num5. Do while num > 0
5.1 add num to sum5.2 prompt for next num5.3 read num
6. Print sum7. End
REPEAT loopFlowchart
Prompt for num
Read num
num< 0
Add num to sum
true false
Prompt for num
Read num
Algorithm: repeat…until
1. Declare variables2. Set sum to 03. Prompt for num4. Read num5. Repeat until num < 05.1 add num to sum5.2 prompt for next num5.3 read num6. Print sum7. End
Trace
num sum1. Declare variables ? ?2. Set sum to 0 04. Read num 55. Repeat until num < 05.1 add num to sum 55.3 read num 75.1 add num to sum 125.3 read num 235.1 add num to sum 355.3 read num -56. Print sum 357. End
Sentinel value
F90 Example programREPEAT…UNTIL structure
INTEGER num, sumsum = 0PRINT*, “This program adds up positive numbers”PRINT*, “A negative number will stop it.”PRINT*, “Enter a number”READ*, numDO
sum = sum + numPRINT*, “Enter a number”READ*, numif (num .le. 0) EXIT
ENDDOPRINT*, The sum is: “, sumEND
1. Declare variables2. Set sum to 03. Prompt for num
4. Read num5. Repeat until num < 05.1 add num to sum5.2 prompt for next num5.3 read num
6. Print sum7. End
While vs. repeat loopsWhile loops can be written to accomplish
everything that a repeat can do.Repeat loops ALWAYS process the first
data item!There are few instances when this is
appropriate. Usually you wish to check the data first.
Example of repeat is program to read characters until EOF is encountered.
Determinate loopsA determinate loop is one that
repeats the statements in it’s body a designated number of times.
This loop uses a counter to keep track of how many times the body of the loop has repeated itself
We call a repetition of the body of a loop an ‘iteration’
Example program session
This program adds up positive numbersHow many numbers will you enter? 3Enter a number5Enter a number7Enter a number23The sum is: 35
Algorithm
1. Declare variables2. Set sum to 03. Prompt for n4. Read n5. Do n times5.1 prompt for num5.2 read num5.3 add num to the sum6. Print sum7. End
Tracenum sum i n
1. Declare variables ? ? ? ?2. Set sum to 0 04. Read n 35. Do n times 15.2 read num 55.3 add num to the sum 55. 25.2 read num 75.3 add num to sum 125. 35.2 read num 235.3 add num to sum 356. Print sum 357. End
Algorithm
1. Declare variables2. Set sum to 03. Prompt for n4. Read n5. Do n times5.1 prompt for num5.2 read num5.3 add num to the sum
6. Print sum7. End
INTEGER n, sum, num, i sum = 0 PRINT*, “How many numbers will you enter?” READ*, n DO 10 i = 1, n PRINT*, “Enter a number” READ*, num sum = sum + num10 CONTINUE PRINT*, “The sum is”, sum END
Comparing f77 to f90
INTEGER n, sum, num, i sum = 0 PRINT*, “How many numbers?” READ*, n DO 10 i = 1, n PRINT*, “Enter a number” READ*, num sum = sum + num10 CONTINUE PRINT*, “The sum is”, sum END
INTEGER :: n, sum, num, i sum = 0 PRINT*, “How many numbers?” READ*, n DO i = 1, n PRINT*, “Enter a number” READ*, num sum = sum + num END DO PRINT*, “The sum is”, sum END
General form of f77 DO loop
DO K INDEX = INT, LIMIT, INCR
label at end of loop
index value(loopcontrolvariable)
test value
increment
initial value
DO 20 i = 1, 10, 2
Loop control variablesTraditionally, loop control variables
are named i, j, k, l, m or n.Implicit data typing automatically
makes these variables type integer.Implicit typing means that the
variables do not have to be declared.All variables should be declared.To turn off implicit typing in f90, use
IMPLICIT NONE
Examples
DO 20 i = 1, 10
DO 20 i = 1, 10, 2
DO 20 i = -1, -10, -2
DO 20 i = 1, -10
10 iterations, increments by 1
5 iterations, increments by 2
5 iterations, decrements by -2
0 iterations, increments by 1
Tables
A common use of DO loops is to produce tabular output.
Each iteration produces one line in the table.
Converting Celsius to Fahrenheitc conversion of Celsius to Fahrenheit temperaturec INTEGER cbegin, climit, cstep PARAMETER (cbegin = 20, climit -20, cstep = -5) INTEGER celsius REAL fahrencc Print the table heading PRINT*, ‘ Celsius Fahrenheit’cc Print table DO 10 celsius = cbegin, climit, cstep fahren = 1.8 * celsius + 32.0 PRINT*, celsius, ‘ ‘, fahren10 CONTINUE END
Celsius to Fahrenheit program output
Celsius Fahrenheit 20 68.00000 15 59.00000 10 50.00000 5 41.00000 0 32.00000 -5 23.00000 -10 14.00000 -15 5.00000 -20 -4.00000
Example: table of squares
c print a list of integers and their squaresc INTEGER I, square, maxicc Read upper bound PRINT*, ‘Enter the maximum number’ READ*, maxicc print table of squares PRINT*, ‘ I I*I’ DO 20 i = 1, maxi square = i * i PRINT*, i, square20 CONTINUE END
Accumulators
Are used to compile sums or count events
Must be initialized before the loop begins
Example adding up the sum of numbers make sure the sum is set to 0 first accumulate the sum in the loop
Example: accumulating
c Summation of integersc INTEGER num, i, sum
cc Add up the sum sum = 0 DO 15 i = 1, 10
READ*, num sum = sum + num15 CONTINUE PRINT*, ‘The sum is ‘, sum END
Example: computing the sum of odd integersc Summation of odd integersc INTEGER n, odd, sumcc Read upper bound PRINT*, ‘This program adds up the sum of odd integers’ PRINT*, ‘between 1 and the next number you enter.’ PRINT*, ‘Enter the number’ READ*, ncc Add up the sum sum = 0 DO 10 odd = 1, n, 2 sum = sum + odd10 CONTINUE PRINT*, ‘The sum is ‘, sum END
Counters
Are used to count event occurancesMust be initialized before the loop
begins set counter to 0
Example accumulating each instance of an event
Example: counting
c Counting even integersc INTEGER num, i, count
cc counting loop count = 0 DO 15 i = 1, 10
READ*, num IF (MOD(num,2) .eq. 0) count = count + 115 CONTINUE PRINT*, ‘There were ‘, count, ‘even numbers’ END
DO loops in reverse
DO loops allow you to increment or decrement the loop control variable during each pass.
Converting Celsius to Fahrenheitc conversion of Celsius to Fahrenheit temperaturec INTEGER cbegin, climit, cstep PARAMETER (cbegin = -20, climit 20, cstep = 5) INTEGER celsius REAL fahrencc Print the table heading PRINT*, ‘ Celsius Fahrenheit’cc Print table DO 10 celsius = cbegin, climit, cstep fahren = 1.8 * celsius + 32.0 PRINT*, celsius, ‘ ‘, fahren10 CONTINUE END
Celsius to Fahrenheit program output
Celsius Fahrenheit -20 -4.00000 -15 5.00000 -10 14.00000 -5 23.00000 0 32.00000 5 41.00000 10 50.00000 15 59.00000 20 68.00000
Converting DO to WHILE
INTEGER n, sum, num, i sum = 0 PRINT*, “How many numbers?” READ*, n DO 10 i = 1, n PRINT*, “Enter a number” READ*, num sum = sum + num10 CONTINUE PRINT*, “The sum is”, sum END
INTEGER n, sum, num, i sum = 0 PRINT*, “How many numbers?” READ*, n i = 1 DO WHILE (i .le. n) PRINT*, “Enter a number” READ*, num sum = sum + num i = i + 1 END DO PRINT*, “The sum is”, sum END
WHILE loops vs. DO loops
The type of loop you use depends on the nature of the tasks to be performed.
Check your algorithm for the the correct description of the process that is involved.
Rules of thumb are given on the next slide
Rules of thumbIf your loop asks for data, and the
number of data items entered by the user may vary each time, use a WHILE loop
If the program knows exactly how many times the loop should execute, use a DO loop
If you intend on asking the user how many times the loop should execute, ask the user for this number only if it is reasonablly small.
A data entry problem
Employee Time Card
Name: Joe SchmoID: 12345Hours: 45Rate: $7.50
You wish to write a program to help a person enter employeetime card data. What approach should you take?
Approach #1
How many time cards do you have?35Enter employee id, hours and rate.12345, 45, 7.50Enter employee id, hours and rate....(this continues 35 times)
Approach #2
Enter employee id, hours and rate. (to end enter negative values)12345, 45, 7.50Enter employee id, hours and rate....(this continues until sentinel values entered)...Enter employee id, hours and rate.-99999,-99,-9.99
A dehumanizing DO loop
Employee Time Card
Name: Joe SchmoID: 12345Hours: 45Rate: $7.50
If you undercount by 1, the program continues after all data hasbeen entered. If you overcount by 1 the program ends prematurely.It forces the user to become a slave to the process.
General advice
Asking a user to perform a tedious, repetitive task with high error potential before they can run your program is bad program design.
The reason we have computers is to handle tedious, repetitive tasks and reduce errors.
Always make the program as simple as possible for the user.
Nested loops
INTEGER stores, depts, sales, amount, i, j stores = 2 depts = 3 DO 10 i = 1, stores sales = 0 DO 20 j = 1, depts PRINT*, “Enter dept “, j, “sales for store “, i READ*, amount sales = sales + amount20 CONTINUE PRINT*, “Total sales for store “, i, “are $”, sales10 CONTINUE END
Program trace i j amount sales stores deptsset stores to 5 2set depts to 3 3do 10 i = 1, stores 1 sales = 0 0 do 20 j = 1, depts 1 read amount 10000.00 add to sales 10000.00 do (j) 2 read amount 25000.00 add to sales 35000.00 do (j) 3 read amount 20000.00 add to sales 55000.00print sales 55000.00do (i) 2 sales = 0 0 do 20 j = 1, depts 1 read amount 35000.00 add to sales 35000.00 do (j) 2 read amount 15000.00 add to sales 50000.00