– Logic

A very large part of how a program works is based on logic.

Logic helps our robot work with data and how to interpret it though branching and looping, but we also use this same logic in general thinking and decision making.

Logic deals with:

  • Truth Values

and uses the following operations:

  • And [ && ]
  • Or [ || ]
  • Not [ ! ]

Truth Values

In Logic, we deal with whether something is true or not. We call these “truth-values.” Sometimes we write T for true and F for false. In programming and talking with computers we use the number 1 for true and the number 0 for false.

And [&&]

Your first logic, or Boolean operation is the “and” operation.

It works a lot like how we use “and” in English:

The “and” is true only if both parts are true. For example:

If I were to say, “I am a alive and I have three eyes.” This would be false because, although I am alive, I have only two eyes. In order for the whole statement to be true both parts have to be true.

What I will show below is what is called a Truth Table. It shows all possible truth values for a given statement or combination of statements.

Let A be one statement and B be another; remember 1=true and 0=false;
“and” is written as && in C.

A B A&&B
1 1 1
1 0 0
0 1 0
0 0 0

Or [||]

Or is the second logical operator, and it almost works like in English. In English “or” is used like a choice, you can have cake OR you can have pie, implying that you can’t have both. In Logic “or” is a bit more fun, you can have both!

In other words, an “or” statement is true when any of its parts are true. It is false only when both parts are false. It’s sort of an opposite of “and,” in a way.

Here’s a truth table for “or” that you can compare with that of “and.”

Let A be a statement, B be another statement, 1 be true, 0 be false, and || be “or”

A B A||B
1 1 1
1 0 1
0 1 1
0 0 0

Not [!]

“Listening to long and dry lectures is fun – NOT!!”

That catch-phrase was very annoying when it was in its heyday, but it clearly shows us what “not” does. It takes the truth value of a statement and reverses it. If A is true, then not-A is false. For example: “I am alive,” is true, so “I am not alive,” is false. In English we move the “not” to where it fits with how we speak. In Logic, we usually put it right out front.

In programming we use the exclamation point ! for “not.” [if you ever hear a programmer saying “bang! something something bang! bang! something” they are referring to the ! symbol as “bang!”]

Here’s a simple truth table to show the effects of “not”:

A is a statement, 1 is true, 0 is false, and ! is “not”:

A !A
1 0
0 1

How do we tie this into programming? That’s where branching and looping come in, but before we can use those, we must learn about Variables and  Testing Statements.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s