Click here to Skip to main content
15,121,414 members
Articles
Article
Posted 30 Mar 2018

Tagged as

(untagged)

Stats

11.5K views
1 bookmarked

Basic Programming Concepts for Beginners

Rate me:
Please Sign up or sign in to vote.
2.71/5 (3 votes)
30 Mar 2018CPOL11 min read
This is an article for beginners which relates basic programming concepts to how a computer works so that core understanding is strong for those wanting to start programming.

Introduction

In this article we will try to relate basic programming concepts with how a computer works so that core understanding is strong for the beginner.

However, to start programming you first need to understand what is a program

What is a program

A program is a set of instructions that the computer executes. (To be more precise, a program is a set of instructions loaded in the CPU that the CPU executes to achieve an outcome). Image 1

Now on to Programming, but First ..

a little bit of maths to begin with. Here is a simple equation Z = X + Y Suppose, the value of X is 100 and Y = 200, then we know Z = 300. Now here comes the twist of the programming world which shatters down the known world of an aspiring beginner. What if we change the equation to: Y = X + Y Believe it or not, this is a valid expression in almost all programming languages. A programming language is a language which the computer understands. Here you are not exactly following the high school concepts of mathematics you have learnt, but writing similar expressions to do mathematical computations and perform many other tasks. In essence, a programming language is just like any other language with its own syntax and constructs. It can be thought of as the language of a Pharaoh ordering the slave (CPU) to come up with a pyramid (output).

Moment of Clarity

Before moving further, let me clarify the difference between programming and computer science. Programming is instructing the computer by means of a programming language to perform certain tasks for your desired output. You may have heard the language names already - C/C++, Java, Python, PHP, Javascript and many others. For many purposes - good grasp of certain programming languages are enough even without a Computer Science Degree. So, let not anybody tell you - you cannot become a programmer without a degree, but certainly the theories of computer science help if you know them. Computer Science is the study of computers and computational systems. It is the scientific and practical approach to computation and its applications. The more you know the theories of this field, the better programmer you become. As a result, you write more efficient codes which helps to run a program faster using less computer resources. But to start off, absolute passion and absolute hard-work is enough to become a reasonably good programmer in the early stages.

Core Hardware Components to Run a Program

The two major components to execute a program are i) CPU ii) Memory (RAM) CPU (Central Processing Unit) runs a set of instructions. RAM (Random Access Memory) works as a temporary storage to help the CPU achieve the desired output/outcome. Why we are discussing these boring hardware stuff before moving on to programming? To make you a better programmer by getting your fundamentals right.

Ok! Really on to Programming

Let me go back to the famous expression Z = X + Y (If you know your maths well, then you should know that X, Y, Z etc. are all variables) Suppose we know X = 100, Y = 200 and we ask the computer to calculate the value of X + Y (which mathematically equates to Z). Here is how we achieve it: I will use the syntax similar to Javascript but the language is not important, the concept is. Since we have three variables, we need to declare them in our program. Most good programming languages requires that you declare the variables explicitly. Therefore we write var X var Y var Z This is called variable declaration. Since we know X is 100 and Y is 200, we write: X = 100 Y = 200 This is called variable assignment. We could actually write var X = 100 - in that case we would call it variable declaration with initiation. In that case we would be reducing some lines like this: var X = 100 var Y = 200 var Z Now it is very important to understand what goes on behind the scene, otherwise the mind blowing stuff we will be dealing with later (Y = X + Y) would be hard to make sense of. The moment we write var X or var Y, a space is allocated in RAM because unlike mathematical variables which exist mainly in our brains, the variables of a program resides in an actual physical location.

 

Here is a conceptual diagram for better understanding:

Image 2

X and Y are like rooms in a building (which is our RAM) and they have the capabilities of holding values that we assign to them. We might say Room Number X holds value 100 and Room Number Y holds value 200 because we had assigned the values through the assignment operator '=' to them. To calculate the value of X + Y, we could simply write X + Y However, doing this operation (every mathematical operation takes place in the CPU) and not storing the result somewhere would mean the result of this operation will be lost forever. There fore we need to store the operation result somewhere and luckily we have enough room in our RAM to make provision for Z which will hold the value of the X + Y result. Therefore, when we write Z = X + Y we are telling the computer to fetch the value of X and Y from RAM, add them in the CPU and then store the result in another space called Z. Image 3 "CPU instructions" in the above image are nothing but the program we have written which goes like this: var X var Y var Z X = 100 Y = 200 Z = X + Y Here Z is assigned a value which is the result of the operation of X + Y Now on to the expression Y = X + Y, which should make some sense now. What we are doing here is, fetch values of X and Y from RAM, add them in CPU and put the value back into Y. I hope by now you have understood computer programming languages are best read from right to left, almost a bit like Arabic isn't it :)? No, just scaring you a little! It is pretty intuitive. We just have to remember everything on the right side of an assignment operator ( '=' ) is processed first and then the assignment happens. Again, an image for illustration purpose: Image 4 To round it off,

