Scratch: What is Conditional Programming?
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.
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:
There are 5 conditional block in Scratch listed below:
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:
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 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.
For this project we will be using the Scratch Cat as the sprite.
Follow the steps below to write the script:
- Open mBlock; if already open and working on a project, save that project. Then, click on New.
- Import the Scratch Cat costume for the sprite from the Sprite Library.
- Drag and drop a hat block to start execution of script.
- Create three variables:
- Marks: a number
- Grade: a letter
- Flag: a Boolean
- Initialise all the variables. (Flag is initialised to zero, i.e. False)
- 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.
- 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.
- 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.
- Similarly, repeat step 8 for other grades.
Below is the complete script:
Click on the green flag to run the script.
Click on the red octagon, next to the green flag to stop the script.