# The Basics Of Computer Programming 101 – Variables And DataTypes

23-10-2011

Having introduced and talked a little about Object Oriented Programming Where Did "Object Oriented" Programming Get Its Name From? Object Oriented isn’t just a random buzzword you hear in programming circles. There’s a reason behind the name - but what? Join me as I explore some of the fundamentals of programming concepts and explain... Read More 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.

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.

## 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

1. Annette Barron
July 18, 2018 at 12:27 am

I can almost understand what these terms mean but what are they supposed to accomplish? I don't know what the punctuation and symbols are to going to accomplish. Is there a list or "glossary" for all these symbols?

• James Bruce
July 18, 2018 at 7:27 am

It depends on the language you're, but usage is often similar:

{ } curly brackets tend to denote blocks of code, such as functions or if statements.

. + Period or plus symbol used in a non-mathmatical sense is used to concatenate, or push things together, like words and variables.

[ ] square brackets denote a list of some kind, an array.

Then you have the boolean comparison symbols, things like <> = && || ! .... Less than and equals should be obvious, while && and || would be an article in themselves.

Make sure you read part 2 as well, might help to explain some of those additional concepts: //www.makeuseof.com/tag/absolute-basics-programming-beginners-part-2/

2. Patricia
February 17, 2018 at 9:45 am

Thank you for the information now this make sense , its good to have you because i'm still new in the field. I would like to learn more about programming and choosing a language .

3. delaram
February 9, 2018 at 8:25 am

you didn't say a word about double variables.

4. Ph?ng
April 8, 2017 at 4:56 pm

I feel a little confused by "String" . Anyway I think that word is easily to make people who begin like me will understand. Thank so much.

5. kyle
January 6, 2017 at 7:59 am

Thank you for this. It really helps

6. m.khalid saifullah
December 26, 2016 at 3:28 am

Vert very good sir

7. Ludwick
September 1, 2016 at 6:36 pm

Good day James
I am in South Africa and would like to start programming, any sharp words for me? please dont ask me about the president

8. Anonymous
May 7, 2016 at 3:21 pm

hello i am Abdullah
i am new in the field of programming and don't know about programming its languages and its coding. but i want to learn programming language which is the best language of programming now.so please suggest me where do i start to learn about programming.
i want to be a software developer.

9. shehan
March 24, 2016 at 10:42 am

I'm Shehan.I have a small knowledge about computer programming.I know somethings like flow charts,but the thing is I still don't get why flow charts and other stuffs like that are related with computer programming.Also I am confused with coding.

10. Anonymous
August 30, 2015 at 9:54 am

I am md. masud chowdhury, a banker, wishes to learn software developing but i have no knowledge about it though i work through various app. sofware. as i have zero knowledge your lesson is a little hard to me understand. I trying my best to learn. Thank you very much.

11. indy
October 27, 2011 at 10:09 am

12. Robbie5oh
October 25, 2011 at 12:43 am

Really liked the way you explained this. yes, I am new to learning programming language, and it is fascinating to me! However, the ones commenting need to keep it as simple as it is explained here! (Programming language for dummies type of explanation!) Not saying I didn't understand, but I had to really concentrate, and read the comments over and over! LOL Love it though! keep on posting comments!

• Jeff Fabish
October 25, 2011 at 1:11 am

What language(s) are you learning?

Programmers really are the unsung heroes of the digital age. It really gets fun once you've conquered the terminology and understand the syntax, then you're in control.

• James Bruce
October 27, 2011 at 8:08 pm

Thanks for the kind words Robbie. More to come!

13. Scutterman
October 23, 2011 at 9:02 pm

Some languages (php being the main one) will have different ways of handling concatenating strings to adding numbers. You use a period (.) to say "treat both as a string" and a plus sign (+) to say "treat both as numbers". Of course, it will complain if you use the wrong one, but that is just something to learn.

For languages that use weak typing and a plus sign for both, you can get around it by ensuring that a potential string is multiplied by 1 before it is added to a number. If you want to ensure it gets treated as a string then simply add a blank string in between the things you are trying to concatenate:

var x = 5;
var y = "5";

print (x + (y*1) ); // 10
print (x + "" + y); // "55"

• James Bruce
October 24, 2011 at 9:00 am

Awesome tips, thanks Scutterman.

• Sam okpe
December 9, 2016 at 7:31 pm

This really looks complicated here o, I hope I get to a level we're this can make sense

• Scutterman
December 10, 2016 at 12:19 am

The main thing is to just get a lot of experience. Being a good programmer is more about knowing different coding patterns and techniques than about any specific language. It's pretty easy to google for language quirks. Just write a lot of code and you'll pick it up really quickly.

• PHill
May 26, 2017 at 2:10 pm

i got it Mr Scutterman so if its (++) so what does double pluses mean that's where i get confused

• Scutterman
May 27, 2017 at 3:16 pm

A variable containing an integer that's followed by two plus signs basically means "add one to this number".

\$i++;
is the same thing as
\$i = \$i + 1;

There is also
++\$i;
The difference between the two is one will return the original value and the other will return the incremented value. I can't remember off the top of my head which is which.

14. Jeff Fabish
October 23, 2011 at 8:20 pm

There are two ways to declare a variable, implicitly and explicitly. Which one you use will most likely depend on which language you're using. Implicit means the compiler / interpreter will (or can) determine for you which variable type would be best. Explicit declaration are where the programmer must define which type it is before compilation or interpretation. Explicit definitions typically are must quicker but you suffer the possibility of using the wrong type, such as using an int, when you could use a short.

Example of implicit declaration:
var impVariable = "Hello World!"; // Converts to string or char array

Example of explicit declaration:
string sVariable = "Hello World!"; // "String" must be declared before compilation

Byte: A byte is comprised of 8 bits. A byte holds an integer with the maximum value of 255 and a minimum value of 0. There are other types such as enums and objects, which probably exceed the scope of a beginner.

Also, there are "signed" and "unsigned" variables. Think of a sign as a minus (-) sign that denotes a negative number. A signed variable can be both positive and negative, however an unsigned variable may only be positive (since it's unsigned, it can't have the '-').

James, you should write an article comparing the different languages and which one is best for what task, I find that's a common question for code virgins.

• James Bruce
October 24, 2011 at 9:03 am

Thanks for the added info Jeffery. I think your article idea is awesome, but I'm not sure if I'm an experienced enough programmer to be recommending languages. I'm limited to java, javascript, c#, php - but I could certainly do some research into others. Which languages do you think it would be best to include other than those I mentioned? Obscure things like prolog for AI might be interesting.. been a while since I touched that.

• Jeff Fabish
October 24, 2011 at 7:18 pm

Yeah, if you decide to do the article you may have to split it up into two versions, which languages are best for application development, and which ones are best for web development. C/C++ (distinguish the object orientated difference between the two) for maximum control to low level procedures; Java (networking and cross platform support), C# (power of C++ but ease of Visual Basic), Visual Basic (rapid application development and developing bad habits) and Python (which is a good starting language). A lot of people think that Java is a synonym for Javascript so lay that fallacy to rest (:

You may want to introduce the concept of an intermediate runtime, such as JVM and .Net, which reduce work load and add to cross platform support. To be honest, I hadn't heard of Prolog prior to this, Lua has pretty much stole the spotlight. If you're not using an engine I would stick with C++ when dealing with AI. In UE3 however, I'm forced to use uScript, which is a hybrid of C++ fortunately. C#, Javascript and Boo (whatever the hell that is) are languages used in many games with Unity.

• Michael Irwin
July 3, 2018 at 3:47 am

Doing an article on languages that have fallen by the wayside (and those we wish had!) would be fun - you could include all these, just to start off with!
Prolog, PL/1, Algol, Snobol, Fortran, Cobol, Coral, Jovial, Ada, Bliss, SmallTalk, BCPL, Mumps, MIIS, M, Pascal, Modula, Perl, Pearl ....... !!!
I'd advise offering readers a problem each time you publish, together with three answers - one in each of C, PHP, and SQL.
These three because they're easy to write simple code in, there's one for base programming, one for web sites, and one for databases, and, from these three, a newb should be able to find something to glom onto and learn more of.

15. Windex98
October 23, 2011 at 7:45 pm

That was refreshing. Thank you.

16. EnzoA
October 23, 2011 at 7:37 pm

any reading material i can also look into?

• James Bruce
October 24, 2011 at 9:04 am

I have a part 2 coming soon, and eventually will go into individual languages. It's still very beginner stages, so I wouldnt suggest reading around at this point. That comes later...