Computational Logic

Learn about the 7 structures of computational logic.

Program hero image

In computational logic, operators define what a computer will do. Operators are symbols used to communicate to a computer what a programmer wants. 

Computers can only operate on basic instructions—instructions like performing math, comparing values, changing states, and storing and editing data in memory. Programming languages, operating systems, and software written by clever programmers in the past do a lot of heavy lifting for programmers today. For example, using JavaScript, we can tell a computer to draw a circle on the screen, instead of telling it to change its electrical signal to each pixel in order to eventually draw a circle. However, programmers (or coders) today still need to understand how computers “think” in order to use a programming language.

The more we learn to think precisely and logically, linking actions to specific instructions, the better we become at programming. Hatch Studio is a tool to learn how to solve problems step-by-step using operations—and symbolic operators like plus or minus signs—a computer can perform. Highlighted below are the different types of symbols used as operators.

Assignment Operators
 = Assignment
 += Addition Assignment
 -= Subtraction Assignment
Logical Operators
 && And
 || Or
 ! Not
Comparison Operators
 > Greater than
 < Less than
 == Equal to
 >= Greater than or equal to
 <= Less Than or equal to
Arithmetic Operators
 + Addition
 - Subtraction
 / Division
 * Multiplication
 % Remainder (Modulo)
 ** Exponent
 ++ Increment
 -- Decrement

Variables are words used to name information for later use. In other words, variables are code words.

A code word is used as a symbolic representation of a thing and it can represent a small amount or a large amount of information. The thing can be a complex or a simple idea. By creating and using code words (i.e. variables) programmers are able to express sometimes complex ideas in short order.

Let’s look at an example.

Thomas and Helen are a chocoholic couple. They have a 3 year old son: Eli. 

They want to talk about chocolate without Eli catching on.

They create a code word (a variable) for "chocolate".





Variable chosen as the code word for chocolate: 


As a code word, a variable can mean any number of things and can even change its meaning over time. Helen and Thomas could later decide that WINKS means something different: cake, cookies, etc.

When Helen now says to Thomas “Can you get some WINKS when you go to the grocery store?”,  Eli is none the wiser.

In computer programming, students can use variables, and the ability to change the variable, to make more powerful programs.

For instance, they could define a variable called “ballSize” to be the size of a circle in a game involving bouncing balls, and then change the value when the ball needs to get bigger or smaller. They could also define a variable called “rocketSpeed” when building a science simulation to measure the speed of a rocket, and then apply physics formulas in order to have an accurate simulation.

variable declaration might look like this: 

var correctAnswer = 256;

The variable name is correctAnswer and it is given a value of 256

The ball size example above might have the size of the ball equal to 10 on Level 1 of the game and then 9 on Level 2 of the game, etc. making every level harder. This shows off the power of programming because instead of having to redefine the size of the ball for every level, a programmer need only change one code word (ballSize) and the whole program is altered: Simple, logical, and powerful programming.


A group of instructions with a single name is called a function. The instructions, grouped together are, in fact, a series of multiple steps. Going back to Thomas and Helen (seen in Variables), they might tell their son Eli to “Get Ready for Bed.”

This could mean:

1. Put away your dinner dishes
2. Clean up your toys
3. Put on your pyjamas
4. Brush your teeth
5. Go pee
6. Pick out a bedtime book to read
7. Get into bed
var getReadyForBed = function(){

In reality, a computer would require more detail for every one of the aforementioned instructions. Putting dishes away, for instance, actually requires first getting up from the table, then picking up the dishes, then carrying them over to the counter, and so on.

For our purposes, let us assume that the function getReadyForBed() includes just the 7 instructions enumerated here. As it is meant to lay out a series of actions that lead from dinner to bedtime, the function getReadyForBed() could be used 7 days a week without needing to list all 7 instructions.

As in real life, functions in computer science are usually groups of instructions that are used multiple times in a program. You might have a function in a game called “Check Collision” that checks to see if the hero has collided with the enemy. You could have a function in a rocket simulation that checks for the effect of gravity. You could have a function in a tax application that does a set of monthly financial calculations that need to be done multiple times.

Ways in which functions are useful in coding:

· Making code readable by chunking groups of instructions
· Helping to figure out exactly where in the code base there are errors 
· Decomposing (breaking down) large problems into smaller, more manageable, chunks, each one of which gets solved in a single function


A loop is a type of structure in which a set of instructions can be repeated multiple times. There are 3 basic types of loops: while, do, and for… next.

While loops are run by the computer until a condition is met. As an example, going back to our example above of Thomas, Helen, and their son Eli, the while loop seen below would have Thomas ask Eli to brush his teeth until he does so; once the condition is met, Thomas would stop asking the question.

A while loop might be run zero times, for example if Eli has already brushed his teeth.

Example of a while loop:

while (Eli hasn’t brushed teeth) {

A do loop also gets run until a condition is met, but it is run a minimum of one time.  

Example of a do loop:

do {


while (RoomIsMessy);

In this case, Eli is going to clean his room at least once, but if he does it well, the room won’t be messy and the loop will end.

For loops (the common way of referring to for… next loops) get run a specific number of times.

For loops can also provide a solution to the pesky getReadyForBed() problem that Thomas and Helen have been trying to address with Eli.

With a for loop, we can create a loop for the getReadyForBed() function, to tell Eli:

Starting on Monday, and ending on Sunday, every evening please run getReadyForBed().

for (weekday = Monday; go forward one day; end on Sunday) {


Because we want getReadyForBed() to run every evening, it is a loop that runs 7 times. We need to convey that information to the computer. We also need to convey the specific starting conditions (the first day is Monday), how we move forward (go forward one day) and the specific ending conditions (the last day is Sunday). It should then repeat exactly: a loop.

Beyond a basic loop, parents such as Helen and Thomas could create a nested loop in order to run multiple sets of repeatable functions.

Nested loops can be created with all three types of loops. The three types of loops can be nested (e.g. layered) interchangeably. The only factor limiting the number of layers that can exist in a nested loop is the constraint created by a shortage of computer memory; it takes a lot before a computer hits that limit.

For busy parents like Thomas and Helen, nested loops are a great way to have multiple sets of repeatable functions executed without needing to rewrite extensive amounts of the same code over and over again.

Here is an example of a nested for loop, but remember: all three types of loops can be nested interchangeably.

#1—1st, outer layer of loop:
Starting the first week of January, and ending the last week of December, run this weekly loop:

#2—2nd, inner layer of loop:
For every week of the year, starting on Monday, and ending on Sunday, every evening please run getReadyForBed.

for (week  = 1st week in January; go forward one week; end the last week in December) {
    for (weekday = Monday; go forward one day; end on Sunday) {
for (weekday = Monday; go forward one day; end on Sunday) {

The result:
A) The outer loop runs 52 times, one for every week of the year.
for (week  = 1st week in January; go forward one week; end the last week in December) {

B) The inner loop runs 7 x 52 times, one for every day of the week, and then for every time through the outer loop, i.e. every week of the year.
    for (weekday = Monday; go forward one day; end on Sunday) {

With this simple computational structure, young Eli gets one instruction and the parents get a whole year of easy bedtime routines starting in the first week of January. That’s the power of loops.

(If only parenting were so easy!)


Conditionals are a structure with 2 parts: the condition and the result if the condition is true. Optionally, you can also specify a the result if the condition is false.

Going back to our intrepid family (Helen, Thomas, and little Eli), we see that every morning Eli needs to get ready to go to school. There is an implicit conditional that goes like this:

IF (Eli changes his clothes AND eats breakfast, AND brushes his teeth) THEN he goes to school ELSE he has to go back and do these 3 things until they are done and his parents are late getting to work.

if (clothesChanged == true
 && breakfastEaten == true
 && teethBrushed == true) {

 } else {
    statusOnTimeForWork == LATE;

There is another outcome which is that Eli goes to school without these 3 things and his parents get to work on time, which is an outcome that all parents have had to grapple with. But that’s a different type of logic for a different example.

In reality, the conditional is a branching structure—one path for when the condition is true and one path for when the condition is false. The path could itself be a condition. In the middle of the above condition, there could be a check on what Eli is wearing:

IF (Eli’s clothes are appropriate) THEN he gets to wear them ELSE he has to pick a different outfit.

if (clothesAppropriate == true){
} else {


In computer programming, conditionals can be used to check error conditions (eg: did someone try to move to the 9th column of a chessboard when there are only 8 columns?), the accuracy of math (eg: does the angle of the rocket launch equal the sum of the rocket fuel plus the slowing effect of gravity?) or any other situation where the computer needs to check something to make sure the instructions are going to result in the end outcome that the user wanted.

Computers are very efficient at running true / false checks, so part of the power of conditionals is being able to use them inside a loop so that a large set of items (e.g. all the prices of all the items at are checked for something specific (e.g. Is any price over $100,000 and if so, flag it for review to confirm that the price was set properly).


Arrays are another structure of computational logic; used to communicate human thoughts to a computer.

Thomas and Helen have discovered that Eli really likes sports. So they buy a big storage box and put all the balls that Eli has and plays with inside the box: soccer balls, bouncy balls, basketballs, baseballs, beach balls, etc.

var balls = [soccerball, bouncyball, basketball, baseball, beachball];

This storage box is very similar to an array of balls. It could be organized in different ways, e.g. the bigger balls on the left, the smaller balls on the right. It can be searched for the right type of ball, e.g. today Eli is playing tennis, so he needs a tennis ball. It can be expanded (we need a bigger box to hold the expanding collection) or shrunk.

Another way of thinking about an array is that it is like an expandable shoe closet with a cubby for every piece of data you want to store.

In computer science, an array can hold a storage box of numbers, letters, words, or other elements or items that are similar and that need to be organized or processed as part of a program. An array might be a high score list for a video game, the list of names that are part of a social network or the monthly revenue totals for a company’s sales numbers as part of a database application.

Arrays are one of the most complex instruction giving category of computational logic structures that appear in almost every computer language. They are surpassed in complexity only by objects.

Arrays are complicated because they involve more levels of abstraction in their presentation—having a collection of things involves thinking about how those things are organized and stored, how they can be manipulated and how they look after every manipulation. Ultimately, arrays require that a computer programmer engage in meta awareness (awareness about awareness) in order to have elegant cause and effect work.

Mastering arrays and array manipulation (e.g. changing the order of the items inside an array, removing specific items from an array, etc.) is a core tool that any decent programmer has in their arsenal.

The reason why arrays are so useful is that we can put almost any data type into an array, and then we can run sets of instructions on every element inside the array. We can also have an array of arrays, which is like having a collection of shoe closets. An example of this would be to have monthly sales numbers for every region or city that you sell to. Each array holds a set of 12 sales numbers for a specific geographic area, and collectively you can store and manipulate all the sales numbers for all the geographic areas in one fell swoop.

Arrays are key to the expression of organized thought.


An object is a way of describing multiple items as a single data type. Let's take fashion as an understandable example.

Thomas, Helen, and their son Eli are a super organized family. They have planned an entire year of clothing options and outfits. Every outfit includes a number of items, which they think of as an ensemble.

Here is a workday outfit for Thomas:

var outfit = {
   pants: jeans,
   top: collared shirt,
   socks: navy,
   shoes: loafers

We can now think of an outfit including its component parts. If the dress code where Thomas works changes (say he gets a new job at a fancy bank), he can change his workday outfit in the computer program just by changing one part of it, but leaving the rest the same:

outfit.pants = slacks

Now, the computer program shows that pants are no longer jeans, but rather that pants are slacks. Nothing else changes.

The family can also become more organized by creating a prototype that acts as a function and describes all outfits, called in official computer speak an object prototype:

function outfit(top, pants, socks, shoes) { = top;
   this.pants = pants;
   this.socks = socks; = shoes;

This type of object is much more powerful because it is much more flexible. With an object prototype function, a programmer can quickly create new outfits for the entire family by inserting new (appropriate) values for the component parts of the function. In other words, new outfits can be quickly created with specific items inserted in the right place. The programmer simply needs to remember that within the object prototype function, there is correct order or sequence of variables. In our example, an outfit contains four variables: #1 must be a top, #2 must be pants, #3 must be socks, and #4 must be shoes. A programmer can assign a value to a variable to indicate that it is not being used, e.g. typing “none” for socks when no socks are desired.

This is how it looks in JavaScript:

var HelenSummer = new outfit (“blouse”, “skirt”, “none”, “flip-flops”);
var EliSoccer =  new outfit (“NikeJersey”, “Soccer Shorts”, “Soccer Socks”, “Soccer Cleats”);
var ThomasWeekend =  new outfit (“relaxed t-shirt”, “chinos”, “ankle low cut”, “sneakers”);

Additional fashion elements can be added, e.g. when it gets colder, sunnier or we want more information about the outfit:

outfit.prototype.winterCoat = “Canada Goose”;
outfit.prototype.sunglasses = “Ray-Bans”
outfit.prototype.waistsize = 32;

All the other parts of computer logic (operators, variables, functions, loops, conditionals, and arrays) can be used to customize outfits any way that is desired.

In other words, different types of computer logic can be combined to deliver powerful end results.

For example:

1. Use loops with a conditional to go through all outfits, look only for Eli’s outfits, and then change every one of his outfits to be a bigger size as Eli grows older.

2. Use arrays to organize the outfits into different boxes, e.g. seasons, or days of the week.

3. Use loops and conditionals to go through the summer outfits and check to see if any of them accidentally have socks, and if so, set the value of the socks variable to “none” because it’s summer and it’s hot. Then go through the winter outfits and do the reverse, to make sure you never go outside in snow without a warm pair of socks to keep your feet warm.

Etc. etc.

The examples here are just the tip of the iceberg of the power of objects. Using objects in the structure of a website such as Amazon provides enormous flexibility allowing the website to describe an unlimited number of store items, to quickly change prices, to list items related to the item a shopper is looking at, to offer customer reviews for all items and all the other things users can see and do while shopping there.

Learn about Computational Thinking

Computers are not able to think in the same way that humans do. Computers understand in a more linear way and learning how to think like a computer is the essence of Computational Thinking. Learn more about this by exploring the Computational Thinking page.