Lesson 10 : Logical Operators

Lesson Description

As of now, students have mastered sprites, backdrops, the Stage, drawing, animating, and creating variables. But PictoBlox is not just about drawing and animating; it’s much more than that. For starters, students can perform mathematical and logical operations using it. In this lesson, students will learn about different types of operators and their use in programming and create activity to showcase the working of logical operators with the help of Pictoblox and Quarky robot.

AND operator is used to determine if two or more conditions are true. If all the conditions are true, the AND operator returns TRUE. If any one of the conditions fail, the AND operator returns FALSE.

In some programming languages AND operator is denoted by the “&&” symbol.

Real-Life Example

You should go to bed only after you have completed your homework and the time is past 8 PM.

Here, if we want to derive the logical operation from this scenario, we have the following conditions:

  1. Condition 1: Have you completed homework?
  2. Condition 2: Is the time past 8 PM?

And the decision we are deriving is: Decision: Should you go to bed?

Pseudocode

Based on this we can write the below pseudo-code:

IF (Homework completed) AND(Time is past 8 PM)
THEN
  Go to bed
ELSE
  Do not go to bed
END

Truth Table

Let us now try to see the different combinations possible with the above pseudo-code.

[table id=1 /]

AND Block in PictoBlox

The () and () Block is an Operators Block and a Boolean Block.

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 only one is true or none are true, it returns false.

Activity: AND with Quarky

Let’s use this block with Quarky.

We are making a code that basically detects whether both the buttons of Quarky are pressed or not. If both buttons are pressed then the display of Quarky will show green, otherwise red.

Quarky Button Inputs

is button () pressed ? block from the Sensors palette provides the state of the selected.

If the button is pressed, then it shows True, and if the button is not pressed, then it shows False.

Lets Code

evive Alert
Alert: Make sure that your Quarky is connected to PictoBlox either using Bluetooth or USB.

Follow the steps below to make the script:

  1. Go to the Sensors palette and add a is button () pressed ? block into the scripting area. The button, L, is set as the default option.
  2. Go to the Sensors palette and add a is button () pressed ? block into the scripting area. change the button to, R, is set as the default option.
  3. Add () and () block, from operator palette. Inside the block add both the is button () pressed ? blocks.
  4. Go to the Control palette and drop an if else block into the scripting area. Drop the () and () block in the white space of the if else block.
  5. Go to the Display palette and drop the display matrix as () block inside the if branch and make all LEDs green. Repeat to add display matrix as () block inside the else branch and make all LEDs red.
  6. Add a forever block around the entire script.
  7. To add the finishing touch, add a when flag clicked block from the Events palette above the forever block.
  8. Finally, click the green flag to run the script.

The OR operator is used to determine if either one of two or more conditions is TRUE. If any of the condition is true, the OR operator returns TRUE. If all the conditions fail, the OR operator simply returns FALSE.

In some programming languages OR operator is denoted by “||” symbol.

Real-Life Example

We should carry an umbrella when either it is sunny, or it is raining. Otherwise, we should not carry it.

Like the previous example, if we want to derive the logical operation from this scenario, we have the following conditions:

  1. Condition 1: Is it sunny outside?
  2. Condition 2: Is it raining outside?

And the decision we are deriving is: Decision: Should we carry an umbrella?

Pseudocode

Based on this we can write the below pseudo-code:

IF (It is sunny outside) OR (It is raining outside)
THEN
  Carry an umbrella
ELSE
  Do not carry an umbrella
END

Truth Table

Let us now try to see the different combinations possible with the above pseudo-code.

[table id=2 /]

OR Block in PictoBlox

The () or () (logical OR) block is an Operators block and a Boolean block. The block joins two Boolean blocks and compares them. If at least one of them is true, the block returns true; if neither of them is true, only then does the block return false. This block is used for checking if any of the conditions are true.

or operator

Activity: OR with Quarky

Let’s use this block with Quarky.

We are making a code that basically detects whether any of the buttons of Quarky are pressed or not. If any of the buttons is pressed then the display of Quarky will show green, otherwise red.

 

We use the NOT operator to reverse or negate a condition. If the condition is true, NOT will return false and vice versa. In some programming languages NOT operator is denoted by “!” symbol.

Real-Life Example

For example, you can go to play only if it is not raining, otherwise, you must stay indoors. Unlike the previous examples, here we have only one condition.

Condition: Is it raining?

Decision: Should we carry an umbrella?

Pseudocode

Based on this we can write the below pseudo-code:

IF NOT (It is raining)
THEN
  Go out to play
ELSE
  Stay indoors
END

Truth Table

And the corresponding table for this is below.

[table id=3 /]

NOT Block in PictoBlox

The not () (logical NOT) block is an Operators block and a Boolean block. The block checks if the Boolean block inside it is false. If it is false, the block returns true; if the condition is true, it returns false. This block can be used to “reverse” Booleans.

not operator

 

Activity: NOT with Quarky

Let’s use this block with Quarky.

We are making a code that basically detects whether the left buttons of Quarky are pressed or not. If any of the buttons is pressed then the display of Quarky will show red, otherwise green.

Assignment

Before you move 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