Lesson 6 : Conditional Statements

Lesson Description

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. Similarly, every day we take many decisions depending on our situation. In this lesson, we will learn about logical operators. Create an activity to make a script that will make Tobi glide to a random position on the Stage whenever we press the space key and another activity 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.

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:

  1. Greater Than >
  2. Less Than <
  3. 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.greater than 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.less thanExample:

  • 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.equal to

 

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

  1. 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.
  2. 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).
  3. 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.
  4. 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.
  5. 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.

Moving tobi to random position

Let’s Code

Follow the steps below:

  1. Create a New file in PictoBlox:
    1. Follow the step if you are working on Windows, macOS, or Linux:
      1. Open PictoBlox and create a new file.
      2. Select the coding environment as Block Coding.

Follow the steps below to make the script:

  1.  Add an if () then block.
  2. Place a key () pressed? block in the space of the if block.Making Tobi Glide
  3. Place glide () secs to () blocks under the if arm.
  4. Place a forever block around the if () then block.
  5. Add a when flag clicked block above to complete the script.Making Tobi Glide
  6. Click the flag when done and press the space key.Moving tobi to random position

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

if () then else

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.

Gliding Tobi towards mouse direction

Let’s Code

Follow the steps below to make the script:

  1. Add an if-else block and place a mouse-down? block in the space of the if arm.Making Tobi Glide with mouse pointer
  2. 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.Making Tobi Glide with mouse pointer
  3. 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.
  4. Add a forever block to run the script continuously.
  5. Add a when flag clicked block to run the script.Making Tobi Glide with mouse pointer2
  6. Click the flag when done and press the mouse.

Gliding Tobi towards mouse direction

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:

  1. Click on Browse.
  2. Search and Select your saved Project file(.sb3) and Click Open.
  3. Click on Upload to submit the assignment.
evive Alert
The file type allowed is the sb3 file, generated from the PictoBlox program. The maximum file size allowed is 5 MB.

Good luck!

 

Table of Contents