What is code?

Code is a set of instructions a computer follows. It’s what happens between some input (like a mouse click) and an output (like whatever is happening on your screen). For example, imagine you scroll down a webpage. The scroll action is the input, the instruction the computer follows is something like “move everything up,” and the output is the screen showing you the next part of the page.

So when you type in your name... the computer can reply.

First name:

Many things that you experience every day are made up of complicated code, from apps and websites, to stoplights and alarm clocks. However, all of it can be broken up into smaller pieces of programming logic. If you think about the logic (or instructions) a stoplight follows, part of it is as simple as “turn green off and turn yellow on... wait 5 seconds… turn yellow off and turn red on.”

Look at the clock app on your phone and you’ll probably see several different functions that it has: alarm clock, timer, etc. We can look at the timer specifically and try to figure out how it works. The first piece is that it has an input and an output. You input the time, and it starts counting down.

But the question is... what’s happening inside? What does code look like?

There are many different languages, some written, some made up of movable blocks. They all have different syntax, which a set of rules you have to follow when writing it, like grammer. In pseudocode (pseudocode is a way to write out code without worrying about the exact grammer of a particular computer language) we can write out what’s going on.

time = 3

print time

time = time - 1

print time

time = time - 1

print time

ring!

So the output of this program is going to be:


3

2

1

Another important concept is a variable. A variable (like x and y in math) hold some kind of value, such as a number or a string of characters, like “what’s up?”. In this example time is a variable “time” which holds a number. The value of time changes throughout the code.

Once you understand how variables can change, you can start to shorten your code with some computer logic. Instead of typing the same thing several times, you can create loops that will repeat part of your code multiple times.

The coding language I’m going to use for these tutorials is called Blockly. It is similar to Snap and Scratch, in that code is represented by interlocking blocks.

The three Blockly examples in this section all do the same thing in different ways. The first is similar to the pseudocode above. Use the toolbox on the left to get more blocks and hit the green play button to start the code.

1

try changing this so it counts down from five

There are a lot of ways to make code shorter or more efficient. Using loops is one of them. Loops are pieces of code that allow you to repeat some of your instructions multiple times. If you're counting down from 100, you wouldn't want to code it the same way as above.

The second and third Blockly examples use a “repeat” block. Everything inside this block (to the right of the bright green area) will be repeated as many times as the block says.

In the second, if you change what time starts as, you have to change both the value of time in the first block, and the number of times it’s repeated. In other words, if you “set time to” 5, but are still repeating 3 times, it will count “5”, “4”, “3”, “ring!”.

In the third, I added another variable to use in the repeat loop so that I only had to set a number once. “count” is set to be the same as the original value of “time”. When time changes later, count is still equal to the original 3.

2

try rearranging the blocks so it counts "3 2 1 ring!"

3

try adding a block so it counts down to zero

Next week we’ll work more with different kinds of loops and computer logic.

Contact me at ruth.leopold@maine.edu for any questions, or to report any issues with this webpage.

Please fill out this survey to help me improve this and further tutorials!