a) We declare variables - which allocates room for them in memory ( I hope after you read this, you would be a little careful about the memory from here on, after all everything has a price). ex. var X;

b) We assign variables - which puts values inside the room. ex. X = 100;

c) For performing an operation, we fetch these/refer to these assigned values by simply referring to the variables, mentioning the operation between them. For example, X + Y means fetch value of X and fetch value of Y and perform the operation ('+') between these variables;

d) We store operation results by simply assigning to a room of our choice (either Z or Y); If you are observant, you'd see that an expression such as Y = X + Y doesn't need Z, hence we don't need to declare Z. This saves a bit of space in the memory. For basic programming it of-course doesn't matter. However, to be mindful of space and other resources will help you to become a great programmer some day. Yes, in modern day programming we have a lot of hardware resource, but sometimes we are abusing them by being careless with our approach to programming.

After all set and done, you might be wondering what should we do with Y or Z. How about showing the calculated result to the user of your program? In some languages we do that by invoking an instruction called print. If you write something like print(Z), it will show Z as the output to the end user in a DOS like black screen (console). In case of Javascript we can show the value in a message box with alert(Z). However, displaying an output to the computer/mobile screen is another discussion. For now, do try to grasp the concepts mentioned here before jumping directly into programming.

 

--- Image 5

Programming Constructs

Recall that a program is a set of instructions that the computer executes. But I did not get into details about the order they would be executed. This is where programming constructs come into action. They are used to control the order/flow in which instructions are executed (or not executed). In programming languages, the expression which translates to an instruction is called a programming statement or just statement. There are a number of recognized basic programming constructs that can be classified as follows: 1) Sequences (First Floor) 2) Selection (Second Floor) 3) Repetition (Third Floor) We can also add routine invocation to this (as Fourth Floor). But lets stick to 1st, 2nd and 3rd for the moment. I have used the concept of 'Floors' to help you visualize better how to go about constructing a program. Remember from last time - a program is a set of instructions loaded in the CPU that the CPU executes to achieve an outcome. Let me rephrase it by a program is a set of instructions loaded in the CPU that the CPU executes in a certain order (or certain orders when you have more than one cpu) to achieve an outcome now moving on ..

1. Sequence

A sequence construct tells the CPU (processor) which statement is to be executed next. By default, in popular languages, this is the statement following the current statement or first statement in the program. In other words, this is the very basic construct of writing a program. You just write line by line what you have in mind (of-course related to programming). Image 6

Apart from the default sequence, some languages support "goto" statement to skip certain statements and jump to a completely different set of statements; however this is very much discouraged. Eager readers may look it up in the web, given they have understood the other programming constructs well.

2. Selection

A selection statement provides for selection between alternatives, alternative as in options of path available for instruction executions to be more precise. A program can take certain routes depending on the situation and selection statements help in choosing between the routes. For example, In a factory, if an employee is present then calculate the salary for 8 hours, otherwise do not calculate the salary, just put a big zero (no work no doe). So depending on the state of the employee (whether present or not) you are asking the program to do one of two things - a) when employee is present calculate salary, b) when employee is absent put salary as 0 and the processor will choose between taking "path a" or" path b" but not both. So this is an example of Selection with a little bit of illustration below: Image 7 Now on to repetation ..

3. Repetation

A repetition construct causes a group of one or more program statements to be invoked repeatedly until some end condition is met. Let's stick with the salary case, and try to give the salary for a whole month. For simplicity sake, let's consider we have 20 working days in a month and we have to calculate the whole month's salary for the employee. Since we already calculated the salary for 1(one) day based on employees's present or absent status, we repeat the same process for 20 days and Ta'Da we have calcuated the whole month's salary for the employee. Simple (!) isn't it ? In this case "some end condition is met" would refer to the whether we have repeated the same sequence of instructions or iterated 20 times or not. We also use the term 'loop' instead of repetation or iteration. (Here 20 days mean 20 iterations). So the whole process in one visualization below: Image 8

Notice that almost seamlessly we have moved from one floor to the next starting from ground zero (basic programming concepts). The first floor is where normal things happen one after the other, where we have only a basic flow - statement after statement. Then we go up to second floor where Employee Salaries are calculated for the day. In this floor we learnt how to chose between calculating salary or skipping it - choosing the right path under certain conditions. Lastly to the third floor, where a very significant thing takes place. There, iterating over experiences (steps taken) of previous floors, we actually developed a complete work flow where employee salaries are are calculated and given at the end of a (20 working day) month.

Image 9

Believe it or not, we just stepped over the very the essence of programming - 1. Give instructions one after another 2. Select certain paths based on certain conditions 3. Iterate/loop through previous steps over and over until we have achieved a certain outcome. Now with this in mind, if you start looking at codes from internet, hopefully you can make sense of some of it, given you learn the basic constructs for a language you are interested in. Happy Coding.

Happy Coding.

Acknowledgements:>

https://cgi.csc.liv.ac.uk/~frans/OldLectures/2CS45/progCons/progCons.html

 

history:

Article first posted - March 31, 2018

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mukit, Ataul
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions

 
-- There are no messages in this forum --