Easy, Programming, Freshers

In the new tech-driven world, everyone is expected to know the basics of how to write computer programs. If you have a little experience with coding or even if you are a complete beginner, here are the fundamental concepts that you will need to be a better programmer.

1. Variables

All high-level programming languages have the concept of variables. If you have any experience with Algebra, you know what variables are. They are names given to containers that can carry a value. For better understanding, think of variables as boxes. Assigning a value into a variable is similar to putting the value into a box with a name, this name is called an identifier.

E.g., Once we assign

Here the identifier is num1, which can be seen as a name card on a box.

num1 is assigned 15, is similar to putting 15 within the box with name card num1.

Since num1 contains 15, now when you mention num1, the computer knows it carries 15 and will use that value in its place.

When you create a variable, basically you are naming a portion in the memory and storing data in there.

2. Functions

Functions allow the re-use of code. To understand their purpose, let’s take an example.

This is a piece of code written in Python that prints a few messages.

Now imagine you want to print this message not once but in different parts of your program. Now the straight forward solution would be to type all this code again and again everywhere needed, but it is time-consuming and inefficient. Functions can be used to give a name to a set of statements. Here we have 3 different statements. We can enclose statements in a function and give it a name so that it can be re-used.

In Python, a function is implemented as follows

show_messages  is the name of the function, therefore

will print the whole message three times.

3. Datatypes

Okay, now we know that variables are similar to boxes. But can you store anything in any given box? To store a watch, you need a small box while storing a laptop will require a bigger one. Variables are no different. Depending on what kind of data you wish to store the size and type of the variable changes. Most programming languages have the following Datatypes

  • Integer – non-decimal numbers
  • Float – decimal numbers
  • Char – a single alphabet or symbol
  • String – a phrase or sentence
  • Boolean – either True or False

Some languages require you to tell what type of variable you wish to create explicitly, that is what size of box you want to choose. If you plan on storing a number value without a decimal point, you would choose an integer variable and so on.

This is a ‘C’ code that creates and assigns values into variables roll_no and marks. You can see that since ‘roll_no’ will always have whole numbers, it is declared as of type int (shorthand for integer) while ‘marks’ are given float.

Some languages, like Python, JavaScript, etc., will automatically assign the right type to variables. So in these languages, you need not specify what type of variable you intend to have.

4. Loops

Just as the name suggests, a loop is used to run statements over and over again. So if it’s running in a loop will it ever end? To ensure that a loop does not repeat indefinitely, it is necessary to give a condition to break the loop. There are three main types of loops :

  1. For-Loops
  2. While-Loops
  3. Do-While-Loops

Here we will see an implementation of a for loop just to get an idea of how loops work.

Different languages have slightly different implementations, but the idea remains the same.

for (starting initialization) (condition to satisfy) (change to make on each iteration)

is the general structure. Let’s see for-loop through an example

This is an example of for loop in C. The starting initialization ( i=0 ) is only run once when the loop begins its execution.

The only statement within the loop is a statement to print the value of i. During the first loop iteration, i is 0 and 0 will be printed.

Now the change to make on each iteration ( i=i+1 ) is executed, making i value to 1.

Next, the condition to satisfy ( i < 100 ) is checked, and we know 1 is < 100, so the loop executes again.

This time i value is 1, and therefore 1 is printed.

This continues until the condition  ( i<100 ) is not satisfied, that is when i equals to 100. This loop will, therefore, print all number from 0 to 99.

These are fundamental concepts that anyone aspiring to code should know and are common to any language you may choose. If you have any doubts regarding programming, feel free to ask them in the comments.