L

learnasyoulike

time


What is programming

Table of contents:

What is computer programming


When we say "this robot can serve dishes to customers of a restaurant", we mean that the robot has been programmed to do a specific task, and that task may be a series of activities, like receive dishes from the kitchen, set a target table as per input, determine the shortest path to that table, move to the customer, and serve the dishes.

Robots are nothing but computers.



Table of contents:




So we program a computer to do a specific job, like calculating all 5-digit prime numbers, solve for the graph of an equation, update a database file, even serve dishes to customers😜.

Statements and languages

Statements or instructions are the building blocks of a computer program. Those are written in a specific language and syntax understandable to a computer.

If I ask you: "What is the value of 2 raised to 3?" , your answer should be 8.
Imagine my question to be a single statement or instruction, and your answer to be the output.
In the above communications, there is a language in between you and me: English. Both of us understand this language. You understand the question, calculate 23, and express the result 8.

Computers work on the conventional system of

Input ---> Process ---> Output.

To communicate the above problem to computer, we cannot use English. A CPU (Central Processing Unit) understands only machine codes. So we use a syntax like:



Above is a single statement written in python. Only a python interpreter can translate it into a language which a CPU can understand (machine language). The translated instruction is passed to the CPU for execution. CPU executes the instruction and passes the produced result to python interpreter. This result is again translated to an understandable format for display.

In the above, the statement print(2 ** 3) is the input which contains instruction and data, the translation and the execution is the process, 8 is the output.


There are a large number of computer languages available today. Some popular names are python, javascript, c++, java, kotlin, go etc. Every language has its own words and syntax that must be followed while writing code. We can choose to learn any of those languages to communicate problems to computer and get them solved.

Following is a computer program which calculates the average of five numbers 99, 109, 223, 1005 and 476954.






Above three statements have been written in javascript language. The output of the program will be:
The required average is 95678

Broadly, both the words statement and instruction are similar in meaning.

So, a statement is a line of code to instruct the CPU to perform some task. It is written in a proper language and syntax computers can understand. Example of statements are:

print("Hello World!")
cout << "Result = " << result;
mov eax, 1


Computer Programs

In the world of computer, a program is a set of instructions or statements which are executed or run one by one to solve a specific problem, and achieve a result or output. Sometimes, to solve a problem, we require to write multiple statements to be executed in a sequence, as seen in the above javascript example.

She who writes a computer program is known as a programmer. A programmer writes a program file as per the problem in hand. She writes multiple statements in a text file. Once such a file is typed, named, and saved, it can be executed or run anytime to obtain the desired output.

A program contains multiple statements as in the following:

Structure of a computer program Structure of a program


Suppose we want to list all the prime numbers inbetween 1 and 9 inclusive.

Our brain 🧠 can do that:   2, 3, 5, 7

We can even list further:  2, 3, 5, 7, 11, 13, 17, 19  without the help of any device.

What if we want to list all the prime numbers inbetween 1 to 999? 🤦

We really need computer programming 💻 to do the job.

Types of programming languages

Broadly, there are two types of programming languages:


High-level language } c++, python, java ... programmer friendly Assembly language Machine language } Low-level languages processor friendly Computer Hardware

High level language

In high-level languages, instructions are mostly English-like, human-readable words. Generally, programmers do not require much knowledge about processors and hardwares of a computer. A high-level language is easy to read, write, learn, understand, modify and maintain. It is also portable, and can be used almost in any platform.

Consider the following python code:


  

  


We know that the above is not an English sentence, still it generates a feel of reading a sentence, having some meaning.

A large portion of the computer programs we see around us are written in high-level language.

They allow us to focus on how a problem can be solved, rather than on how a computer processor works internally.

One may become a full-fledged computer programmer without knowing any low-level language, if a good high-level language is mastered. Language like c++ even provides some low-level features in the form of high-level programming.



Low level language

In a low-level programming language, instructions are less human-readable, but more machine-friendly. Programmers require to have detail knowledge about processors and hardwares of a computer.

These languages are said to be closer or nearer to the processor and hardware of a computer.

So, they are easier for the CPU to understand and execute, but difficult for a human to read, write, learn, understand and modify.

Low-level languages require less memory and program is executed faster than any high-level language.

Low-level programs are less portable. Program written for one processor is difficult to execute with a different processor [processor-dependent].

Assembly language and Machine language are the examples of low-level programming language. Out of the both, assembly language is the easier one and more English-like. But high-level language is the most user-friendly and easiest too.




Any program written in a high-level or low-level programming language must be translated into machine code before execution of it.

A computer's CPU, in reality, understands nothing except 0 and 1.

High-level program Machine code

In the world of computer, whatever programming language we use, whatever operating system, whatever software, the ultimate result is the translated machine code.

In fact, the level of any computer language is determined on how similar it is to machine code, the lowest level of computer languages.



Differences between high level and low level languages

High level languageLow level language
Closer to human languageCloser to machine language
Further from machine languageFurther from human language
Programmer-friendlyProcessor-friendly
Very widely usedComparatively less used
Consumes more memoryConsumes less memory
More execution timeLess execution time
Easy to read, write, understandHard to read, write, understand
More portableLess portable
Platform independentPlatform dependent



Examples of Programs

Now with our earlier problem ...
How to list of all the prime numbers from 1 to 99?

Solution: write a program in any high level language that does this job. If you know programming, you can write a program to list all the prime numbers from 1 to upto a certain number. Any high level language is capable enough of doing the task.

The following python program will list all the prime numbers in between 1 and 99.

Python

A program written in python

By changing line 20 in this program, we can even find primes upto 10000, 100000 etc. As you can see, multiple statements have been stored in a .py file. Any time this program file can be loaded in memory and executed. The python interpreter translates each statement to machine language and passes to CPU for execution; an output is produced as a result. This is what we call a computer program (python program here).

Following is a javascript program that does the same task.

Javascript

A program written in javascript

Please observe that different programming languages have different styles and syntax, as well as statements. The purpose of the above two and following three programs are the same, and they produce the exact same outputs.


C++

A program written in c++

A program written in c++ language to display prime numbers.



Following two are written in java and visual basic languages.

Java

A program written in java


Visual Basic

A program written in visual basic



In the above, programming languages have been classified into two categories, high-level and low-level languages. This classification is done mainly on the basis of the proximity of a language to the machine codes w.r.t the wordings and syntax.

Programming languages can also be broadly categorized into procedural, functional and object-originated languages depending on their use of procedures, subrourines, functions and objects containing data and code.




--- x ---









Want to leave a message for me?