Home

Teaching Kids to Code Compound Conditionals (aka, AND, NOT, and OR)

|
|  Updated:  
2018-06-29 17:29:15
Getting Started with Coding
Explore Book
Buy On Amazon
As your young coder constructs his programs, he uses logic operations to make decisions about which code needs to execute, and under which conditions. The if-then statements he wants to construct may need to consist of more than one condition — they may need compound (multiple) conditions to execute the consequence. The main logic operations your coder uses to create compound conditional are and, not, and or. Here is what each one means:
  • and means that every condition is true.
  • not changes the Boolean value of a statement: true becomes false, and false becomes true.
  • or means that at least one of the conditions is true.

In pseudocode

Here is the general pseudocode format of a compound conditional:

if homework is done and it's after 9pm then go to bed

The if portion of a compound conditional can only return a Boolean value: true or false.

Here are some examples of compound conditionals in pseudocode:

if (weight >= 40 lbs and height >= 38 in) then (use booster seat)

if (not fuse tripped) then (electrical current flows)

if (SAT > 1500 or ACT > 33) then (earn scholarship)

Compound conditionals in Scratch

Your coder may want to construct a Frogger-style video game, and needs to control the game action based on conditions of the game at different times. Here’s how your coder can write code for logic operations in his game.

And

The image below shows a portion of the game using the and operation in Scratch. The frog object has three variables — lives, timer, and y-position. When the green flag is pressed, the values of lives, timer, and y-position are set. The say command announces the Boolean value of the logic operation

lives > 0 and timer > 0

and command coding The and command executes when all conditions are true.

Because both statements are true, the frog announces true.

Not

What you see below shows a new version of the game using the not operation in Scratch. When the green flag is pressed, the values of lives, timer, and y-position are set. The say command announces the Boolean value of the logic operation

not touching car

not command coding The not command changes the Boolean state.

The value of touching car is false, so the value of not touching car is true. The frog announces true!

Or

The image below shows another version of the game demonstrating the or operation in Scratch. When the green flag is pressed, the values of lives, timer, and y-position are set. The say command announces the Boolean value of the logic operation

touching beetle or touching butterfly

or command coding The or command executes when at least one condition is true.

The value of touching beetle is true, and the value of touching butterfly is false. So the frog announces true!

Check out the image below to see a slightly altered version of the game conditions involving the or command. In this case, the beetle is no longer caught by the frog's tongue and they aren’t touching. The value of touching beetle is now false, and the value of touching butterfly is also false. So the frog announces false.

or command coding The or command yields false when none of the conditions is true.

In Python

For young coders working in Python, compounding logical operators allow for structuring more precise control in their programs. For example, a program may be structured to compute different paychecks for people with different salaries, bonuses, and other variables. The table below shows the logical operators your young coder uses in Python.

Logical Operators in Python

Operator What It Means
== equivalent to
and and
not not
or or
The image below shows an example of code snippet that uses logical operators in Python. The example shows code that computes different paychecks for people with different salaries, bonuses, and other variables. The code references three variables: pay, salary, and bonus, where the current value of each is pay = 50000, salary = 40000, and bonus = 10000. The code and its execution are created in pythontutor.com.

Python logical operators Python logical operators are shown in this code snippet and execution, using pythontutor.com.

In JavaScript

Coders can also use logical operators in JavaScript to control the execution of their programs. This table shows the logical operators your young coder uses.
Logical Operators in JavaScript
Operator What It Means
== equivalent to
&& and
! not
|| (double pipes) or

In Java

When programming in Java, young coders can — with a few exceptions — use the same logical operators shown in the table above. A key exception your coder needs to know is how to compare objects. The equality of objects should not be tested using the == operator. Instead, when trying to determine equivalence between the object values, use the .equals() method. Here is an example:

name.equals("Speedy Gonzales")

If the two objects are equal, the equals method returns true. Otherwise, it returns false.

About This Article

This article is from the book: 

About the book author:

Sarah Guthals, PhD, co-founded an ed-tech company and now continues to build technology for kids to learn, create, and share safely online. She loves to teach teachers how to teach coding in the classroom.

Camille McCue, PhD, is Director of Curriculum Innovations at the Adelson Educational Campus in Las Vegas where she leads the Startup Incubator, teaches STEM, and kickstarts K-12 learning initiatives.