Conditional Statement
Conditional statements allow the program to check the conditions by testing a variable against a value and acting accordingly. A program that has conditional statements is called a Conditional Program, and the process is known as Conditional Programming.
Relational Operators
A relational operator is an operator that tests a relation between two entities. The result of a relational operator is either true or false.
S No | Operator | Definition | Image |
1 | 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 a false.
Example: 15 > 10 will return true. 10 > 15 will return false. |
|
2 | 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 a false.
Example: 15 > 10 will return false. 10 > 15 will return true. |
|
3 | Equals to | It results in true only if the first number equals the second number; otherwise, it results in false. |
Activity 1: Gliding using if () then Block
Make a script that will make Tobi glide to a random position on the Stage whenever we press the space key.
Flowchart
Coding Steps
Follow the steps below:
- Open
PictoBlox App, go toMy Space, and click on the+button to create a new file. - Add the when flag clicked block.
- Add forever block below the when flag clicked block.
- Add an if () then block inside the forever block.
- Place a key () pressed? block from the
Sensingpalette in the space of the if block. - Place glide to () second block under the if arm.
- Click the green flag and press the space key to move Tobi around.
Activity 1: Output
Activity 2: Gliding Using if () then Else Block
Make a script that will make Tobi move toward the mouse cursor whenever we click it. When we’re not clicking it, he should go back to his default position in the center.
Flowchart
Coding Steps
Follow the steps below:
- Open
PictoBlox App, go toMy Space, and click on the+button to create a new file. - Add the when flag clicked block.
- Add forever block below the when flag clicked block.
- Add an if-else block and place a mouse down? block from the
Sensingpalette in the space of the if block. - If it is true, Tobi should glide toward the mouse. Therefore, add a glide () secs to x: () y: ()block and place mouse x and mouse y from the Sensing palette in their respective white spaces. Set time as 0.5 secs.
- 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. Set time as 0.5 secs.
- Click the green flag and click on the stage to move Tobi around.
- Save the file with the name
Conditional Programming.
Activity 2: Output
Logical Operator
A logical operator is an operator that is used to perform a logical operation on one or more Boolean (True/False) values. Common logical operators include AND, OR, and NOT.
AND Operator
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:
- Condition 1: Have you completed homework?
- Condition 2: Is the time past 8 PM?
- Decision: Should you go to bed?
Have you completed homework? | Is the time past 8 PM? | Should you go to bed? |
YES | YES | YES |
NO | YES | NO |
YES | NO | NO |
NO | NO | NO |
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 3: AND 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.
Coding Steps
Follow the steps below:
- Create a New file in
PictoBlox App. - Connect
QuarkytoPictoBlox. - Go to the
Sensorspalette and add a is button () pressed ? block into the scripting area. The button, L, is set as the default option. - Go to the
Sensorspalette and add a is button () pressed ? block into the scripting area. Change the button to, R. - Add () and () block, from the
Operatorspalette. Inside this block add both the is button () pressed ? blocks.
- Go to the
Controlpalette and drop an if else block into the scripting area. Drop the () and () block in the white space of the if else block. - Go to the
Displaypalette 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. - Add a forever block around the entire script.
- To add the finishing touch, add a when flag clicked block above the forever block.
- Finally, click the green flag to run the script. Now test the AND logic with Quarky switches.
- Save the file as
AND Logic.
Activity 3: Output
Logical Operator – OR Operator
The OR operator is used to determine if either one of two or more conditions is TRUE. If any of the conditions 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 the “||” 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:
- Condition 1: Is it sunny outside?
- Condition 2: Is it raining outside?
- Decision: Should we carry an umbrella?
Is sunny? | Is raining? | Carry umbrella? |
YES | YES | YES |
NO | YES | YES |
YES | NO | YES |
NO | NO | NO |
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.
Challenge
Make 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.
Logical Operators – NOT Operator
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, the operator is denoted by the “!” 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 go out to play?
Is it raining? | Go out to play? |
YES | NO |
NO | YES |
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.
Challenge
Make a code that basically detects whether the left button of Quarky is pressed or not. If any of the buttons is pressed then the display of Quarky will show red, otherwise green.
Make sure you finish and submit the assignment, as well as take the quiz, before moving on to the next lesson.