Class 9Computer ScienceFull chapter

Programming with Scratch

Your first real programming chapter. Learn the Scratch interface, the block categories and block shapes, the three control structures — sequence, selection and iteration — and how variables, costumes and broadcast messages turn blocks into a working project.

Why Block-Based Programming Exists

Quick answer Text-based languages punish beginners with syntax errors. Block-based languages such as Scratch hand you ready-made instruction blocks so that all your attention goes to the logic.

A program is a set of instructions written in a language that a computer can follow, and the person who writes it is a programmer. Before a single instruction is typed or dragged, the programmer must work out the algorithm — a finite, step-by-step method that solves the problem in a definite order.

Most programming languages are text-based. In a text-based language such as Python, Java or C++, every instruction is typed as a line of text and must obey the grammar of that language exactly. Miss a bracket or spell a keyword wrongly, and the computer reports a syntax error — an error caused by breaking the grammar rules of the language — and refuses to run the program at all. A beginner can then lose a whole lab period hunting a missing semicolon instead of learning logic.

Block-based programming, also called visual programming, was created to remove exactly that obstacle. Instructions come as ready-made blocks shaped like jigsaw pieces; the learner drags one out of the palette and snaps it under another. Because the wording, spelling and punctuation are already printed inside the block, a syntax error is impossible. Because every block has a shape, blocks that do not belong together simply refuse to join. The learner's attention goes entirely to the part that matters: what should happen, in what order, under what condition, and how many times.

Scratch is the best known block-based language. It was developed by the Lifelong Kindergarten group at the MIT Media Lab and is free to use. It runs in a web browser or as an offline editor installed on the computer, so a lab with a slow connection can still use it. A piece of work made in Scratch is called a project. In current versions a saved project file carries the extension .sb3; older versions used .sb2, so quote the extension only if your textbook does.

Advantages an examiner expects you to list: syntax errors cannot happen; the output is visible immediately on the stage, so a mistake in logic is spotted at once; blocks are colour-coded, so the right instruction is easy to find; sound, pictures and animation are built in; and the same three control structures — sequence, selection and iteration — reappear in Python and Java, so the thinking transfers.

Know the limits as well, because a state two limitations question is common. Dragging is slower than typing, so long programs are tiring to build; large scripts become hard to read; block languages are not normally used to build professional software; and, apart from a few ready-made extensions, they do not work easily with files, databases or hardware. Scratch is a learning language — the logic it teaches is later rewritten in a text-based language.

What the examiner wants: define block-based programming as a way of writing programs by dragging and joining ready-made instruction blocks instead of typing text, and then give one advantage in your own words.

Program Set of instructions for a computer definition · Written by a programmer in a programming language.
Algorithm Finite, ordered, step-by-step method definition · Written before the code, in plain language.
Syntax error Grammar rule of the language broken error type · Impossible in Scratch because the blocks are pre-written.
Scratch project file .sb3 (older versions .sb2) extension · Saved with File then Save to your computer.
Remember
  • A program is a set of instructions; an algorithm is the step-by-step logic behind it.
  • A syntax error is an error caused by breaking the grammar rules of a language.
  • Block-based (visual) programming uses ready-made jigsaw blocks, so syntax errors cannot occur.
  • Scratch was developed by the Lifelong Kindergarten group at the MIT Media Lab and is free.
  • Advantages: no syntax errors, instant visual output, colour-coded blocks, logic transfers to Python.
  • Limitations: slow for long programs, not normally used for professional software, weak file and hardware handling.

The Scratch Interface

Quick answer Four working areas — stage, sprite list, blocks palette and code area — plus the green flag, the stop sign and the Code, Costumes and Sounds tabs.

The Scratch editor is one window divided into four working areas. Learn all four names: questions often ask simply where a project runs or where blocks are joined, and labelling a diagram of the window is a standard practical-file task. The exact arrangement shifts between editors and versions, so describe the areas by what they do rather than by a fixed corner of the screen.

1. The stage. The stage is the rectangular area, usually at the upper right, where the project actually runs. It is the output area — everything the viewer sees happens here. The stage uses a coordinate system: the centre is x = 0, y = 0; x runs from −240 on the left to 240 on the right, and y from −180 at the bottom to 180 at the top, so the stage is 480 units wide and 360 units tall. The picture behind everything is called the backdrop. A project may hold several backdrops and switch between them to change scene. The stage can hold scripts of its own, but it cannot move, and it has backdrops rather than costumes.

