As I mentioned before, the best way to learn a programming language is to first learn some concepts. This is mainly because languages are becoming more interchangeable, but also because I’ve seen too many people try to “rush” into programming. Sometimes this is because of a lack of patience. However, all too often, it’s because a structured curriculum tends to focus on the nuances of a specific language – without first ensuring a solid foundation is built.

You can’t build a house on quicksand.

Therefore, I’m writing these posts with the assumption that you have no prior “foundation”. Ideally, someone who’s never written a program should be able to read these articles, and have his or her first program written in one hour or less – and I will, of course, help anyone along the way if there are questions.

There are three key components to any computer program:

Variables/Constants
Data Structures
Logic Structures

Don’t worry if any of these concepts seem a bit exotic. It’s no different than saying a car has three key components, each comprising various parts: Engine, Chassis, and Electronics.

Although these components may differ between car manufacturers and models, they are all found in the vast majority of cars. Likewise, the three concepts in the list are what build the foundation of a program. Since they’re fundamental concepts, they are practically interchangeable between languages.

Today I’ll only go over the most important – Variables and Constants.

What’s a variable and what’s a constant?

The Merriam-Webster dictionary defines a variable as “something that changes or can be changed”. It defines a constant as “something that stays the same”.

This is fine and dandy, but chances are you didn’t come here to read from a dictionary. The way I’ve had great success teaching students programming, in the past, is by comparing a variable to a placeholder.

If you’ve ever done algebra, you know that “x” can be substituted in place of any number. If “x” is “1”, then “x + 1” must be “2”. Sure, sometimes you’re trying to find the value of “x”, but sometimes you’re just using “x” as a placeholder.

If you’ve ever played a card game, you probably know that a “wildcard” can be substituted for another card value. Depending upon the rules of the game, that value might be constant – “a Joker is always equivalent to a King” – or it might be variable – “a Joker can be whatever you want.”

An example of a variable in the programming world would be “myAge”. Here’s a play-by-play on how this would work:

  • You set myAge to 22. The computer now substitutes the value 22 anywhere you use the variable myAge.
  • You write a computer program with references to myAge all over the place.
  • If you decide you want to be younger, you only have to change one thing – wherever you initially defined myAge, change the 22 to 21.
  • Now, every single reference to myAge will reflect the value 21 instead of 22.
  • You can even use myAge to do arithmetic! Assuming myAge is now 21, you’d increase it to 22 by – similar to algebra – writing “myAge + 1”.

There is no end to what you can do with programming, and hopefully you are intrigued by the possibilities.

However, it gets crazier.

Apart from just performing arithmetic on a variable and using it as a placeholder, you could also use myAge to control the flow of your program.
For example, suppose you wrote a program in the USA which listed types of alcohol. Perhaps the program wouldn’t even start if myAge is less than 21!
Imagine seeing this dialog box when you try to launch the program:

msgbox-age

This would be a rather annoying program! However, you get the picture – as a programmer, you are in control of the computer, not the other way around.
For reference, it only took one line of code to make that dialog box. One line. 30 seconds.

Okay, but what is a constant and how is it different?
The nuances depend on the programming language – Java doesn’t even inherently support the concept of constants – but in general, you can think of a constant as a read-only variable.

Whereas “myAge” can change, let’s pretend you had a constant for the speed of light. Let’s call it theSpeedOfLight – we’re not calling it “c”, because one-letter variables are typically frowned upon (they’re difficult to search for, among other things.) Since the speed of light doesn’t change, theSpeedOfLight would be best defined as a constant rather than a variable.

In the next post, I’ll go over some of the most common data types. These are what tell the compiler – the thing that “reads” and interprets your code – what type of variable you are using, in each case.

Share

Comments are closed.

Post Navigation