Conditional Statements in Scratch

Conditional or If-then relationships allow the computer to pick between two options. This kind of selection statement is extremely important.

If-then statements perform an action if the condition is true.  If-then-else statements perform an action if the condition is true and perform a different action if the condition is false

Boolean operators allow us to determine whether a condition is true or false. There are many kinds of boolean operators in Scratch. These diamond-shaped blocks will either return true or false

Comparison Boolean Operators (with Numbers): 

These blocks compare two numbers and
Return either true (when you have a valid
statement) or false (non-valid statement).

For example, 1 > 2 would return false and 2 > 1 would return true.

State whether the operator returns true or false:
(Write answers here)

Conditionals + Comparison Boolean Operators: These two block types can be put together to create if-then relationships with outputs. Please write the printed output (write “nothing” if there is no output) for the code sequences below. 

Boolean Logic: Boolean Logic allows us to combine two or more boolean operators to create more complicated conditions. Scratch supports three basic boolean logic blocks. 

AND Gate: Both inputs need to be “true” in order to return a “true”

OR Gate: One or both inputs needs to be “true” in order to return a “true”

NOT Gate: Automatically switch the value. True -> False | False -> True

Truth Tables: Boolean Logic can be represented by “truth tables.” These present all possible inputs and provide all possible outputs. 
Given: Truth Tables for OR (downward arrow), NOT (double-headed arrow), and AND (upward arrow) (in that respective order).

Comparison Boolean Operators (with Strings): We have a boolean operator that can check for letters in a string.
Write an answer here.

Keyboard Sensing: You can also create controls based on keyboard booleans which check whether a key is pressed (true result) or not pressed (false result).

What prints when I press “w”:

What prints when I press “a”:

What prints when I press “s”:

What prints when I press “d”:

The “forever” loop: This allows us to keep checking for different conditions and seeing if they are true. Without the forever loop, the flow of control (sequence of code) would run once and be done. This would be very quick, and we wouldn’t be able to press a key in time


Create a variable, set it to a random number 1-3 and use comparison boolean operators and conditionals to print out a color associated with that number (you may select these combinations yourself).

Search the string “I love Scratch” for the letter “s” with an if-then-else statement. Utilize printing to see whether the letter was found. 

Write a calculator script, taking in three user inputs (two numbers and the operation denoted by a sign +, -, *, /) and then printing out the solution to the problem based on the given sign. 

Initialize (a.k.a. create) a variable to represent a number line. Write a program that continuously (i.e. surrounded by a for loop) allows me to either move up (+1) or down (-1) a number line (represented by the var #).

What is the result of these (try them out!):

Advanced: In chemistry, hydrogen bonds only occur when in elements that contain both hydrogen (H) and nitrogen (N), both hydrogen (H) and oxygen (O), or both hydrogen (H) and fluorine (F). Given two compounds through user input, print whether they will be able to form hydrogen bonds. This can only occur if they both meet the conditions stated before (having both H and N, H and O, or H and F).

No comments:

Post a Comment