2. The sprite list, also called the sprite pane. A sprite is an object or character in the project that can be given instructions — the cat, a ball, a car, a button. Every sprite owns its own scripts, its own costumes and its own sounds. The sprite pane, normally just below the stage, shows a small picture of every sprite and lets you set its name, its x and y position, its size, its direction and whether it is shown or hidden. New sprites come from the sprite library, the paint editor or a file.

3. The blocks palette. This is the tall column on the left listing every instruction available, arranged in colour-coded categories. Nothing in the palette runs the project — blocks must first be dragged out of it.

4. The code area, also called the script area. This is the large working space in the middle. Blocks dragged here and snapped together form a script: one connected stack that runs as a single unit. A sprite may hold several separate scripts side by side, and they can run at the same time. Clicking a script once runs it straight away, a quick way to test part of a project.

Above the code area sit three tabs — Code, Costumes and Sounds. When the stage is selected instead of a sprite, the Costumes tab becomes Backdrops. Above the stage are the green flag, which starts the project, the red stop sign, which stops every running script, and a full-screen button that hides the editor. The File menu offers New, Save to your computer and Load from your computer, though the exact wording differs between the online and offline editors.

What the examiner wants: keep two pairs straight. Sprite versus stage — the sprite is a movable object, the stage is the fixed area it moves on. Costume versus backdrop — a costume belongs to a sprite, a backdrop belongs to the stage.

Stage size 480 units wide, 360 units tall stage units · x from −240 to 240, y from −180 to 180.
Stage centre x = 0, y = 0 coordinates · go to x: 0 y: 0 sends a sprite to the middle.
Sprite Object that carries out instructions definition · Owns its scripts, costumes and sounds.
Backdrop Background picture of the stage definition · Costume is the equivalent word for a sprite.
Editor tabs Code, Costumes (Backdrops), Sounds tabs · Costumes becomes Backdrops when the stage is selected.
Remember
  • Stage = the output area where the project runs; centre is x = 0, y = 0.
  • Stage coordinates run x −240 to 240 and y −180 to 180, so the stage is 480 by 360 units.
  • A sprite is an object that can be given instructions; each sprite has its own scripts, costumes and sounds.
  • The blocks palette lists all instructions; the code (script) area is where blocks are joined.
  • A script is one connected stack of blocks that runs as a unit.
  • Green flag starts the project, the red stop sign stops all scripts; the stage has backdrops, not costumes.

Block Categories, Block Shapes and Sequence

Quick answer Nine colour-coded categories supply the instructions; four block shapes decide where a block may be used; sequence means blocks run top to bottom, so order changes the result.

In the current version of Scratch the palette groups blocks into nine categories; older versions grouped them differently, so follow your textbook. Colours also differ between versions, so learn the names and the jobs rather than the colours.

Motion blocks move and turn a sprite: move 10 steps, turn 15 degrees, go to x: y:, glide 1 secs to x: y:, if on edge, bounce. Motion blocks work only on sprites, never on the stage. Looks blocks change appearance: say [Namaste!] for 2 seconds, think, switch costume to, next costume, show and hide. Sound blocks play and control sound: start sound, play sound until done, change volume by.

Events blocks start scripts when something happens — when green flag clicked, when [space] key pressed, when this sprite clicked, when I receive [message]. Control blocks decide the order in which instructions run: wait, repeat, forever, if ... then, if ... then ... else, repeat until, stop. Sensing blocks let the project notice the outside world: touching [edge]?, key [space] pressed?, mouse down?, and ask [ ] and wait together with the answer reporter.

Operators blocks do the arithmetic and the comparing: +, , *, /, pick random 1 to 10, the comparisons >, < and =, the logic blocks and, or, not, and the text block join. Variables blocks create and change stored values, and also hold lists. My Blocks lets you build your own named block from instructions you keep repeating.

Every block also has a shape, which decides where it is allowed to go. A hat block has a curved top, sits at the very top of a script and starts it — every Events block is a hat block. A stack block is the ordinary jigsaw shape with a notch on top and a bump below; it does one job and passes control to the block underneath. A reporter block is rounded and reports a value — a number or text — such as x position, answer, pick random and every variable. It never sits in a stack on its own but drops into a rounded hole inside another block. A Boolean block is hexagonal — pointed at both ends — and reports only true or falsetouching [edge]?, key pressed?, ( ) > ( ). It fits only into a hexagonal hole. A cap block is flat along the bottom and ends a script, so nothing can be attached below it: stop [all] and the forever loop behave this way.

Sequence is the first and simplest control structure: instructions are carried out one after another from the top of the script to the bottom, each block finishing before the next starts. Order therefore changes the result — in the script below, swapping the last two blocks would make the sprite move before it speaks, which is a different program built from identical blocks.

when green flag clicked
go to x: 0 y: 0
point in direction 90
say [Namaste! Watch me move.] for 2 seconds
move 100 steps

What the examiner wants: for name the category questions, connect the verb to the group — moving is Motion, saying is Looks, deciding or repeating is Control, detecting is Sensing, calculating or comparing is Operators.

Motion move, turn, go to x y, glide category · Works on sprites only, never on the stage.
Looks say, think, next costume, show, hide category · Controls appearance and speech bubbles.
Sensing touching?, key pressed?, ask and wait, answer category · Detects the user, the mouse and other sprites.
Operators + − * /, pick random, =, and, or, join category · Arithmetic, comparison, logic and text joining.
Block shapes hat, stack, reporter, Boolean, cap shapes · The shape decides where a block is allowed to fit.
Remember
  • Nine categories: Motion, Looks, Sound, Events, Control, Sensing, Operators, Variables, My Blocks.
  • Hat blocks start a script; stack blocks do a job; cap blocks end a script.
  • Reporter blocks are rounded and report a value; Boolean blocks are hexagonal and report true or false.
  • ask and wait, and the answer reporter, belong to Sensing; pick random and join belong to Operators.
  • Sequence means blocks run from top to bottom, one after another.
  • Changing the order of blocks changes the program, even when the blocks are the same.

The Three Control Structures

Quick answer Every program ever written is built from three structures — sequence, selection and iteration. Scratch gives each of them a block you can see and touch.

A control structure decides the order in which the instructions of a program are carried out. There are only three, and every program in every language is built from them in some combination. Scratch shows all three as visible shapes.

1. Sequence. Instructions run one after another, top to bottom, each finishing before the next begins; a plain stack of blocks under a hat block is a sequence. Making tea is a sequence — boil water, add tea leaves, add milk, strain — and changing the order changes the result.

2. Selection, also called branching or decision. The program tests a condition and chooses which instructions to run. A condition is anything that is either true or false, so in Scratch it is always a hexagonal Boolean block dropped into the hexagonal hole. Scratch offers two selection blocks. The if ... then block runs the blocks inside its mouth only when the condition is true. The if ... then ... else block has two mouths: the first runs when the condition is true, the second when it is false. Exactly one of the two always runs, never both, never neither.

when green flag clicked
ask [What is 12 x 8?] and wait
if <(answer) = 96> then
    say [Correct!] for 2 seconds
else
    say [Wrong. The answer is 96.] for 2 seconds

Putting a second if inside the else mouth of the first is called nesting, and it is how a program grades marks into A, B and C.

3. Iteration, also called looping or repetition. A group of instructions is carried out more than once, so the same block need not be dragged twenty times. Scratch has three loop blocks, and the difference between them is commonly asked.

repeat (10) is a counted loop: the blocks inside run a fixed number of times that you type in. forever is an infinite loop: the blocks inside repeat endlessly until the red stop sign is clicked or a stop block runs. It is a cap block — flat along the bottom — so nothing can be attached below it, because nothing below it could ever run. repeat until <condition> is a conditional loop: it checks the condition before each pass and keeps repeating while the condition is false, stopping the moment it becomes true. Because the test comes first, a repeat until loop may run zero times — if the condition is already true when the loop is reached, the blocks inside are never carried out at all. That single sentence answers many questions about loops.

when green flag clicked
go to x: -200 y: 0
repeat until <(x position) > 200>
    change x by 10
    next costume
say [I reached the other side.] for 2 seconds

A loop inside another loop is a nested loop, used for patterns — an outer repeat 4 that turns a corner with an inner repeat 10 that draws a dotted edge.

What the examiner wants: name all three in order — sequence, selection, iteration — define each in one line, and give one block as evidence for each: a plain stack, if ... then ... else and repeat. If the question says with an example, a short script answers better than a paragraph.

Sequence Blocks run top to bottom, once each structure · The default order in every script.
Selection if &lt;condition&gt; then / else structure · Exactly one branch of if-else runs.
Counted loop repeat (10) iteration · Runs a fixed, known number of times.
Infinite loop forever iteration · Runs until the red stop sign or a stop block.
Conditional loop repeat until &lt;condition&gt; iteration · Tests first; may run zero times.
Remember
  • The three control structures are sequence, selection and iteration.
  • Selection tests a condition; if...then runs blocks only when true, if...then...else always runs exactly one branch.
  • A condition in Scratch is a hexagonal Boolean block reporting true or false.
  • repeat (n) runs a fixed number of times; forever repeats endlessly until stopped.
  • repeat until tests before each pass, so it can run zero times if the condition is already true.
  • forever is a cap block, so no block can be attached below it.

