The Basics Of Computer Programming 101 – Variables And DataTypes

Ads by Google

basics of computer programmingHaving introduced and talked a little about Object Oriented Programming before and where its namesake comes from, I thought it’s time we go through the absolute basics of computer programming in a non-language specific way. This is the kind of stuff computer science majors learn in the first term, and I’m aiming this at people with absolutely zero experience in programming.

Today, I’ll be covering the most fundamental part of any programming language – variables and datatypes. We’ll have a few more lessons after this on the fundamentals before we delve into any actual code, so no worries about things getting complicated yet.

Variable and Datatypes:

At the core of any program are variables. Variables are where the dynamic information is stored. When you type your name into a web form and send it, your name is a variable.

Not all variables are the same though. In fact, there are many different types of variables that nearly every programming language has. Let’ s look at a small selection of them, as well as their short names if they have one:

Character (char): This is a single character, like X, £, 4, or *. You don’t often create single character variables, but they are at the core of the language so you need to know what they are.

String: This is a “string” of characters (see how they’re at the core?) of any length. In my previous example – your name on web form – your name would be stored as a String variable.

Ads by Google

Integer (int): A whole number – whole meaning there are no digits after a decimal point. So 65 would be a valid integer; 65.78 would not.

Floating-point number (float): A number that may have digits after the decimal place. 65.00 is technically a floating point number, even though it could be represented just as easily as an integer as 65. It takes more memory to store a float, which is why there is a distinction instead of just creating a “number” datatype.

Boolean (bool): A variable to represent true or false (or it could also mean 0 or 1, on or off). The simplest datatype and commonly used – get used to this one!

Array: These are essentially lists of other variables. There are a variety of array types depending on the language, but basically they’re just a collection of variables in a sequential list. For example: 1,2,3,4,5 might be stored as an array (of length 5) containing integer variables. Each variable in the array can then be accessed using an index – but you should know the first item in the list has an index of 0 (yes, that can be be confusing sometimes). By storing them as an array, we make it easy to send a collection of variables around the program and do things with them as a whole – such as counting how many things are in the array or doing the same thing to each item (which is called an iteration, and we’ll get to that another time). You should also know that a string is actually just an array of characters.

Phew, I hope that wasn’t too technical. If you need to re-read that, no one would blame you. If you still don’t get it, tell me in the comments.

basics of computer programming

Strong and Weak Typed:

Moving on, programming languages can be divided into those that are strongly-typed, and those that are weakly-typed. A strongly typed language (such as Java) requires that you explicitly declare what type of variable you are creating, and they get very upset if you start trying to do things with them that you shouldn’t. For example, a strongly typed language would give you errors if you tried to add an integer and a string together. “How on earth am I supposed to mathematically add together a word and a number?”, it would cry – even though you as a human clearly understand a string “5” is semantically the same as an integer with the value of 5.

A weakly typed language on the other hand would just say “whatever”, and give it a shot without complaint – but the answer could go either way. Perhaps “5+5” = 10, perhaps it’s “55” – who knows! It might seem at first like weakly-typed languages are easier to write, but they can often result in curious errors and unexpected behavior that take you a while to figure out.

Assignment and Equality:

Nothing to do with socialism…Instead, its a concept that catches out many programming newbies so I wanted to address it now. There is a difference between assigning and testing for equality. Consider the following, both of which you would probably read as “A is equal to 5”:

A = 5;
A == 5;

Can you tell the difference? The first is known as assignment. It means assign the value of 5 to variable A. You are “setting” the variable value. The second statement is one of equality. It’s a test – so it actually means “is A equal to 5?” – the answer given back to you would be a boolean value, true or false. You’ll see how this can mess up your programs in later lessons.

That’s it for today’s lesson. Please don’t hesitate to ask questions in the comments if you didn’t understand something, and I’ll be more than happy to re-word it or explain differently. Next time we’ll take a look at functions and return values, before moving onto loops and iteration.

Image Credits: ShutterStock 1, 2

Join live MakeUseOf Groups on Grouvi App Join live Groups on Grouvi
Hacker Talk
Hacker Talk
506 Members
Affiliate Disclamer

This article may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.
New comment

Please login to avoid entering captcha

Log In