Download presentation
Presentation is loading. Please wait.
Published byAshley Green Modified over 9 years ago
1
Program variables Counters Accumulators Major and minor totals Control Click icon to hear comments (the download may take a minute )
2
Counters vs. Accumulators Counters usually change by a constant amount, such as +1 Counters can count down by subtracting Counters are almost always integers Accumulators usually increase by varying amounts, such as the amount of a sale Unlikely to ever subtract, but may be set to zero Data type often allows decimal (single or double precision)
3
Syntax Counter – studentCount = studentCount + 1 Accumulator – totalsales = totalsales + sales Notice that the variable name is repeated exactly on both sides of the equal sign Previous value is gone once a line is executed
4
Variable name reuse If a value might be needed later in the program, store it in a new variable before adding to it month1Sales = sales totalsales = totalsales + sales
5
Major and Minor totals “Major” refers to large group totals “Minor” refers to small group totals Examples – When totaling fund raising activity for students in a school, totals for students in a class would be considered minor totals. Totals for classes in the same grade would be stored in a major total. The total for the entire school might be called a grand total (a major total also).
6
Rolling totals For efficiency, add individual transactions to minor totals only. When the minor total for a group is complete, add it to the next higher level major total. Then set the minor total to zero, and re-use it to accumulate the total for the next group.
7
Where to use Counters – Number of records read – Number of lines printed – Number of errors – Successful/ unsuccessful attempts – Iterations of a loop Accumulators – Group totals – Total taxes – Total sales – Inventory value
8
Processing efficiency - summary Calculate summary average when needed using the value in the counters and accumulators (not for each transaction) Roll minor totals to major totals when processing groups of records Reset a variable and reuse it rather than creating a new variable when possible.
9
Controlling program flow Store status – Keep track of which actions have been completed – Store a beginning value for comparison or restore later Bread crumb path – Append or delete a value corresponding to hierarchy or flow Control flow on a branch – Use value to determine which branch of logic to follow – Use to indicate an action has been completed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.