Conditional programming is performing different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false. This is the ability to test a variable against a value and act in one way if the condition is met by the variable or another way if not . For example, if the bulb is not working, you check if 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. Hence according to the situation or condition you react. Same thing you can represent in a flowchart:
Flowcharts are used in designing and documenting simple processes or programs. They help visualize 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 script.
But before going further let us look at some important operators which returns Boolean and play the role of conditional statement. Given 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 your marks in exam and you want to assign yourself grade you will get in exam according to the marks scored in the exam. For this project use Scratch Cat as sprite. The grading system is given below:
We will first make a flowchart and then proceed to programming in Scratch.
To store marks and grade, you will two variables named “Marks” and “Grade”. From the flow chart you observe that you want to run the program until a valid marks is input by the user. By valid you mean a number between 0 and 100. To keep track whether the user have entered a valid marks or not you will need another variable “Flag”, which is a Boolean. All the three variables must be initialised in the beginning of the script.
Follow these steps:
Now your script is ready and should look similar to the script shown in the figure. Click on the green flag and check the script.
Given below is one of the example: