A relational operator is an operator that tests a relation between two entities. The result of a relational operator is either true or false.
There are 3 operators in PictoBlox:
- Greater Than >
- Less Than <
- Equals to =
Let’s have a look at each one by one.
Greater Than
This block reports true only if the first number is greater than the second number. If the first number is equal to or less than the second, it results in false. Example:
- 15 > 10 will return true.
- 10 > 15 will return false.
Less Than
This block reports true only if the first number is less than the second number. If the first number is equal to or greater than the second, it results in false.Example:
- 15 > 10 will return false.
- 10 > 15 will return true.
Equals to
It results in true only if the first number is equal to the second number; otherwise, it results in false.
Life is full of conditions: if you complete your homework, your mum will let me play, if you don’t, she will scold you; if you go to Domino’s on Wednesday you’ll get 50% off on the price, but if you go on any other day, you won’t get any discount, etc.
In the real world, you luckily have your brain to check what the condition is, whether it is met or not, and respond accordingly. But a computer isn’t that lucky, unfortunately (or fortunately?). Even though it can perform every task at lightning-fast speed, you still need to give instructions to perform those tasks; even checking the conditions.
This is where the conditional statements come in handy. These statements allow the program to check the conditions by testing a variable against a value and act accordingly. A program that has conditional statements is called a Conditional Program, and the process is known as Conditional Programming.
Conditional Blocks
- The if () then block will check whether the specified condition is true or not. If it is true, the blocks 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. The condition is checked only once.
- The if () then else block will check whether the specified condition is true or not. If the condition is true, the code held inside the first C (below the if arm) will run. Then, the script will continue; if the condition is false, the code inside the second C (below the else arm) will run. (Unlike the if () then block).
- The 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.
- In the forever block, the Blocks held inside 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.
- The block wait() seconds pauses its script for the specified amount of seconds — the wait can also be a decimal number.
Activity
Let’s make a script to see it in action. We’re going to make a script that will make Tobi glide to a random position on the Stage whenever we press the space key.
Let’s Code
Follow the steps below:
- Create a New file in PictoBlox:
- Follow the step if you are working on Windows, macOS, or Linux:
- Open PictoBlox and create a new file.
- Select the coding environment as Block Coding.
- Follow the step if you are working on Windows, macOS, or Linux:
Follow the steps below to make the script:
- Add an if () then block.
- Place a key () pressed? block in the space of the if block.
- Place glide () secs to () blocks under the if arm.
- Place a forever block around the if () then block.
- Add a when flag clicked block above to complete the script.
- Click the flag when done and press the space key.
if () then else Block
The if () then else block will check whether the specified condition is true or not. If the condition is true, the code held inside the first C (below the if arm) will run. Then, the script will continue; if the condition is false, the code inside the second C (below the else arm) will run. (unlike the if () then block).
Activity
Let’s make a script to see it in action. We’re going to make a script that will make Tobi move towards the mouse cursor whenever we click it. When we’re not clicking it, he should go back to his default position in the center.
Let’s Code
Follow the steps below to make the script:
- Add an if-else block and place a mouse-down? block in the space of the if arm.
- If it is true, Tobi should glide towards the mouse. Therefore, add a glide () secs to x: () y: () block and place mouse x and mouse y in their respective white spaces.
- If the condition is false, it will stay at the center. Thus, place another glide () secs to x: () y: () block and write set x and y value to 0.
- Add a forever block to run the script continuously.
- Add a when flag clicked block to run the script.
- Click the flag when done and press the mouse.
Save the project file as Working with Conditions.
Assignment
Before moving on to the next lesson, a small assignment awaits you!
You must upload the PictoBlox program you created in this activity to the website. Submitting the assignment is a must in order to receive the certificate after completing the course.
Follow the steps below to upload your assignment:
- Click on Browse.
- Search and Select your saved Project file(.sb3) and Click Open.
- Click on Upload to submit the assignment.
Good luck!