Think Programming : What you should know before you start programming.

Programming can be tough for beginners, unless you try and understand the basic concepts of programming.

Like any other task in your life, starting out with something new can be a difficult. It can be riding a bicycle/car for the first time or learning how to program for the first time.

This post focuses on imparting the message to you that programming is easy. And what kind of thoughts/questions should cross your mind when you think programming.

Yes, programming is easy. Anybody can program, but the difference lies in how skilful you are as a programmer. This is just like any other aspect of your life, everybody can ride a bicycle but not everybody is skilled enough to participate in Tour de France(the ultimate bicycling event), but that does not mean that you do not know how to ride a bicycle, right!

With this post we will try to work on an approach which would enable you to think in right direction when it comes to programming. The goal is to introduce you to the concept(tools) of programming so that you understand why a particular concept exists.

To make things easier, think of programming as a carpenter’s tool box. Each tool in the box is different from other tool and is used in different situations. Similarly there are many tools in a programmer’s tool box and each one of them are used in different situations.

One important thing to embed in your mind is that every concept in programming is borrowed from the real life. So if you come across a concept of programming, think about where you would need it in real life and once you understand that, the concepts will become very easy to you.

So are you ready to dig into Programmer’s toolbox? Let’s go through the basic programming concept which would get you started with your programming journey. The aim is not to tell you how to write code, but to help you understand why each of these programming concept(tool) exist.

Let’s go through each one of them one by one.

Program Structure

What is a Program?
A program is a sequence of instructions to accomplish a task.
Example : School Enrolment ProgramWhat are the sequence of instructions you have to go through to enrol someone into a School?

  • Get student’s detail
  • Get admission form – you go to the School’s reception, where they give you an admission form.
  • Fill in Student details
  • Submit the form at reception.
  • Wait and get the information about test schedule
  • Appear for the test
  • wait for the results
  • On success, pay the admission fees.

In a similar way, when you write a program, it is important to think through the sequence of instructions your program should go through

Example, if you had to write a program which would simulate the School Enrolment program, you can just go through the above set of instruction and write your program.

Or if I ask you to write a program to add 2 numbers, what will be your sequence of instructions?

  • Get first number
  • Get second number
  • Add 1st and second number
  • Show the output of addition

Following the above examples, if you are able to write the sequence of instructions which your program should go through, then you can accomplish any task through your program.

So what is Program Structure?
It is the correct placement of instructions in your program such that its execution leads to the output you want.

For eg: You can not first do the addition of numbers and then get the numbers which you want to add. That would be wrong. or think of a house structure, if you don’t lay the foundation first then your entire building will collapse.

It is important to add instructions to the program. Put it in a manner that the output of one instruction, acts as an input to the next instruction. So you should always structure your program carefully.

Input and Output – I/O 

A program on its own is not enough to accomplish any task. It needs input based on which it gives the output, just like any other system in this world.

So what kind of input a program works on and what kind of output it presents? Answer to that question is Data. 

What is Data?  Data is a set of values know as information, which is given as input to the program. A program process the data/information and presents the output.

But how do you represent Data? To answer this question, let’s go to our next concept that is Types also known as Data types.

Data Types

As the name suggests, types help you determine what kind of data you are dealing with.
For eg: 

  • How would you represent someone’s age – by numbers or digits like 23(data/info).
  • What is the 2nd letter/character in alphabets? – B (data/info) 
  • How would you represent someone’s name – by sequence of characters/alphabets : eg – Maya(data/info)
  • How would you represent ID of your college – by combination of numbers and characters : eg –  MIT1709(data/info)
  • What would be your answer to – Google knows everything? it will be a boolean value, either True or False(data/info).

In programming world we want to represent everything which has a representation in real life into this virtual world of computers. In order to achieve that, the concept of types was introduced.

As we discussed in the above examples, in order to represent certain data/information in real life we have the concept of numbers, alphabets, boolean etc. Similarly in the virtual world of computers there are concepts of numbers, strings (which can be combination of alphabet, number and some special characters) and Boolean(truth value) exists.

Let’s go through the data types available:

  • Numbers
    • Integer – These are the whole numbers( with no decimals) which can be positive, negative and zero. Eg: -10, 15, 0
    • Float – These are the fractional numbers containing decimals – Eg : 10.5 , 11.0 etc.
  • Char
    • Data type that holds one character – Eg : ‘a’, ‘x’ , ‘$’ , ‘3′ etc.
  • Strings
    • Strings are the sequence of characters such that you can for words, sentence or some ID in virtual world. Eg : “element” , “id123″ ,  “this is a string”.
  • Boolean
    • A boolean is an answer to a question in form of yes, or no. It is represented by true or false. It helps you in writing code based on conditions.

User Defined Data types

You can also create your own data types if needed.  They are essentially the combination of one or more of the data types discussed above, these are know as User Defined Data Types.

We will discuss more about User defined data types in separate article. Let me walk you through an example:

If I ask for detail of a student, what data would you come up with and which all data types would you use?
Student’s name – Maya – string
Student’s age – 20 – int
score – 67.8 – float
grade – B – char

Now If I ask you detail about another 50 student, you can go about the same as above. But it would make much more sense if we could group those attributes of a student together. Because we know each of the students will have the same set of attributes.

So we can create our own User Defined Data type as Student. And use it to store the values/data related to a student.

Note : Different programming language have different way of representing data types, but the core concept are almost similar.

In real life we always come across situations where we need to create a list of items or create a map like a phone dictionary where you could refer to someone’s phone number against their name.

Programming languages also provide certain types known as Data structure. It would take care of the need of list and map of an item. But it is important to understand that the list and map are nothing but the collection of basic Data types i.e numbers, strings etc.
Eg : List of names – [“Andy”, “Barbara”, “Chris”]Map of phone numbers – { “Andy” : “9876543210” , “Barbara” : “978645310” }

You will often come across situations where you will need to use list and maps. It’s important to understand these concepts. For now it’s just an introduction to them, but we will discuss them in greater in a separate post.

Variables

Like in real life many things change over time, like a person’s age/address etc. The input data to a program may or may not change after its manipulation.

But if the data is changing, then how would we keep track of it? 

Well that’s where the concept of Variables comes into picture. We tag our data with a symbolic name called variable, and we know for sure the name of the variable will never change but its value may change over time. 

Variables are nothing but a tag to your data, so that every time you wanted to refer the value of your data, you could just call them by their variable name.

We use variables every time in our day to day life. Eg : PersonName, Age, ID etc.The name of the person(variable) is Andrew(value).The age(variable) is 23(value).

Similarly we assign the values of our data to a variable in programming, which can change over the time of execution.

Again, how variables are approached by programming languages varies from one to the other language.

In some languages you have to define the type of the variable and in some you don’t. The one which do not define the data type, has to figure out the type by their own.

Example of variables(varies from language to language) :studentName = “Andrew” or String studentName = “Andrew”

Conditional Statement

How often do you come across the situations where you want to take actions based on certain condition being true or false.

Eg: When you are crossing roads, when you see traffic light, you take decisions based on the color of the traffic light. If traffic light is red than you stop; If it’s green then you move forward.

Similarly, in programming world you would find situations where you want to act based on certain condition being true or false. And for this the programming languages have tools like if and else.
This makes it easy for you to write decision based program.

Example, if you wanted to simulate the traffic light example, you would write something like this( ignore the yellow light in this example, like you do in your real life 😉 )

If color == “red”{ 
  vehicle = stop
}else{
    vehicle = start
}

Loop Structures 

This is the best concept/tool of the programming, its ability to do a task repeatedly.In real life you repeat a task till the outcome is achieved.
 For example, if you wanted to fill a bucket with water with the help of a mug, you would continue to pour the water into bucket with mug till it’s full.

Or when you want to count the number of books on the table, you would go over them one by one to get the final count. So if there were 10 books you would go over them 10 times to get the count.

Similarly in programming world, if you want to do something repeatedly you make use of loop structures.

Generally programming languages provide 2 kinds of loop structures.
While loop – which continues to do an action while a  condition is true – While bucket is not full keep pouring the water with mug.
For loop – This is used when you want to control the number of times a particular actions should be performed. You specify how many times you want do the repeated action.

Functions

Wouldn’t it be nice if you don’t have to write the same piece of code again and again, and you could just reuse your existing code for achieving a similar task? 

Well! that’s where concept of functions helps us out. You write the piece of code in a function which you think can be reused. Think of function as a block of code, which you can just use wherever you need, without actually having to write the block of code again.

It’s like, if you knew how to make a Washing machine, then you would just create it once and keep refusing it. You wouldn’t keep creating a washing machine every time you have to wash clothes.

And just as you know that washing machine is created by someone else, but everyone who has access to it, can use it. Similarly your functions can be extended to everyone(if you allow) over the internet to use and you can use someone else’s functions for your purpose.

Functions pioneer the phrase – don’t reinvent the wheel. 

Errors

Thanks to the invention of erasers, we can erase our mistakes in notebooks. We do mistakes all the time. 
Programs runs on input – input to the system is provided by humans, so it is likely that we will make mistakes while providing input to a computer.

Similarly there has to be a concept in programming which would tell us whenever we make any errors. This concept goes by the name Errors and Exceptions.

For example: If a toaster accepts a piece of bread as input, it will work fine as long as you provide the bread as input. But toaster will toast anything which you put inside it, since it can not check what is the type of input given to it. What will be the outcome? I’m sure it will be something unpleasant.

But programming languages have the ability to check on the input type. And it is programmer’s responsibility to put those checks in place. The input has to be checked, if correct accept it, and if it’s not then display the appropriate error messages.

For example, if your program displays the addition of 2 numbers provided as input, and the end user enters 2 stings as input, your program should check on the type of the message and show the appropriate error message.

Now some errors are already known, like when you divide a number by 0, or if there is not enough memory in the computer to run your program, or if you are trying to access some resource but that is not available( like file, folder, memory etc..). Programming languages makes them as defined errors and will pop those errors up whenever encountered. It is up to the programmer on how to handle the error, i.e. halt the program when error is encountered or try an alternate solution.

Note : Never ever assume that user will only enter correct input, users sometimes knowingly or unknowingly enter the wrong inputs and it is programmer’s duty to validate those input.

Libraries

In real life, Library is the place where you would visit, get the information from someone else’s work and use its derivative in your work. More importantly, it is the collection of records of other people’s work which you can refer to.

So we have got to have a similar concept in virtual world right! Well, every programming language maintains a library, where programmer’s can upload their work, and it could be referred by someone else, such that not everyone has to do the work from scratch which is already done.

Here’s a tip , if you ever feel like if there is something already implemented in the programming language of choice that you can reuse, just do a google search with something like this and you will see the results.

<programming language> library for <what you are looking for>Eg: python library for excel

Often you will find that there is a library for almost everything, you just have write you task/business logic on top of it.

There are many other concepts which are available in programming languages, but the ones discussed above are almost pretty much present in all programming language and are the core ones. So if you are a new programmer or starting out with a new language , you should always think about the concepts discussed above.

Of course you will get familiar with the syntax of the programming language of your choice once you get started with it, but I hope this post helps you understand why those concepts are there in those languages.

Leave a Reply