Introduction Welcome Getting Started My First Program     Troubleshooting and Distributing Transforming Data Interactive Programs     Programming Exercises     Making Games
Documentation Declarations     Procedures     Variables Statements     Flow Control Expressions Libraries     Print     IO     Random     Game Engine
Tutorials Complete Tutorials     Introduction to Programming     Quick Start     Programming Exercises     Programming Games

TRANSFORMING DATA


Storing and Operating on Data





How do Computers Store Data

We have already seen that computers are machines that store and operate on data as instructed by us - the programmers. We have instructed the computer to show a message on the screen and now we want to make a more elaborate program, one that deals with data: receiving data (input), processing data and outputting data.
So let's learn about how computers store data!

Computers deals with data in binary form - or bits: a single value that can either be on or off, true or false, 1 or 0. As you can imagine, not many interesting things can be stored in a single bit, but when computers start combining these bits more things become possible. When 8 bits are combined, we have a byte. One byte is enough to store 256 different states, so it can be used to store small numbers for example.
Here is how it looks transforming the binary format computers understand to the decimal format we use.
000000000     0
000000001     1
000000010     2
000000011     3
000000100     4
000000101     5
000000110     6
000000111     7
000001000     8
...

That's how computers understand numbers! (If you are a bit confused, don't worry too much about it, we'll have other opportunities to understand every bit of programming!)

You probably have heard of a KB (kilobyte) or MB (megabyte) or GB (gigabyte) and so on. This simple means a certain number of bytes: for instance, 1 KB is 1024 bytes; 1 MB is 1024 KB (or 1,048,576 bytes). Today's computers have so much memory that it becomes possible to store tons of stuff like images, videos, games, documents and so on. All of that is available to us as programmers!

Imagine the memory of the computer - all the bits it has available - as a HUGE row of lockers.
Pretty much like the ones you find at a library or the gym.

The memory of the computer is a huge row of lockers

We can designate lockers to hold certain values and refer to those pieces of memory using an arbitrary name.

The lockers are referred to by name and contain a value

Remember back when I gave an example of the instructions necessary to read and add two numbers together. Let me refresh your memory:
read_number
   place1
read_number
   place2
add_numbers
   place1, place2, place3
write_number
  place3

Now we know where place1, place2 and place3 come from: they are identifiers to pieces of memory that are storing numbers.
These identifiers are also referred to as variables.
So a variable is simply a piece of memory (that stores data) that we refer to by name.
This way, we can declare these variables to let the computer know we are going to use some memory.




Declaring and Assigning Variables

If we want to operate on data, we have to ask the computer for a locker to store that data in.
This process is called declaring a variable.

Let's create an empty project and add the entry point.
To declare a variable, simply drag off the Execute pin and create a New Variable node.

How to declare a variable in Vizzcode

This node reserves a piece of memory (or a locker, according to the previous analogy) that we can use to store data and operate on it.
Besides reserving, the node is also assigning a value to the variable (or putting some stuff inside the locker). In this case, the number zero.
The variable came with the default name var but we can change it to whatever name we want by simple double clicking that name and typing a new name.
Note that identifier (variables) names can only contain letters (A-Z, a-z), numbers (0-9) or underscore '_' but cannot start with numbers.
You can learn more about naming identifiers here.

Just like in real-world where lockers have different sizes, you can store different-sized data in variables. Because of that, the compiler needs to know the type of data being stored in the variable.
You can learn about types in detail here.

Because the value 0 was assigned to the variable and the variable type is set to auto the compiler automatically assigns the correct type of integer (or int) to the variable. You can also manually set it to integer.





Printing Variables

Now let's start doing something interesting with this variable.
You already know how to print a message on the screen, right? So go ahead and import the "Print" library like you did before but this time, instead of calling print, try calling print_int, like this:

Printing integers in Vizzcode

The call to print_int is expecting a value to print. We can either type the variable name there or drag off the connection pin and create a node with the name of our variable.

Printing variables in Vizzcode Printing variables in Vizzcode by connecting nodes


This way we are asking the computer to display to the screen the value of the variable (or content of the locker, if you are still following that analogy...).

BEFORE RUNNING THE PROGRAM... Let me ask you a question: what value do you expect the computer to print and why?
Think about it: "execute" the instructions in your head (or a piece of paper, where you can write down the value of the variable at each moment) and tell me.
Hmm... good answer. Let's compile and execute to see what the program writes.
0
**** End of program. Press any key to exit ****

Zero!
When we declared the variable, the value 0 was also assigned to it. Try changing that value and compiling and running the program again.

A small note: if you try to print more than once, you will notice that the values are printed side-by-side which can be quite confusing. Try adding more than one print_int in your program and seeing that for yourself.
If you want to add a line break, simply add a print call and ask it to print "\n". Like this:

Using \n to break links

This will print the first time, add a new line and print again.
The \n characters represent a new line in code and the command prompt will break the line whenever it sees one.




Assigning Variables

Now the possibilities are emerging!
Can we change the value of a variable after setting it?
Yes! You can create an assign node. It takes the variable you want to assign to and the value you want to assign to that variable.

How to assign a value to a variable in Vizzcode

What happens now when you try printing the variable before and after the assign?
Remember that you can expect the instructions to executed one at a time and in order, so you can know the result by simply reading the nodes yourself.

Can you tell me this program will print?
#import
"Print"


main
:: () {
    var :=
1
;
    
print_int
(var);
    
print
(
"\n"
);
    var =
2
;
    
print_int
(var);
    
print
(
"\n"
);
    
print_int
(var);
    
print
(
"\n"
);
    var =
3
;
    var =
4
;
    
print_int
(var);
    
print
(
"\n"
);
}

If you are having a hard time or want to check, just copy and paste this code inside the Text panel in Vizzcode. Remember you can program by writing text or connect nodes all at once!

What about this one? (That's what I'm talking about!)
#import
"Print"


main
:: () {
    var1 :=
43
;
    
print_int
(var1);
    
print
(
"\n"
);
    var2 :=
21
;
    
print_int
(var2);
    
print
(
"\n"
);
    var1 = var2;
    
print_int
(var1);
    
print
(
"\n"
);
    var2 =
1
;
    
print_int
(var1);
    
print
(
"\n"
);
    
print_int
(var2);
    
print
(
"\n"
);
}





Operating on Data

Last but certainly not least, we can operate on the data.
The most basic types of operations we can perform are arithmetic expressions:
main
:: () {
    addition :=
123
+
456
;
    subtraction :=
123
-
456
;
    multiplication :=
123
*
456
;
    division :=
123
/
456
;
    modulo :=
123
%
456
;
}

In the Visual panel, you can either type the expression inside the text box or drag off the pin and create an operator node.

Operating on Data in Vizzcode Operating on Data with nodes in Vizzcode


Exactly like literal numbers (numbers like 123 or 456 typed directly in the code), variables can also be operated on:

main
:: () {
    first :=
10
-
6
;
    second := first +
9
-
2
;
    first = second*second;
    first = second*first;
    second = second+1;
}
Operating on variables


Just like in math, operators have precedence and you can use parenthesis to change that.

main
:: () {
    a :=
10
;
    b :=
6
;
    one_result := a - b *
100
;
    another_result := (a - b) *
100
;
}
Operating on variables


Note how the order of the operation in the Visual panel is mirrored by the parenthesis use in the Text panel. Try changing one and then the other to see them update.

Spend some time playing around with the operators and printing the results. Make sure you understand why the computer is displaying each value and try to know before executing the program what the results will be!




A Note on Node Execution

Now that you have lots of operators flying around, you may start to get confused on the execution of nodes.
A complete discussion on scopes (and what they are... :P ) is available here but the most important ideia is that the white connection (or execution connection) is what dictates the order of the instructions. So by following it, we know the main actions (or statements) of the program.

From there, the expression connections are executed staring from the main node, like this:

Understanding the flow of execution in Vizzcode

Note that every node must be connected to another one (so that all of them eventually connect back to the Global Scope Start).
Once you get the hang of it, you will see that it becomes second nature. ;)
That's why it's very important to create (and understand) several programs. The habit of trying to know what the output is going to be before seeing it is what will strengthen your programming muscles the most, because the entire program will be executing in your head: catching bugs even before they exist!

Now that you have processed and outputted data, you are ready for the last piece of the Input -> Processing -> Output puzzle: making an interactive program.