Presentation is loading. Please wait.

Presentation is loading. Please wait.

Roles of Variables with Examples in Scratch

Similar presentations


Presentation on theme: "Roles of Variables with Examples in Scratch"— Presentation transcript:

1 Roles of Variables with Examples in Scratch
© 2014 Project Lead The Way, Inc. Unpublished work © 2013 Project Lead The Way, Inc.

2 What are variable roles?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name What are variable roles? 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?

3 What are variable roles?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name What are variable roles? Certain reasons for using a variable come up over and over. Eight roles cover 90% of the variable use by first-year programmers. Fixed Most recent Stepper Walker Accumulator Aggregator Best-so-far One-way flag

4 Quick Summary Fixed Assigned once Most recent Assigned Accumulator
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name 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

5 What are variable roles?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name What are variable roles? 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.

6 Why use a fixed variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Fixed Why use a fixed variable? 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 A major source of bugs is when one “thing” appears in multiple places. It is common for that thing to be changed in some but not all of those places during revisions. Adding user input could be text input or by Graphical User Interface. Either way, using the user’s value is much easier if all the places that need that value already use a variable. Then, instead of being a fixed role,… (see next slide)

7 Variable Role: Fixed Pattern: Assigned at the head
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Fixed 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 A major source of bugs is when one “thing” appears in multiple places. It is common for that thing to be changed in some but not all of those places during revisions. Adding user input could be text input or by Graphical User Interface. Either way, using the user’s value is much easier if all the places that need that value already use a variable. Then, instead of being a fixed role,… (see next slide)

8 Variable Role: Most-Recent Why use a most recent variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Most-Recent Why use a most recent variable? 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

9 Variable Role: Most-Recent
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Most-Recent Initialized Value is used Unpredictably assigned

10 Variable Role: Accumulator Why use an accumulator variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Accumulator Why use an accumulator variable? To keep a running total or cumulative value – could be multiplication, addition, net, . . . Common pattern: Assigned to initial value before loop, Assigned with inside of loop Result used after loop

11 Variable Role: Accumulator Pattern: Initialize-Accumulate-Report
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Accumulator Pattern: Initialize-Accumulate-Report

12 Variable Role: Aggregator Why use an aggregator variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Aggregator Why use an aggregator variable? To collect items and remember them all separately Common pattern: Initialize to empty collection before a loop, Append element to aggregate during iteration The aggregate is used during or after the loop

13 Variable Role: Aggregator Pattern: Initialize-Aggregate-Report
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Aggregator Pattern: Initialize-Aggregate-Report

14 Variable Role: Stepper Why use a stepper variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Stepper Why use a stepper variable? 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: “Counter” is a fine word if the stepper starts at 1 and increments by 1, but “stepper” is more general and groups a larger number of reasons that programmers use a variable

15 Why use a walker variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Walker Why use a walker variable? Refer to members of a collection during iteration.

16 Variable Role: Walker Scratch requires “indexing” with a stepper like C++. Python and Java are easier! 01 02 for fruit in fridge: do_something_with(fruit) Python, Java Scratch, C++, C

17 Variable Role: Best-so-far Why use a best-so-far variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: Best-so-far Why use a best-so-far variable? To remember the record holder while iterating across many opportunities to set the record Frequent pattern: Initialize to worst-possible value before loop, During iteration, compare something to best-so-far and maybe assign a new record After loop, best-so-far used as the true record-best from all iterations

18 Variable Role: Best-so-far Pattern: Initialize Check and Set Record
Report Presentation Name Course Name Unit # – Lesson #.# – Lesson Name

19 Variable Role: One-way Flag Why use a one-way-flag variable?
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: One-way Flag Why use a one-way-flag variable? To remember whether any of several opportunities meet a single condition Common pattern: “Clear” the flag (initialize) to say the opportunity has not yet been met Check for condition with each iteration and “raise” flag if true Flag is not cleared during iteration After loop, check if flag was raised during the iterations

20 Variable Role: One-Way Flag Pattern: Initialize to clear flag
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: One-Way Flag Pattern: Initialize to clear flag Check and Raise Flag Report Event-driven example

21 Variable Role: One-Way Flag Pattern: Initialize to clear flag
Presentation Name Course Name Unit # – Lesson #.# – Lesson Name Variable Role: One-Way Flag Pattern: Initialize to clear flag Check and Raise Flag Report Procedural example


Download ppt "Roles of Variables with Examples in Scratch"

Similar presentations


Ads by Google