Events, the Green Flag and Broadcasting

Quick answer Scratch is event-driven: nothing runs until something happens. Broadcast messages let one sprite tell every other sprite when to act.

An event is something that happens which a program can notice and respond to — a click, a key press, a message arriving. Scratch is event-driven, which means no script runs on its own; each script waits under a hat block until its event occurs, which is how the software on a phone or a computer behaves.

The commonest hat block is when green flag clicked. The green flag above the stage is the start button. Clicking it fires every such script in every sprite and in the stage at the same moment, so several scripts run side by side. The red stop sign beside it stops all running scripts at once. Other Events hat blocks include when [space] key pressed, which is how arrow-key control of a game is built; when this sprite clicked, used for buttons; when backdrop switches to [ ]; and when loudness > (10), which reacts to sound from the microphone.

A single sprite may carry several scripts, each with its own hat block, running independently: one can hold a forever loop that moves the sprite while another waits for a key press, which is how a game character moves and jumps at once.

Broadcasting is the way sprites talk to one another. A broadcast is a message sent by one sprite (or by the stage) that every sprite in the project can hear. It is like an announcement over the school public address system: the office speaks once, and every class told to act on it does so. Messages are made in the Events category with New message and should be given meaningful names such as correctAnswer or gameOver.

Three blocks are involved. broadcast [message] sends the message and the sending script carries straight on. broadcast [message] and wait sends it and then pauses the sending script until every script that received it has finished. when I receive [message] is a hat block: any sprite carrying it starts that script the moment the message is sent. One message may be received by many sprites, and a sprite may listen for several messages.

Here two sprites are synchronised: the Teacher sprite asks a question, and the Star sprite stays hidden until it hears the message.

Sprite: Teacher
when green flag clicked
ask [Capital of India?] and wait
if <(answer) = [Delhi]> then
    broadcast [correctAnswer]
else
    say [Not quite. It is Delhi.] for 2 seconds

Sprite: Star
when green flag clicked
hide

when I receive [correctAnswer]
go to x: 0 y: 100
show
repeat 10
    change size by 5
    wait 0.1 seconds
set size to 100 %

Without broadcasting the Star sprite could not know what the Teacher sprite found out, because a sprite cannot read another sprite's script. Broadcasting is therefore the method of communication and synchronisation between sprites.

What the examiner wants: define broadcasting as sending a message from one sprite that other sprites can receive, so their scripts start at the right moment, name the pair broadcast and when I receive, and add that broadcast and wait pauses the sender.

Event Happening the program responds to definition · Click, key press, message, backdrop change.
Green flag when green flag clicked hat block · Starts all such scripts together.
Send a message broadcast [name] Events block · Sender does not pause.
Send and pause broadcast [name] and wait Events block · Sender waits for all receivers to finish.
Receive when I receive [name] hat block · Starts a script in any sprite that listens.
Remember
  • An event is something that happens which a program responds to; Scratch is event-driven.
  • The green flag starts every when-green-flag-clicked script at the same time; the red stop sign stops all scripts.
  • Events hat blocks include key pressed, sprite clicked, backdrop switches to, and when I receive.
  • Broadcasting sends a message from one sprite that any sprite can receive with when I receive.
  • broadcast continues at once; broadcast and wait pauses the sender until receivers finish.
  • Broadcasting is used for communication and synchronisation between sprites.

Variables, Costumes and Simple Animation

Quick answer A variable is a named store whose value can change while the program runs. Costumes are the alternative pictures of a sprite, and switching them makes animation.

A variable is a named location in the computer's memory that holds a value which can change while the program runs. The name stays the same; the value varies — which is why it is called a variable. It can hold a number such as 250 or text such as Anita.

A variable is created in the Variables category with Make a Variable. You type a meaningful name — score, lives, total — and choose one of two options. For all sprites makes a global variable that every sprite and the stage can read and change, so a game score should be global. For this sprite only makes a local variable belonging to that one sprite. The new variable appears in the palette as a rounded reporter block.

Three blocks handle it. set [score] to (0) puts a value in, replacing whatever was there — always use it at the start of a project to reset. change [score] by (1) adds to the current value, so it is used for counting; change [score] by (-1) subtracts. The tick box beside the variable, or the show variable block, displays it on the stage in a small watcher panel — the easiest way to check a program while it runs.

A shop example in rupees:

when green flag clicked
ask [Price of one notebook in rupees?] and wait
set [price] to (answer)
ask [How many notebooks?] and wait
set [quantity] to (answer)
set [total] to ((price) * (quantity))
say (join [Total: Rs ] (total)) for 3 seconds

Notice that answer is only a temporary Sensing reporter — it is replaced by the next ask. That is why the value is copied into a variable straight away.

A costume is one of the alternative pictures of a sprite. A sprite may have many costumes, managed in the Costumes tab, where they can be painted, uploaded or chosen from the library. Only one is visible at a time. The Looks blocks switch costume to [ ] and next costume change which one is shown; next costume moves to the following costume and wraps round to the first after the last.

Animation is created by switching costumes quickly one after another so that the eye reads the change as movement — the same principle as a flip book. Two costumes showing a character with legs apart and legs together are enough for a walk. A wait block inside the loop controls the speed: without it the costumes change too fast to see.

when green flag clicked
go to x: -180 y: 0
point in direction 90
forever
    next costume
    move 10 steps
    if on edge, bounce
    wait 0.1 seconds

The stage does the same with backdrops: switch backdrop to [classroom] changes the scene, and next backdrop steps through them.

What the examiner wants: define a variable as a named memory location whose value can change during the program, and state the difference between set (replaces the value) and change (adds to it). For animation, say costumes switched rapidly with a wait between them, not just the sprite moves.

Variable Named store whose value can change definition · Created with Make a Variable in the Variables palette.
set set [score] to (0) Variables block · Replaces the existing value; used to reset.
change change [score] by (1) Variables block · Adds to the existing value; used to count.
Costume next costume / switch costume to Looks block · Alternative pictures of one sprite.
Animation next costume + wait inside a loop technique · Backdrops do the same job for the stage.
Remember
  • A variable is a named memory location whose stored value can change while the program runs.
  • Variables can be For all sprites (global) or For this sprite only (local).
  • set replaces the stored value; change adds to it, so change is used for counting.
  • The answer reporter is temporary and is overwritten by the next ask, so copy it into a variable.
  • A costume is one of the alternative pictures of a sprite; only one is visible at a time.
  • Animation = switching costumes rapidly with a wait block to control the speed.

Plan First: Algorithm, Flowchart and a Worked Project

Quick answer Write the algorithm, draw the flowchart, then drag the blocks. A complete guess-the-number project shows all of it working together.

Dragging blocks before the logic is clear wastes a lab period. The correct order of work is problem, algorithm, flowchart, program, testing, and questions are often set on the planning stages alone.

An algorithm is a finite sequence of unambiguous steps that solves a problem. It must have a definite start and stop, take input, produce output, and use steps clear enough that two people would carry them out identically. A flowchart is the same algorithm drawn as a diagram, using standard symbols joined by arrows called flow lines. A loop or a branch is visible at a glance, but a long flowchart is clumsy to draw and to change.

The symbols to memorise: an oval (terminal) marks Start and Stop; a parallelogram marks input and output; a rectangle marks a process such as a calculation; a diamond marks a decision, with one arrow in and two out labelled Yes and No; a small circle is a connector; and arrows show the flow.

The worked project: Guess the Secret Number. The computer picks a secret number from 1 to 20. The player keeps guessing; after each guess the sprite says whether it is too low or too high, and the tries are counted. When the guess is correct a second sprite appears to celebrate.

The algorithm, in plain language:

  1. Start.
  2. Set secret to a random whole number from 1 to 20.
  3. Set tries to 0 and guess to 0.
  4. Repeat until guess equals secret: ask for a guess; store it in guess; add 1 to tries; if guess is less than secret say Too low, otherwise if it is greater say Too high.
  5. Broadcast the message winner.
  6. Display the number of tries.
  7. Stop.

The flowchart begins with an oval Start and a rectangle for the three variable settings, then a diamond asking guess = secret? Its No arrow leads to an input parallelogram, a rectangle for tries = tries + 1 and a second diamond guess < secret?, whose two outputs loop back. Its Yes arrow leads to an output for the tries and an oval Stop.

The first sprite holds the logic:

Sprite: Cat
when green flag clicked
set [secret] to (pick random (1) to (20))
set [tries] to (0)
set [guess] to (0)
repeat until <(guess) = (secret)>
    ask [Guess my number from 1 to 20] and wait
    set [guess] to (answer)
    change [tries] by (1)
    if <(guess) < (secret)> then
        say [Too low - guess higher.] for 1 seconds
    else
        if <(guess) > (secret)> then
            say [Too high - guess lower.] for 1 seconds
broadcast [winner]
say (join [Correct! Tries: ] (tries)) for 3 seconds

Two details matter. guess is set to 0 at the start because 0 can never equal a secret number from 1 to 20, so the repeat until loop is guaranteed to run at least once. The inner if is nested inside the else, so when the guess is neither lower nor higher — that is, correct — nothing is said and the loop ends.

The second sprite waits for the message:

Sprite: Balloon
when green flag clicked
hide

when I receive [winner]
go to x: (0) y: (100)
show
repeat 10
    change size by (5)
    wait 0.1 seconds
set size to (100) %

Finally, test the project. A bug is an error in a program and debugging is finding and removing it. Test with a guess of 1, a guess of 20 and a wrong guess, and tick the variable watchers to see secret and tries change.

What the examiner wants: when a question says write an algorithm and the Scratch blocks, give numbered steps ending in Stop, then the script. Never skip the algorithm.

Oval (terminal) Start / Stop flowchart symbol · Every flowchart has exactly one Start.
Parallelogram Input / Output flowchart symbol · Reading a guess, printing the total.
Rectangle Process flowchart symbol · Calculation or assignment such as tries = tries + 1.
Diamond Decision flowchart symbol · One arrow in, two out, labelled Yes and No.
Debugging Finding and removing bugs definition · A bug is an error in a program.
Remember
  • Correct order of work: problem, algorithm, flowchart, program, testing.
  • An algorithm is a finite set of unambiguous ordered steps with a definite start and stop.
  • Flowchart symbols: oval = start/stop, parallelogram = input/output, rectangle = process, diamond = decision.
  • A decision diamond has one arrow in and two labelled arrows out (Yes and No).
  • Initialise variables before a repeat until loop so the loop is entered correctly.
  • A bug is an error in a program; debugging is finding and removing it.

Quick reference

Every term, tag and rule from this chapter in one place — screenshot it before your exam.

Set of instructions for a computer
Programdefinition
Finite, ordered, step-by-step method
Algorithmdefinition
Grammar rule of the language broken
Syntax errorerror type
.sb3 (older versions .sb2)
Scratch project fileextension
480 units wide, 360 units tall
Stage sizestage units
x = 0, y = 0
Stage centrecoordinates
Object that carries out instructions
Spritedefinition
Background picture of the stage
Backdropdefinition
Code, Costumes (Backdrops), Sounds
Editor tabstabs
move, turn, go to x y, glide
Motioncategory
say, think, next costume, show, hide
Lookscategory
touching?, key pressed?, ask and wait, answer
Sensingcategory
+ − * /, pick random, =, and, or, join
Operatorscategory
hat, stack, reporter, Boolean, cap
Block shapesshapes
Blocks run top to bottom, once each
Sequencestructure
if &lt;condition&gt; then / else
Selectionstructure
repeat (10)
Counted loopiteration
forever
Infinite loopiteration
repeat until &lt;condition&gt;
Conditional loopiteration
Happening the program responds to
Eventdefinition
when green flag clicked
Green flaghat block
broadcast [name]
Send a messageEvents block
broadcast [name] and wait
Send and pauseEvents block
when I receive [name]
Receivehat block
Named store whose value can change
Variabledefinition
set [score] to (0)
setVariables block
change [score] by (1)
changeVariables block
next costume / switch costume to
CostumeLooks block
next costume + wait inside a loop
Animationtechnique
Start / Stop
Oval (terminal)flowchart symbol
Input / Output
Parallelogramflowchart symbol
Process
Rectangleflowchart symbol
Decision
Diamondflowchart symbol
Finding and removing bugs
Debuggingdefinition

Test yourself

Tap an answer to check it instantly — you'll see why it's right, and what to revise if it isn't.

0 correct · 0/12 answered
Q1 Why block-based programming easy

What is the main advantage of a block-based language such as Scratch over a text-based language for a beginner?

Q2 Scratch interface easy

A sprite is sent to the centre of the stage. Which block does this?

Q3 Block shapes easy

A block with a curved top that must sit at the very top of a script is called a

Q4 Block shapes medium

A hexagonal (pointed) block in Scratch always reports

Q5 Iteration hard

How many times will the blocks inside a repeat until loop run if the condition is already true when the loop is reached?

Q6 Iteration medium

Why can no block be attached below a forever loop?

Q7 Broadcasting easy

Broadcasting in Scratch is used to

Q8 Broadcasting hard

What is the difference between broadcast [go] and broadcast [go] and wait?

Q9 Variables medium

The variable score holds 7. After the block change [score] by (1) runs, score holds

Q10 Costumes and animation easy

Simple animation of a walking character in Scratch is produced by

Q11 Block categories medium

In which block category will you find pick random 1 to 10?

Q12 Flowcharts easy

In a flowchart, a diamond shape represents

NCERT solutions & previous-year questions

Step-by-step model answers — tap a question to reveal the full solution.

NCERT questions 8

1 What is block-based programming? State two advantages of using it to learn programming.Why block-based programming

Block-based programming, also called visual programming, is a way of writing programs by dragging ready-made instruction blocks from a palette and snapping them together, instead of typing lines of text.

Two advantages:

  1. No syntax errors. The wording and punctuation are already inside each block, and blocks whose shapes do not match refuse to join, so the beginner never loses time on missing brackets or misspelt keywords.
  2. Immediate visible output. The result appears on the stage as soon as the script runs, so a mistake in the logic is noticed and corrected at once.

(Other acceptable advantages: colour-coded categories make blocks easy to find; sound and animation are built in; the same control structures are used later in text-based languages.)

2 Name and describe the four main areas of the Scratch window.Scratch interface
  1. Stage — the rectangular output area where the project runs. Its centre is x = 0, y = 0, and it measures 480 by 360 units. Its background picture is the backdrop.
  2. Sprite list (sprite pane) — shows every sprite in the project and lets you set each sprite's name, position, size, direction and visibility.
  3. Blocks palette — the column listing all available instruction blocks, arranged in colour-coded categories.
  4. Code area (script area) — the large working space where blocks are dragged and snapped together to form scripts.

Above these are the Code, Costumes and Sounds tabs, and above the stage are the green flag and the red stop sign.

3 Differentiate between a sprite and the stage.Scratch interface

A sprite is an object or character in a project that can be given instructions — it can move, turn, change size, speak and change costume. A project may contain many sprites, and each sprite has its own scripts, costumes and sounds.

The stage is the fixed area on which the sprites appear and the project runs. It cannot move and cannot use Motion blocks. Its background pictures are called backdrops, not costumes, although the stage can still hold scripts of its own, for example to switch backdrops when a message is received.

In one line: the sprite is the actor, the stage is the place where the acting happens.

4 What is a variable? How is a variable created in Scratch, and what is the difference between the set and change blocks?Variables

A variable is a named location in memory that stores a value which can change while the program is running. The name stays fixed; the value inside it varies. In Scratch a variable can hold a number or a piece of text.

Creating one: open the Variables category, click Make a Variable, type a meaningful name such as score, and choose For all sprites (a global variable, usable by every sprite) or For this sprite only (a local variable belonging to one sprite). The new variable then appears as a rounded reporter block.

Difference:

  • set [score] to (0) — replaces whatever is stored with the new value. Used to reset a variable at the start of a project.
  • change [score] by (1) — adds the given amount to the value already stored. Used for counting, and a negative amount subtracts.
5 Explain the three control structures with one Scratch example each.Control structures

1. Sequence — instructions run one after another from the top of the script to the bottom.

when green flag clicked
go to x: 0 y: 0
say [Hello!] for 2 seconds
move 50 steps

2. Selection — a condition is tested and the program chooses which blocks to run.

if <(answer) > (32)> then
    say [Pass] for 2 seconds
else
    say [Fail] for 2 seconds

3. Iteration (looping) — a group of blocks is repeated.

repeat 4
    move 100 steps
    turn 90 degrees

The loop above draws a square path. Scratch also provides forever, which repeats endlessly, and repeat until <condition>, which repeats while the condition is false.

6 What is broadcasting? Explain with an example how two sprites use it.Broadcasting

Broadcasting is the sending of a message by one sprite (or the stage) which every sprite in the project can receive. It is the way sprites communicate with each other and keep their actions synchronised, because one sprite cannot read another sprite's script.

The message is created in the Events category. The sender uses broadcast [name], and any sprite that should react carries the hat block when I receive [name].

Sprite A (Runner)
when green flag clicked
glide 2 secs to x: 200 y: 0
broadcast [reachedEnd]

Sprite B (Flag)
when green flag clicked
hide

when I receive [reachedEnd]
show
say [Race over!] for 2 seconds

The block broadcast [name] and wait does the same job but pauses the sending script until every receiving script has finished.

7 What is a costume? How are costumes used to animate a sprite?Costumes and animation

A costume is one of the alternative pictures of a sprite. A sprite may have several costumes, managed in the Costumes tab, where they can be drawn, uploaded or chosen from the library. Only one costume is displayed at a time.

Animation is produced by switching costumes quickly one after another so that the eye reads the change as movement, exactly like a flip book. The Looks blocks next costume and switch costume to [ ] do the switching, and a wait block inside the loop controls the speed.

when green flag clicked
forever
    next costume
    move 10 steps
    if on edge, bounce
    wait 0.1 seconds

Without the wait block the costumes change too quickly to be seen as a walk. The stage uses backdrops in the same way to change scenes.

8 Write an algorithm and draw (describe) a flowchart for a program that asks a student's marks and displays Pass if the marks are 33 or more, otherwise Fail.Algorithms and flowcharts

Algorithm

  1. Start.
  2. Read the marks obtained by the student into marks.
  3. If marks is greater than or equal to 33, display Pass.
  4. Otherwise display Fail.
  5. Stop.

Flowchart — an oval marked Start, an arrow to a parallelogram Read marks, an arrow to a diamond marks >= 33?. The Yes arrow leads to a parallelogram Display Pass; the No arrow leads to a parallelogram Display Fail. Both arrows then join and lead to an oval marked Stop.

Scratch blocks

when green flag clicked
ask [Enter your marks] and wait
set [marks] to (answer)
if <(marks) > (32)> then
    say [Pass] for 2 seconds
else
    say [Fail] for 2 seconds

Scratch has no ready-made greater than or equal to block, so marks > 32 is used for whole-number marks; an or block combining > and = is equally acceptable.

Previous-year board questions 5

Q1 Name the block category that contains the blocks ask [ ] and wait, and answer. 1 mark

Sensing. Sensing blocks let a project detect the outside world, including input typed by the user.

Q2 Differentiate between the repeat (10) block and the repeat until block. 2 marks
  • repeat (10) is a counted loop. The blocks inside run a fixed number of times decided by the programmer, and the number does not depend on anything that happens while the program runs.
  • repeat until <condition> is a conditional loop. It checks a condition before each pass and keeps repeating while the condition is false, stopping as soon as it becomes true. The number of repetitions is not known in advance, and the loop may run zero times if the condition is already true.
Q3 Write the Scratch blocks for a project in which a sprite asks the user for a number and says whether the number is positive, negative or zero. 3 marks
when green flag clicked
ask [Enter a number] and wait
set [n] to (answer)
if <(n) > (0)> then
    say [Positive] for 2 seconds
else
    if <(n) < (0)> then
        say [Negative] for 2 seconds
    else
        say [Zero] for 2 seconds

The second if ... then ... else is nested inside the else mouth of the first, which is how three outcomes are produced from blocks that each offer only two.

Q4 What is broadcasting in Scratch? State one difference between broadcast and broadcast and wait. 3 marks

Broadcasting is the sending of a named message by one sprite or by the stage, which any sprite in the project can receive using the hat block when I receive [message]. It is used for communication and synchronisation between sprites, so that one sprite can make another start acting at the correct moment.

Difference: broadcast [message] sends the message and the sending script continues immediately, whereas broadcast [message] and wait sends the message and then pauses the sending script until every script that received the message has finished running.

Q5 A shop project must ask for the price of one item in rupees and the quantity bought, add a fixed delivery charge of Rs 40, and display the total bill. Write the algorithm and the Scratch blocks. 5 marks

Algorithm

  1. Start.
  2. Read the price of one item into price.
  3. Read the quantity into quantity.
  4. Calculate total = (price multiplied by quantity) + 40.
  5. Display total.
  6. Stop.

Scratch blocks

when green flag clicked
ask [Price of one item in rupees?] and wait
set [price] to (answer)
ask [How many items?] and wait
set [quantity] to (answer)
set [total] to (((price) * (quantity)) + (40))
say (join [Total bill: Rs ] (total)) for 3 seconds

Points to note. The answer reporter is replaced by the next ask, so each answer must be copied into a variable immediately. The multiplication and addition blocks come from Operators, and join is used so the amount appears inside a sentence rather than on its own. Ticking the variable watchers on the stage makes it easy to check price, quantity and total while testing.

Part of Priodemy for School

Interactive Maths & Science — free with every school on Priodemy EduSuite. Explore more chapters and labs on the Priodemy for School hub.

Ask AI