0% found this document useful (0 votes)
109 views

Scratch What Is Conditional Programming

This document discusses conditional programming in Scratch. It explains that conditional statements check if a condition is true or false and allow a program to execute different code depending on the result. The document provides examples of how conditional logic can be used to check test scores and assign a letter grade. It also lists the different conditional blocks available in Scratch, such as if/else blocks. Finally, it presents a sample Scratch project that uses conditional logic to prompt a user for their exam score and output their grade based on a provided grading scale.

Uploaded by

dguruge8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Scratch What Is Conditional Programming

This document discusses conditional programming in Scratch. It explains that conditional statements check if a condition is true or false and allow a program to execute different code depending on the result. The document provides examples of how conditional logic can be used to check test scores and assign a letter grade. It also lists the different conditional blocks available in Scratch, such as if/else blocks. Finally, it presents a sample Scratch project that uses conditional logic to prompt a user for their exam score and output their grade based on a provided grading scale.

Uploaded by

dguruge8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Scratch: What is Conditional Programming?

thestempedia.com/tutorials/conditional-programming/

Introduction
Conditional statements check whether a programmer-specified Boolean condition is true
or false. They make it possible to test a variable against a value/compare a variable with
another variable and make the program act in one way if the condition is met, and
another if it isn’t. They make the program very powerful and be able to be used for a vast
variety of purposes, from creating simple calculators to controlling robots. A program that
has conditional statements is called a Conditional Program, and the process is known as
Conditional Programming.

For example, if a bulb is not working, you first check whether the switch is ON or OFF. If
the switch is OFF you turn on the bulb, but if the switch is ON and bulb is not working,
you replace the bulb or check connection. You act according to the situation or condition.

You can represent the same thing using a flowchart:

Flowcharts are used while designing and documenting simple processes or programs.
They help visualise what is going on and thereby help understand a process, and
perhaps also find flaws, bottlenecks, and other less-obvious features within it. It is
recommended to make a flowchart for every project before you start writing the script.

1/7
Before going further let us look at some important operators which return Boolean result
and play the role of conditional statement. Below is the list and function of the operators:

Block

Function

The block checks if the first value is less than the second value. If it is less, the
block returns true; if not, it returns false. This block works with letters too, as
well as numbers. In Scratch, letters at the top of the alphabet (e.g. a, b, c) are
worth less than letters at the end (e.g. x, y, z).

The block checks if the first value is equal to the other value. If the values are
equal, the block returns true; if not, false. This block is not case-sensitive.

The block checks if the first value is greater than the other value. If the second
value is less, the block returns true; if not, it returns false.

The block joins two Boolean blocks so they both have to be true to return true.
If they are both true, the block returns true; if they are not all true or none true,
it returns false.

The block joins two Boolean blocks so any one of them can be true to return
true — if at least one of them is true, the block returns true; if neither of them
are true, it returns false.

The block checks if the Boolean inside it is false — if it is false, the block
returns true; if the condition is true, it returns false.

There are 5 conditional block in Scratch listed below:

Name Block Function

2/7
Name Block Function

Repeat
Blocks held inside this block will loop a given amount of
times, before allowing the script to continue.

If a decimal is put in, the number is rounded up.

Forever Blocks held inside this block will be in a loop — just like
the Repeat () block and the Repeat Until () block, except
that the loop never ends (unless the stop sign is clicked,
the Stop All block is activated, or the stop script block is
activated within the loop). Due to this infinite loop, the
block has no bump at the bottom; having a bump would
be pointless, as the blocks below it would never be
activated.

If-Then The block will check its Boolean condition. If the condition
is true, the blocks held inside it will run, and then the
script involved will continue. If the condition is false, the
code inside the block will be ignored and the script will
move on (unlike in the If () Then, Else block). The
condition is only checked once; if the condition turns to
false while the script inside the block is running, it will
keep running until it has finished.

If-Else The block will check its Boolean condition: if the condition
is true, the code held inside the first C (space) will
activate, and then the script will continue; if the condition
is false, the code inside the second C will activate (unlike
the If () Then block).

Repeat Blocks held inside this block will loop until the specified
Until Boolean statement is true, in which case the code
beneath the block (if any) will execute.

Project
To understand the concept of conditional programming we will make a small project.

Problem: You have received the marks of your final exam and you want to assign
yourself a grade according to the marks scored in the exam. The grading system is given
below:

Marks Grade

81-100 A

61-80 B

3/7
Marks Grade

41-60 C

33-40 D

0-33 E

Solution: We will first make a flowchart and then proceed to programming in Scratch.

To store the marks and the grade, we will need two variables named Marks and Grade.
From the flow chart you can observe that we want to run the program till the time the user
enters valid marks, i.e. between 0 and 100. To keep track whether the user has entered

4/7
valid marks or not we will need another variable Flag, which is a Boolean. All the three
variables must be initialised at the beginning of the script.

Scratch Script
For this project we will be using the Scratch Cat as the sprite.

Follow the steps below to write the script:

If you’re working with evive in Scratch mode for the first time, or have uploaded the
Arduino firmware previously, first upload the Scratch fimware; you can find the procedure
to do the same here.
1. Open mBlock; if already open and working on a project, save that project. Then,
click on New.
2. Import the Scratch Cat costume for the sprite from the Sprite Library.
3. Drag and drop a hat block to start execution of script.
4. Create three variables:
1. Marks: a number
2. Grade: a letter
3. Flag: a Boolean
5. Initialise all the variables. (Flag is initialised to zero, i.e. False)
6. As discussed, we want to keep asking the user to enter marks until it is valid. For
this purpose, the repeat until () block is useful. The variable Flag will be used to
determine whether the marks entered are valid or not. In the beginning, when no
input is entered, Flag should be zero, i.e. false, as the code inside the block should
execute at least one time.
7. Once we get inside the loop, we will ask the user to enter the marks and save it in
the Marks variable, similar to as done in the tutorial on Variables and Operator
blocks.
8. Now we must check in what range the marks are. So, we will use another if block
and other conditional blocks to check the range. For example, let us check is if
Marks are less than 33. Using () < () (less than) operator block we can check
whether the condition is true or false. If the statement is true, we will set grade to E,
inform about it to the user, and change Flag to one, i.e true.
9. Similarly, repeat step 8 for other grades.

Below is the complete script:

5/7
Click on the green flag to run the script.

6/7
Click on the red octagon, next to the green flag to stop the script.

7/7

You might also like