1 1 5 roles of variables _ SCRATCH

21 129 0
1 1 5 roles of variables _ SCRATCH

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Roles of Variables with Examples in Scratch Computer Science and Software Engineering © 2014 Project Lead The Way, Inc 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? 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  Stepper  Accumulator  Best-so-far  Most recent  Walker  Aggregator  One-way flag Quick Summary  Fixed Assigned once  Most recent Assigned unpredictably  Accumulator Running total  Stepper Predetermined  Aggregator Running list  Walker Elements of iterator sequence of values  Best-so-far Record holder  One-way flag Won’t reset flag until after iteration 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 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 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 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 Variable Role: Most-Recent Initialized Value is used Unpredictably assigned 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 Result used after loop inside of loop Variable Role: Accumulator Pattern: Initialize-Accumulate-Report 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 Variable Role: Aggregator Pattern: Initialize-Aggregate-Report Variable Role: Stepper Why use a stepper variable? • • • • Iterate a specific number of times Know that th th th or or n iteration is being executed Represent integers– e.g., for factorials Common pattern: Variable Role: Walker Why use a walker variable? • Refer to members of a collection during iteration Variable Role: Walker Scratch requires “indexing” with a stepper like C++ Python and Java are 02 for fruit in fridge: do_something_with(fruit) Py tho n, 01 Ja va easier! Sc rat ch , C+ +, C • 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 Variable Role: Best-so-far Pattern: • • • Initialize Check and Set Record Report 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 Variable Role: One-Way Flag Pattern: • • • Initialize to clear flag Check and Raise Flag Report Event-driven example Variable Role: One-Way Flag Pattern: • • • Initialize to clear flag Check and Raise Flag Report Procedural example ... Elements of iterator sequence of values  Best-so-far Record holder  One-way flag Won’t reset flag until after iteration What are variable roles? • • Roles say why we are using the variable Roles. .. iteration Variable Role: Walker Scratch requires “indexing” with a stepper like C++ Python and Java are 02 for fruit in fridge: do_something_with(fruit) Py tho n, 01 Ja va easier! Sc rat ch , C+... 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:

Ngày đăng: 07/07/2017, 07:24

Mục lục

  • Slide 1

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan