Introduction

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:

 

Flow Chart Bulb

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:

NameBlock

Function
Less Than

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).

Equals to

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.

Greater than

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.

AND

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.

OR

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.

NOT

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:

NameBlockFunction
RepeatBlocks 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.
ForeverBlocks 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-ThenThe 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-ElseThe 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 UntilBlocks held inside this block will loop until the specified 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 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:

MarksGrade
81-100A
61-80B
41-60C
33-40D
0-33E

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

Flow Chart grade

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:

  1. Start a new project.
  2. Import Scratch Cat costume for the sprite.
  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 False)
  6. As discussed you want to keep asking the user to enter marks until it is valid. For this kind of purpose, the Repeat until block is useful. Flag will be used to determine whether valid marks have been entered or not. In the beginning, when no input is entered, flag should be false, as the code inside the block should execute atleast one time.
  7. Once you get inside the loop, you will ask the user to enter the marks and save it in “Marks” variable; just the same way we did in the previous chapter.
  8. We will now check in what range the marks lie.
    So, we will use another if block and use conditional blocks to check the range.
    For example, let us check is if Marks is less than 33. Using Less than operator block, we can check whether it is true or false. If the statement is true, we will set grade to ‘E’, say the grade to the user and change Flag to true.
  9. Similarly make “If” blocks for other grades.

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:

Grades GIF