2026年1月26日月曜日

What are Variables, Lists, and Operators in Scratch?

 In Scratch, Variables, Lists, and Operators are the "brains" of your program. They allow your sprites to remember information, make decisions, and perform calculations.

Here is a detailed explanation in English.


1. Variables (Data Storage)

A Variable is like a small box that holds one piece of information at a time. This information can be a number or text.

  • Global Variables ("For all sprites"): Every sprite in the project can see and change this value (e.g., a "Game Score").

  • Local Variables ("For this sprite only"): Only the sprite that owns the variable can change it. In OOP, these are called Private Properties. This is useful for things like a sprite's individual "Health" or "Speed."

  • Common Blocks:

    • set [my variable] to [0] (Assigns a value)

    • change [my variable] by [1] (Increments/Decrements)

2. Lists (Arrays)

A List is a collection of many variables grouped together. Think of it as a "to-do list" or a "inventory" where each item has an index number (1, 2, 3...).

  • Why use lists? If you want to remember the names of 50 players or all the items in a player's backpack, creating 50 separate variables would be difficult. A list stores them all in one place.

  • Key Features:

    • Add/Delete: You can dynamically change the size of the list during the game.

    • Search: You can check if a list contains [item].

  • Common Blocks:

    • add [apple] to [inventory]

    • item (1) of [inventory] (Retrieves the first item)

3. Operators (Calculations & Logic)

Operators are the blocks used to perform math or compare values. They are the "logic" that processes your variables.

A. Arithmetic Operators (Math)

These perform standard calculations:

  • Addition ($+$), Subtraction ($-$), Multiplication ($*$), and Division ($/$).

  • pick random (1) to (10): Very important for making games unpredictable.

  • mod: Finds the remainder of a division (useful for checking if a number is even or odd).

B. Comparison Operators (Booleans)

These compare two values and return either True or False:

  • greater than (>), less than (<), and equal (=).

  • Example: if <(Score) > (10)> then [Broadcast: Winner!]

C. Logical Operators

These combine multiple conditions:

  • and: True only if both sides are true.

  • or: True if at least one side is true.

  • not: Reverses the result (True becomes False).


Summary Table

FeatureConceptReal-world Example
VariableSingle Data PointYour current age.
ListCollection of DataA shopping list of groceries.
OperatorsLogic / MathCalculating your change at a store ($Price - Cash$).

📋 Copy Icon: Feel free to copy this explanation for your coding notes!

Would you like to see a small script example that combines a Variable and an Operator to create a "Level Up" system?

0 件のコメント:

コメントを投稿