• A variable role is the reason we are using the variable.• Variables always remember data for later use.. • Create a single place to tweak a number used throughout a program – low maint
Trang 1Roles of Variables with Examples
in Scratch
Trang 2• A variable role is the reason we are using the variable.
• Variables always remember data for later use But why are we trying to
remember something?
What are variable roles?
Trang 3• Certain reasons for using a variable come up over and over
• Eight roles cover 90% of the variable use by first-year programmers.
What are variable roles?
Trang 4Quick Summary
Assigned once
Most recent Assigned unpredictably
Accumulator
Running total
Aggregator Running list
Trang 5• Roles say why we are using the variable.
• Roles are not syntax.
Examples of syntax include whether the variable is used in a conditional,
in an expression, or in an assignment block
What are variable roles?
Trang 6• Create a single place to tweak a number used throughout a program – low maintenance
• Make code easier to read – no wondering “why subtract 20 here?”
• Make it easy to add features: user decides on the constant
Variable Role: Fixed
Why use a fixed variable?
Trang 7• Pattern:
• Assigned at the head
of a program or at the
head of a code block
• Used in any way later but never assigned again
• Convention suggests all caps
Variable Role: Fixed
Trang 8• Retrieve or calculate once, use multiple times
• Remember state of a process
• Remember user input until needed
• Embed explanation
• Debug by printing
• Pattern: Appears on left of assignment and then in a variety of syntax
Variable Role: Most-Recent
Why use a most recent variable?
Trang 9Variable Role: Most-Recent
Initialized
Unpredictably assigned Value is used
Trang 10• To keep a running total or cumulative value – could be multiplication, addition, net,
• Common pattern:
1. Assigned to initial value before loop,
2. Assigned with inside of loop
3. Result used after loop
Variable Role: Accumulator
Why use an accumulator variable?
Trang 11Variable Role: Accumulator
Pattern: Initialize-Accumulate-Report
Trang 12• To collect items and remember them all separately
• Common pattern:
1. Initialize to empty collection before a loop,
2. Append element to aggregate during iteration
3. The aggregate is used during or after the loop
Variable Role: Aggregator
Why use an aggregator variable?
Trang 13Variable Role: Aggregator
Pattern: Initialize-Aggregate-Report
Trang 14• Iterate a specific number of times
• Know that 5th or 7th or nth iteration is being executed
• Represent integers– e.g., for factorials
pattern:
Variable Role: Stepper
Why use a stepper variable?
Trang 15• Refer to members of a collection during iteration.
Variable Role: Walker
Why use a walker variable?
Trang 16• Scratch requires “indexing” with a stepper like C++ Python and Java are
Trang 17• To remember the record holder while iterating across many opportunities to set the record
• Frequent pattern:
1. Initialize to worst-possible value before loop,
2. During iteration, compare something to best-so-far and maybe assign a
new record
3. After loop, best-so-far used as the true record-best from all iterations
Variable Role: Best-so-far
Why use a best-so-far variable?
Trang 18Variable Role: Best-so-far
Pattern:
• Initialize
• Check and Set Record
Trang 19• To remember whether any of several opportunities meet a single condition
• Common pattern:
1. “Clear” the flag (initialize) to say the opportunity has not yet been met
2. Check for condition with each iteration and “raise” flag if true
3. Flag is not cleared during iteration
4. After loop, check if flag was raised during the iterations
Variable Role: One-way Flag
Why use a one-way-flag variable?
Trang 20Variable Role: One-Way Flag
Pattern:
Event-driven example
Trang 21Variable Role: One-Way Flag
Pattern:
Procedural example