Fundamentals of Programming: Part 5 – Data Types

by
Inf

Hello! Continuing with the Fundamentals of Programming series, we will now look at what Data Types are. If you have not read the earlier series, now would be a good time to start. You can click here go to Part 1.

What are Data Types? Well, they are a way for you to tell the computer what kind of data you are referring to. In English, you have two basic kinds of data types: numbers and letters. To a computer, those would be “char” and “integer”. Char is short for “character” and integer means a whole number, without fractions or decimal points.

Unlike humans, computers are not so intelligent (yet!) and need to be told exactly what kind of data they are dealing with. For us, numbers are “3” and “3.24”. For a computer, you’d need to specify “integer” for whole numbers, “float” or “double” for decimal numbers and even specify the sign! Talk about smart!

Just how many data types are there?!

Lots! And they have specific uses too. We have to start somewhere, so let’s start with numbers.

  • Integers: They are whole numbers. E.g. “3”, “210” and even “4521” are all integers. They are represented as 2 bytes (16 bits) in most programming languages. They can be signed (positive/negative) or unsigned.
  • Float: These are “floating point” numbers. Fancy term for “decimal numbers” or fractions if you want. E.g. “2.35”, “30.16” are floating point numbers. Floating point numbers are actually stored in a quite complex, as a sign bit, an exponent, a fraction and a bias. You don’t need to know all that, but if you want to… Floats are stored on 4 bytes.
  • Double: For a rather obscure reason, floating point numbers are not precise enough. Computers have troubles dealing with the fractional part and after a number of digits after the decimal, you lose precision. So for high-precision decimal numbers, you use double. It’s stored on a massive 8 bytes for high-precision numbers e.g. used in scientific data.

Various languages define other data types e.g. Date or Currency, but the 3 above are almost universal.

What about text? Here it’s easier.

  • Char: This represents a single character. Note, characters include letters, numbers and symbols, but you can’t make calculations using char. They’re the literal representation of the number. For e.g. ‘n’, ‘N’ and ‘7’ are chars. Note the single quotes.
  • String: Basically, you can think of them as an array of chars. They are just sequences of characters. E.g. “Hello” is a string. Note, double quotes.

Normally, to avoid confusion, chars are represented inside single quotes while string are within double quotes.

How would you represent true or false?

  • Booleans: It’s a “true” or “false” value that can only be one at a time. Programs don’t have a separate data type for that. They normally just represent true as “1” and false as “0”.

There are a few other data types you may want to know of, although per-se, they are not necessarily “primitive” data types:

  • Enumerations: Sometimes you want data that only comes from within a list of values which you define (or enumerate). For example, “Days of the week” would be “Monday, Tuesday, Wednesday… Sunday”. You can then make checks to see if some value falls within your list.
  • Array: This is a sequence of other data types. As I stated before, arrays have data types in most languages i.e. they are typed. You can such as, have arrays of integers, floats, or chars – strings basically.
  • Map: This is a abstract type that allows you to store a number of key-value pairs as mappings. For e.g. you can map Strings to Integers or Integers to Strings or even Strings to Strings. For e.g. “John – Doe” would be a mapping of “Forename-Surname”. The key is “John” and the value is “Doe”. I could ask the map for “John” and it would give me the corresponding value, which is “Doe”, if I was looking for surnames.
  • Set: A set can be thought of as an array that does not allow duplicates. You cannot have a set like [2][3][2] for example. If you tried that, it’d just store it as [2][3] and ignore duplicates, in most cases. It could also complain about duplicates in some languages.
  • Queue: This structure is like an array, but you read from one side and put new things from the other. It’s like a queue in real life: people join at the end of the queue and leave from the head. It is a First-In-First-Out (FIFO) structure.
  • Stack: Data is read and written to from the same end. Think of a pile of plates. The last one you put is the first one you will take off the pile. It’s is a Last-In-Last-Out (LILO) structure. Or First-In-Last-Out (FILO) if you want.
  • Pointer: A pointer points to a memory location. For example, you can have a pointer that references the memory location where an integer is stored for e.g. Or have a pointer to indicate at which point you have reached in an array.

Why are there all these data types? Why do you need them at all? That’s because computers represent data and use them differently. You cannot do calculations with strings directly. You cannot addition two booleans. The different types are useful when you want to do type-checking, for example, the user has not input letters when you’re trying to do calculations with numbers. Or you are trying to put letters in an array for floats for example. The compiler in strict languages will complain bitterly and prevent you from committing mistakes, so your programs break less often.

Operators

There are different operators you can use on different data types. Here are the most commonly used operators:

  • = :Not equal to! Yep! Not Mathematics! This sign means “assignment“. It is used to assign values to variables!
  • == :This one is what you know as “equal to“. It compares equality and returns “true” or “false” depending on what it finds.
  • === :Some languages have this sign. It compares both values and data types. If both are same, then “true” is returned, otherwise “false”.
  • + :Normally represents addition. I.e. the sum of two numbers. In some languages however, it can also represent joining two strings. For e.g. “2+2” will give “4”. But, “Hello” + “World” will give “HelloWorld”.
  • – :Subtraction. Simple. “5-3” will give “2”.
  • * :Unlike Mathematics, where x represents multiplication, in programming, you use asterisk. “2*2” gives “4”.
  • / :This one is division. “4 / 2” gives “2”.
  • % :Modulo operator. It gives the remainder of a division operation. E.g. “5 % 2” would give 1. It is useful to check if a number is even or odd. Even number % 2 would give zero. Odd numbers give 1.
  • .  :That’s a dot. Some language use it to join (concatenate) two strings, instead of + to avoid confusion. So, “Hello” . “World” gives “HelloWorld”.

Below are comparison operators. They return true or false (boolean data type) depending on what the condition is.

  • > :Greater than sign. Used in comparisons.
  • >= :Greater or equal to sign.
  • < :Less than sign
  • <= :Less than or equal to sign.
  • ! :Represents “not“. I.e. it inverses other operations. For example !(x>2) is “not x greater than 2” or “x not greater than 2” which is equivalent to saying (x<=2), or “x is less or equal 2”. E.g. != is not-equal. Careful here, != is not equal. It’s not !==. Strange, but that’s how it is!
  • AND: The boolean AND operation. Please see below.
  • OR: The boolean OR operation. Again, please see below.

The last 2 operators are mainly used with Boolean data types. You use them to make a series of checks to see what the values are. For AND, all the conditions must be true for the final result to be true. If a single condition is false, the net result is false. For OR, at least ONE condition must be true for the end result to be true. If any is false, you don’t care. It’s still true. If none are true, then you get false.

Here are some examples of comparison operators and their boolean outputs:

  • 2 > 3  :False
  • 3 > 2  :True
  • 2 >= 2  :True
  • 2 == 2  :True
  • 1 != 2  :True (1 is not equal to 2? Yep)
  • 2 != 2  :False (2 is not equal 2? It is. Hence, false)

Here are boolean AND and OR comparisons:

  • (2 > 3) OR (2 == 2)   :True since (2==2 is true)
  • (2 > 3) AND (2 == 2)  :False. All conditions must be true and (2 > 3) isn’t true.
  • (1 == 1) OR ( (2>3) AND (4>3) )  :True. This one is tough! First, you check the inner-brackets. (2>3) is false, and without needing to check the rest, you know AND will now return false. (1==1) OR (false) is how the expression looks now. So (1==1) OR (false) returns TRUE since (1==1) is true, while the other is false, so you don’t care.
  • (2>3) AND ( (1==1) OR (2==2) )  :False. Why? Because even if the inner OR returns true for both, the first part of AND, (2>3) is false, so the whole thing becomes false.

I understand that the above is a bit hard to understand for beginners. Do try a few other examples on your own or re-read this section if you are having troubles. Don’t worry too much if you don’t understand it at first. It comes together with practice and familiarity with programming languages.

Here are the truth tables for AND and OR, if you are interested:

AND: Only true if ALL conditions are true.

True-True = True
True-False = False
False-True = False
False-False = False

OR: True if AT LEAST ONE condition is true.

True-True = True
True-False = True
False-True = True
False-False = False

I end this post on Data Types and Operators here. I hope it was not too hard to understand and you now have a fairly good grasp of what data types do and why they are needed. If you have questions, please ask in the comments section below. Other programmers’ views are welcome too. See you in the next series!

[seriesposts title=”In the same series:” titletag=”h3″ listtype=ul show_date=0]