The terminologies we will see in this article will make the road to learning Pseudocodes and Flowcharts (We will come back to what are Pseudocodes and Flowcharts) a piece of cake. Understanding these terminologies not only helps us quickly develop Pseudocodes and Flowcharts, but it will also help us identify our code structure before we even write any single line of code because that’s the purpose of Pseudocodes and Flowcharts.
Here we will address concepts like Variables, Constants, Datatypes, Declarations, Arrays, and Expressions and Statements. In the following article, we will try to understand Conditions, Code blocks, Loops, and if we have enough energy left, we will discuss Functions.
Variables
In the previous articles, we spent a lot of time trying to understand different value representations in computers. For a computer to process some value, it must be supplied for the computer via input units, but how do we know that the specific value we accepted belongs to our program? What is going to happen if we need that value for another computation at later times? That’s where variables came in handy.
Variables are names that stand for values. We can consider variables as naming a memory area, and if we want to access the value stored on it or store a value on it, we simply use the name. In simple terms, they are identifiers of a memory section. As they are memory areas, and their name suggests, we can change their value whenever we want.
To describe Variables using a real-world example, let’s say you are attending a very formal wedding. Before you get to the wedding venue, the wedding planner will reserve a seat with your name tag; the only thing you need to do is look for your name, then sit, and whenever the wedding planner wants to speak to you, they only need to come to the table you are sitting at. In this case, the planner is the programmer, the value is you, the seat reserved for you is a memory area, and your name tag is a variable name.
In any program, every variable has a Name, Datatype, Size, and Value. Variables can represent numeric values, characters, character strings, or memory addresses. Most programming languages differ in how they assign a Datatype to a variable which determines the size of the variable. However, most programming languages share the same rule on how to name a variable.
Rule on Naming a variable
A variable name should:
- Use only alphabets, numbers, and underscores (_)
- Not begin with a number, and must have at least one alphabet
- Not be a reserved word (this is specific to the programming language)
The following are good examples of Variable names: my_variable, my_1, _x, number1, etc.
The following are invalid examples of Variable names: 1number (shouldn’t start with numbers), number 1 (spaces are not allowed), int (a reserved word is not allowed).
Constants
They are just like variables (identifiers of memory are), but their value is constant, meaning we cannot change it once assigned. They are often used to store a value that will never change, like a value of the gravitational force of an earth 9.81 in Physics, like the value of Pi Π 3.14 in Mathematics.
When naming a constant, the basic rules for variables are valid here. However, it’s suggested to use all caps (all capital letters) to signify that it’s a constant, but it’s not obligatory.
Datatypes
Datatype is a classification of a particular value type to determine how much size should be allocated and what kind of operations are allowed on the value. Data types are essential to any computer programming language. Maintaining memory and operations within a computer program becomes very difficult without them.
The following are Datatypes shared over any programming language:
- Integer: A positive or negative whole number, a number that has no fractional part. E.g., 0, -125
- Floating-point: A positive or negative number with a decimal point. E.g., 3.5, -11.75
- Character: Can only represent a single character. E.g., a, A, 1, $
- String: A sequence of characters. E.g., John Doe, blah blah
- Boolean: Either True or False value.
Declaration
In the earlier example, your seat is reserved before you get to the wedding venue. Likewise, Variables need to be reserved before their use. The mechanism of reserving variables in computer memory is called Declaration.
Declaration of a variable serve two purposes:
- It reserves a memory area with an identifier (meaning Variable name)
- It associates a Datatype with the identifier
A typical variable declaration in C++ looks like:
int count;
int
signifying the Datatype, count signifying the variable name.
A typical constant declaration in C++ looks like:
const float PI = 3.14;
Arrays
In the example above, the wedding planner is reserving a sit for only you, i.e., a single person. For example, what happens if you are going with your family and your family is composed of 10 people? A simple answer you might think of is to reserve 10 different seats with different names. Right? But isn’t it more straightforward if the planner could gather all the 10 seats, put them around a single table, and reserve it as xyzzy’s family seat? That’s what arrays do in a nutshell.
Arrays are mechanisms to declare similar variables under the same name and put them one next to the other to make accessing values easier.
A typical array declaration in C++ looks like this:
int ages[10];
This will create 10 different variables under the name ages
, and we can access each by their index value.
note
In any programming language, arrays are 0-based indexes, i.e., in the above example, the first variable will be found at age[0]
; the last (the 10th) variable will be located at age[9]
. [number]
is a mechanism to differentiate indexes.
Expressions and Statements
In any human language, there are phrases and statements. Likewise, in programming, Expressions signify phrases, and statements are statements. You can take expressions as unfinished statements those yield some value.
For example, in C++:
x = 7 * y
is an expression but
x = 7 * y;
is a statement. 7 * y
alone is an expression; it will become a statement if we add a ’;’ at the end, just like this 7 * y;
.
Phewwww 😌, we tried to cover so much information with a single article. Before I change my mind and keep writing, let me rest my case here. I will see you in the following article, where I will talk about Conditions, Code blocks, Loops, and Functions.