computer science and software engineering© 2014 project lead the way, inc. roles of variables with...
TRANSCRIPT
![Page 1: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/1.jpg)
Computer Science and Software Engineering © 2014 Project Lead The Way, Inc.
Roles of Variables with Examples in Scratch
![Page 2: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/2.jpg)
• 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?
![Page 3: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/3.jpg)
• 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?
Fixed Most recent Stepper Walker Accumulator Aggregator Best-so-far One-way flag
![Page 4: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/4.jpg)
Quick Summary Fixed Assigned once
Most recent Assigned unpredictably
Accumulator Running total
Aggregator Running list
Stepper Predetermined sequence of values
Walker Elements of iterator
Best-so-far Record holder
One-way flag Won’t reset flag until after iteration
![Page 5: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/5.jpg)
• 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?
![Page 6: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/6.jpg)
• 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: FixedWhy use a fixed variable?
![Page 7: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/7.jpg)
• 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
![Page 8: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/8.jpg)
• 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-RecentWhy use a most recent variable?
![Page 9: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/9.jpg)
Variable Role: Most-Recent
Initialized
Unpredictably assigned
Value is used
![Page 10: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/10.jpg)
• 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: AccumulatorWhy use an accumulator variable?
![Page 11: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/11.jpg)
Variable Role: AccumulatorPattern: Initialize-Accumulate-Report
![Page 12: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/12.jpg)
• 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: AggregatorWhy use an aggregator variable?
![Page 13: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/13.jpg)
Variable Role: AggregatorPattern: Initialize-Aggregate-Report
![Page 14: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/14.jpg)
• Iterate a specific number of times• Know that 5th or 7th or nth iteration is being
executed• Represent integers– e.g., for factorials• Common
pattern:
Variable Role: StepperWhy use a stepper variable?
![Page 15: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/15.jpg)
• Refer to members of a collection during iteration.
Variable Role: WalkerWhy use a walker variable?
![Page 16: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/16.jpg)
• Scratch requires “indexing” with a stepper like C++. Python and Java are easier!0102
for fruit in fridge: do_something_with(fruit)
Variable Role: Walker
Pyt
hon,
Ja
va
Scr
atch
, C
++, C
![Page 17: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/17.jpg)
• 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-farWhy use a best-so-far variable?
![Page 18: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/18.jpg)
Variable Role: Best-so-farPattern:
• Initialize• Check and Set Record• Report
![Page 19: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/19.jpg)
• 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 FlagWhy use a one-way-flag variable?
![Page 20: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/20.jpg)
Variable Role: One-Way FlagPattern:
• Initialize to clear flag• Check and Raise Flag • Report
Event-driven example
![Page 21: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Roles of Variables with Examples in Scratch](https://reader036.vdocuments.mx/reader036/viewer/2022081602/551c3ac8550346a3488b47db/html5/thumbnails/21.jpg)
Variable Role: One-Way FlagPattern:
• Initialize to clear flag• Check and Raise Flag • Report
Procedural example