Introduction
I had several discussions with non-programmers and they usually ask me questions about my job.
I noticed that several things that can be obvious for a programmer need to be explained in detail for a non-technical person. I noticed also that some people want to jump into the world of programming but find it complex or late to start.
In this article, I will prove you that programming is for everyone. We will see the basic things to know how to be able to write a computer program. We will define what means a computer program, what is it used for? And what do we need to create it? If you are a beginner and you don’t know anything about programming, then this article is for you.
In this article we will start asking the question that can be obvious, but we will see that they contain several details. Since there are several articles on the internet that help you to start to learn a specific programming language. This article will not focus on technical instruction to write your first program (you can find a lot of hello world samples on the web). I tried in this article to give you visibility about the environment that you will be using. Then when you'll start the technical parts you'll understand the general concept of programming and what you’ll be doing. So let’s say this article is step 0 or it's a kind of discussion about programming around a cup of coffee.
Audience
- Everyone
- People who want to change their career and start programming
- Students that finished high school and asking what field to choose.
Prerequisite
What is a Computer Program?
A computer program in a point of view of a non-programmer is an executable that can be run and which gives a specific result. You can consider the video games that you play or your text editor as a program. Which is right.
But in the point of view of a programmer, the executable is considered as a part of a whole ensemble that constitutes a program.
Indeed, a program is a number of instructions, written in a specific language and understood by a specific compiler that gives an output that is compliant with a certain need.
The phrase is a bit complex? If yes don’t worry the next sections will make it clearer.
Why do we program?
Sometimes, we have some needs to do some complex sub-tasks to achieve a global task. Each sub-task will take a lot of time so the global one will have a bigger amount of time.
For example, to calculate the net salary from the gross pay (the global task) we need to do several calculations (sub task). If we do it manually for a big number of employees it will be a very long task.
This is why we make a computer program that covers this need. And here we are explaining the part of the definition in the first section "that gives an output that is compliant with a certain need.”
What are the elements needed to write a program?
So now that we know what we are talking about, we will see the ingredients to make a program.
A programming language
We said at the first section “A program is a number of instructions, written in a specific language”. These instructions are written in the form of an algorithm*. Writing an algorithm means that we have to write a set of steps that represents our reasoning. This is a logic that is largely used in mathematics.
Let's take again the example of the salary calculation.
Let's suppose that the gross pay of a person is 5000 dollars, we need to subtract 10% to the social security and 20% taxes to get the net salary. So let's say it's a mathematical function f(X) = X- X*0,1 - X*0,2 where X is the Gross salary.
So logically we will process this way,
- Calculate the amount to pay to the social security : SS = 5000 * 0,1 = 500
- Calculate the taxes : Tx = 5000 * 0,2=1000
- Calculate the Net Salary : NS = 5000 - 500 - 1000 = 3500 dollars
Here in our mind we executed an algorithm with three simple steps.
And if we translate it to a code in the c# language, it will be as follows :
//Declaration of our variables
double grossSalary = 5000;
double socialSecurity;
double taxes;
double netSalary;
//Calculate the Social security
socialSecurity = grossSalary * 0.1;
//Calculate the Taxes
taxes = grossSalary * 0.2;
//Calculate the net salary
netSalary = grossSalary - socialSecurity - taxes;
//Show the result
Console.WriteLine("The Salary is " + netSalary);
Of course, we can simplify it in only one operation but I just wanted to explicitly show the algorithm of our way of thinking in the code.
If you are still not familiar with code, it doesn't matter, we will see the general structure in the section, What is the General Structure of a program?
Above we just gave a simplified definition of an algorithm. But what is a programming language?
We can tell to the computer to execute these three steps, but we cannot tell it in English, Arabic or French. We need to tell it in a language that it understands. Here comes the programming language.
*PS: If you want to have an idea about the history of the word algorithm, it is derived from the name of the scientist Al-lkawarezmi (the founder of algebra).
There are several languages C, C++, C#, Java, PHP, VB, python, assembler, ABAP ….. How do we choose a language?
As in the human life, the language changes according to the environment, for example people in the Middle East and in North Africa talk the Arabic language, others talk English in America, Hindi in India …
It is exactly the same for programming languages. We use the best suitable one for the environment. For example, if we are using Microsoft, it’s better to use c#. If we want to make a program that interacts directly with the hardware, C language is the best choice (for developing drivers for example or electronic components …), but if we want to develop a program that gives a user some functionalities, it is may be to do it with javascript or another one that can interact with tools that provide a nice user interface features ...
Once we understood what means a programming language, we have to use it. When we write words in a specific language, we obtain a text that is called Source Code. The Source Code is the representation of an algorithm in a specific programming language.
But… there still is something missing …How is this Source code understood by the machine? Can we write the source code in Microsoft Word and then execute it?
A compiler
In this section, we will detail the part "understood by a specific compiler" of our definition in the first section.
When you buy a new computer, it will not be able to understand the source code that you write. You need to install some tools to give your code some meaning.
So first thing to do when we want to become a programmer is to choose the language that we want to use. Then prepare the computer by installing tools that will allow us to program.
These tools are the editor and the compiler.
The compiler will check everything you code; it will parse your code and analyze it, if there is an error, it will indicate the line and you need to repair it to be able to run your program.
The Role of the compiler is to do,
The lexical analysis
It checks all the lexical errors, it means that it checks that every word that you wrote in your source code is correct and belongs to the programming language.
Let’s suppose that there is a compiler for English:
If I say: I’m very happy, this will compile fine.
If I say: I’m very ffkubhjhg a Lexical Error in compilation (only if the word ffkubhjhg exists in English and I don’t know it)
If we take the example of C# language, if we want to show something in the screen we use the predefined method Console.WriteLine(“Hello”); here it compile, if we write Bonsole.WriteLine(“Hello”); it will give a lexical error.
The syntactic analysis
The syntactic error happens in the order of words in a written code.
For example, if we suppose again that there is an English compiler.
If I say: I eat an apple and drink a bottle of water => it compiles fine
Apple eat an I a bottle drink of water => syntactic error.
Same thing if we take the example of printf in C# language, if I say Console.WriteLine()”Hello”; there will be a syntactic error.
The semantic analysis
The compiler ensure that our code will run without problems during execution. So it performs a semantic analysis.
Not all semantic errors are detected by the compiler and they are commonly raised as warning because they are generally errors that happen in execution.
To make it simple, The code that we write must be sensible. It means that even if we write a code using the right keywords and in the right order. It needs to have a right sense
For example, if I say The chicken is giving milk to his baby. It is lexically and syntactically right. But it doesn’t make sense.
Exactly in programming, when you perform a calculation that in some level it tries to divide a number by 0, this will generate problems in the execution.
This is a piece of program that can give an error, again, if you don’t understand it doesn’t matter the section What is General Structure of a program? Will make it clear
int x=0;
int a;
int b=5/x;
=> Semantic error
Code Generation
What we write in a specific language is the source code. The computer doesn’t understand it. So the compiler after verifying that what we wrote is correct, generates a code that is understood by the machine which is called machine code.
Finally we will use the compiler as a tool, here we just explained its role to be aware of them but these roles will be a black box for us.
Editor
We cannot write our source code anywhere. Microsoft word is not the right place to do it.
Here you have two alternatives,
- You can choose a very basic text editor and write your code then in the command line use the compiler. C:\>Compiler MyProgram.c But here you need to be a very patient person. Because this method is not user friendly at all.
- Or we can get a tool that encloses all that we need: compiler, assistance while writing and a very good organization.
Big companies and free software community offer a big variety of editors: Eclipse (free), Visual Studio (Microsoft Licence & Community free version), Visual Code (free), DevCpp (free)…
The editor to choose depends on several parameters like the language, the need ...
What is the General Structure of a program?
The structure of the program may vary according to the environment you use, but here we will define the most standard and basic one.
External uses
In the source code, we can use some pre-defined things. We need to specify the place from which we are importing them.
The first section in the source code is the definition of the external uses of the current file.
Entry point
It is simply the point from which the program starts. The beginning point must be specified in a clear and common way for a program.
It depends also on the kind of the project you create, if you create a console Application in C#, the entry point is a file called program.cs that has a specific main method on it.
Declarative section
Once the entry point is specified, we can start programming. For example, we want to calculate (x*13)+7 for x =1.
We write,
X=1;
Y =(x*13)+7; but what are x and y ?
We need to tell to the program that we will use x and y and they are integer. This is made in the first section of a C# program. And the things that we declare there are called variables. Because we can change then content whenever we want (only for special cases).
Int x;
Int y; // Declarative section
x=1;
y =(x*13)+7;
Treatment section
It contains all instructions and operations that we want to do. Each operation needs to finish with a ; then the compiler will understand that he is compiling the next instruction.
In all the sections of the program, the programmer can call predefined methods or classes (which are data provided by the framework of programming that you use) or some keywords which are words that exist in the programming language and are automatically understood.
Conclusion
Let's say this is just a very general Idea about what programming is and I really hope that it will help you in your next step of your journey as a programmer.