Structuring Programs for Languages and Applications - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Structuring Programs for Languages and Applications

Description:

Incrementing (counting) COUNTER = COUNTER 1. or C = C 1 ... Increments or decrements a variable each time loop is repeated ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 24
Provided by: facult7
Category:

less

Transcript and Presenter's Notes

Title: Structuring Programs for Languages and Applications


1
Structuring Programs for Languages and
Applications
  • Chapter 7
  • Problem Solving with Loops

2
The Loop Logic Structure
  • 3rd logic structure
  • Repeating structure
  • WHILE/WHILE-END loop
  • Repeats while condition is true
  • Stops when condition is false
  • REPEAT/UNTIL loop
  • Repeats while condition is false
  • Stops when condition is true
  • Automatic-counter loop

3
Incrementing (counting)
  • COUNTER COUNTER 1
  • or C C 1
  • Used to count number of items as part of solution
    to problem

4
Accumulating (summing)
  • SUM SUM VARIABLE
  • or S S V
  • Sum Variable must be initialized to 0 before
    starting loop
  • TOTALSALES TOTALSALES SALES

5
WHILE/WHILE-END
  • Repeat all instructions between WHILE and
    WHILE-END until condition is FALSE
  • WHILEltCONDITION(S)gt
  • INSTRUCTION
  • INSTRUCTION
  • .
  • .
  • WHILE-END
  • Brackets and indention to improve readability

6
WHILE/WHILE-END Flowchart
  • WHILE symbol diamond (decision)
  • Circles on page connectors are used-only part
    of a flowchart

7
Example
  • Page 127
  • Create algorithm and flowchart to find average
    age of all students in a class.

8
REPEAT/UNTIL
  • Repeat instructions between REPEAT and UNTIL
    until condition is TRUE opposite of
    WHILE/WHILE-END
  • REPEAT
  • INSTRUCTION
  • INSTRUCTION
  • .
  • .
  • UNTIL ltCONDITION(s)gt

9
REPEAT/UNTIL flowchart
  • Use brackets and indentation
  • Place repeat on flowchart

10
WHILE/WHILE-END vs REPEAT/UNTIL
  • Do not know of times instructions will be
    repeated
  • Instructions in loop dont want processed
  • Examples
  • Entering info on clients -dont know of clients
  • Total of sales-dont know how many sales
  • Calculating test averages
  • Do not know of times instruction will be
    executed
  • Instructions WILL BE executed at least once
  • Dont know condition for repeating until after
    instructions are processed

11
Example
  • Page 131
  • Same as previous example average age of
    students in class

12
Automatic-Counter
  • Increments or decrements a variable each time
    loop is repeated
  • Variable as counter to start counting at
    specified - increments each time loop is
    processed
  • Loop repeats until counter is greater than ending
    number

13
Automatic-counter
  • Beginning value, ending value, increment value
  • Constants
  • Variables
  • Expressions (calculations)
  • Test to continue depending on programming
    language
  • Beginning
  • end

14
Automatic-counter algorithm flowchart
  • LOOPCOUNTERBEGIN TO END STEP S
  • INSTRUCTION
  • INSTRUCTION
  • .
  • .
  • LOOP-END COUNTER

To decrement STEP is negative BEGIN must be gt
than END
15
Automatic-counter-RULES
  • Executes LOOP instruction counterbeginning
    number
  • Executes LOOP-END increments counter
  • Counter lt END number processing continues in
    loop
  • Counter gt END number processing outside loop

16
Example
17
Nested Loops
  • Same rules (brackets, indentation, etc.)
  • Inner loop doesnt have to be same as outer loop

18
Indicators
  • Logical variables to change processing path or
    control when loop ends
  • Flags, switches, trip values
  • User does not see!

19
Algorithm instructions flowchart symbols page
139-140
  • Decision Logic Structure
  • Instructions Incrementing Accumulating
  • Loop Logic Structures
  • IF/THEN/ELSE
  • COUNTERCOUNTER 1
  • SUMSUM VARIABLE
  • WHILE/WHILE-END
  • REPEAT/UNTIL
  • LOOPCOUNTER-BEGIN TO END STEP S
  • LOOP-END COUNTER

20
Recursion
  • A module or function calls itself
  • Condition to end MUST BE in module
  • Recursive procedures faster than conventional
    loop structures

21
Recursion
Factorial number 5!(5!54321) Until N1 to
end Page 143
22
Homework Assignment
  • Questions 1-4 (p 141)
  • Problems 1-5 (these will be flowcharts)

23
End of chapter 5
Write a Comment
User Comments (0)
About PowerShow.com