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
305 Members
Internet Crimes
Internet Crimes
106 Members
Internet Memes
Internet Memes
54 Members
Apple Fanboyism :heart::iphone::heart:
Apple Fanboyism :heart::iphone::heart:
12 Members
Comments (15)
  • Md Masud Chowdhury

    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.

  • indy

    more please moreeeeeeeeeee

  • Robbie5oh

    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

      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

      Thanks for the kind words Robbie. More to come!

  • Scutterman

    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”

  • Jeff Fabish

    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

      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

      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.

Load 10 more
Affiliate Disclamer

This review 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.
Affiliate Disclamer

This review 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