There are three key components to any computer program:

  • Variables/Constants
  • Data Structures
  • Logic Structures

In the last post, I described the difference between a variable and a constant. In this post, I’ll get into detail about data structures and the different types of variables.

A data structure is, as its name implies, simply a way of structuring data.

Let’s pretend you’re in grade school and are learning the colors of the rainbow. Remember flash cards? You could write down each color of the rainbow on a separate flash card, and you’d end up with seven flash cards total:

  • Red
  • Orange
  • Yellow
  • Green
  • Blue
  • Indigo
  • Violet

You could do the same thing for the Planets, the Days of the Week, and so on and so forth.

In programming, it’s important to keep efficiency in mind. In the last post, I showed how you might use a variable to depict your current age (myAge).
However, how would you use variables to depict the colors of the rainbow? Your first thought might be to simply define 7 variables!

Although this would technically work, it might get annoying if you had to type rainbowSecondColor a dozen times. Also, what if a new color got added to the rainbow?
What if you wanted to, in your program, refer to the colors of the rainbow as one object instead of individual colors?

This is where arrays come in to play. Instead of shouting “Hurray!” if your rainbowSecondColor program actually compiles and runs properly, you can shout “Array!” because, of course, you’re a lot less prone to making typographical errors if you use an array. It’s also easier to maintain the list of items in an array, and look how clean the code is:

The one line of code, above, does what you would previously need to do in seven lines of code!

If you wanted to add a new color to the end of the list, you would simply append it after “Violet”. If you wanted to change Red to “Vermillion” and Blue to “Cerulean”, you could just edit them in-place! You can also dynamically change the array while your program is still running. For example, you could have a button that the user would click to add a new color to the end of the array.

Notice how I haven’t given many examples of actual code until now. This is because, again, the concepts are most important to learn first. The syntax, or how you write code, would come later.

How would you make a reference to a particular color in this array? Remember, in the previous post, we just had myAge and you would simply substitute it. Assuming myAge is equal to 22, myAge + 1 would give you 23. But how would you refer to, say, “Orange” in the array above?

What? Why the number 1?

Most programming languages follow what’s called Zero-Based Numbering. This means that you would refer to the first element, or item, in the array with the number “0”. Red would be 0, Orange would be 1, Yellow would be 2 …

While the details vary between languages, the concept stays the same: If you wanted your program to say “Orange”, and the programming language uses Zero-Based Numbering (like most), you would simply substitute the word Orange with rainbowColors[1].

If you wanted your program to display “Red”, then “Orange”, and cycle through the colors in order, you would use what’s called a loop. I’ll explain loops in the next post, which talks about Logic Structures.

Those of you with a keen eye have likely noticed the word String throughout the post. What on Earth does that mean?

Writing the word String in front of your variable tells the compiler, or what reads your code, the data type of the variable. It tells it that you’re trying to stuff text into a variable!

In general, there are three major Data Types:

  • Integer – Use this when you want to stuff a whole number into a variable (for example, myAge)
  • String – Use this when you want to stuff text into a variable (for example, rainbowFirstColor)
  • Double – Use this when you need a high precision number – that is, a decimal point followed by more digits – in a variable

Most programming languages require you to specify the data type for every variable. This is good, because while “myAge + 1″ makes sense, “rainbowFirstColor + 1″ makes no sense. You can’t add “1” to “Red”.

In conclusion, when you’re declaring variables, most languages require you to specify the data type before the variable, such as Java and C#:

Some programming languages require you to specify the type after the variable, such as Visual Basic:

In the next post, I’ll go over logic structures. These are what control the “flow” of the program, or how it gets from Point A to Point B.

Share

Comments are closed.

Post Navigation