A Brief Introduction to Programming11 min read
In this article, I will give you a brief introduction to programming, learn important concepts related and how a program can be translated that computers can understand.
First, what is a program?
A program is simply a collection of statements or steps that solve a problem and need to be converted into a language that computer can understand (we will discuss this in a later section) to perform one or more tasks. The statements usually are written in languages (such as Python, Ruby, JavaScript, Java, C, C++, etc..) that humans can understand but not computers. The statement is entered as a logically ordered set (also call algorithm). In order for a computer can understand and execute the algorithms, the set of statements needs to be converted into a language that the computer can understand. This conversion process uses an interpreter or compiler.
As you probably know, we count number on base 10, but for computer, only language the computer can understand is called “binary” and it’s just the combination of ones and zeroes.
What is an interpreter?
An interpreter is a separate program that is needed in order for a program to run. It performs exactly as its name implies—it interprets the program’s statements one by one into language that computer can understand and acts as a translator between the program and the computer.
What about the compiler?
A compiler, on the other hand, reads all of the statements from the program and converts them into a computer language. Thus the finished compiled program stands on its own and does not need an interpreter.
What programming language to speak with the computer?
The first step in programming is to determine what language you want to use to communicate with the computer. As stated earlier, your computer speaks only one language–binary. All of the other programming languages that are used by humans to create computer programs end up in binary. But remember, no programming language is the king and has the ultimate power, each has its own weakness and strength. So in actuality, you can choose whatever programming language suits your needs and meets your preferences.
Below are few programming languages that I recommend:
- Python
- JavaScript
- Ruby
- C#
- Java
- C++
- C
- Assembly
- etc…
- Python is well known for working with artificial intelligence and machine learning.
- JavaScript is the indispensable part of web development.
- Java is used to the mobile app and also a web application.
- C# for creating the desktop app and game application.
- Ruby also is utilized in web development with Ruby on Rails framework.
- C++ is geared toward game development and embedded systems.
- C is a programming language that beginner should learn.
- Assembly 😀 is a lowest-level language also a high-level language that works well if you want to control hardware. (We’ll talk about this in a later section).
Hello, World program in all of the mentioned languages
Python:
print("Hello, World!")
JavaScript:
document.write("Hello, World!");
Ruby:
puts "Hello, World!";
C#:
namespace HelloWorld
{
class Hello {
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
Java:
class HelloWorld {
public static void main (String args[]){
System.out.print("Hello, World!");
}
}
C++:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
C:
#include <stdio.h>
main(){
printf("Hello, World!");
}
Assembly:
mov ah, 13h
mov dx, 0C00H
mov cx, 30
mov al, 00
mov bh, 00
mov b1, 1fH
mov ah, 13H
lea bp, [Msg]
int 10H
int 20H
Msg: db 'Hello, World!'
EXE_End
What are low, high, machine, assembly and assembler languages?
- Low-level language is a programming language that resembles more what a computer can understand rather than human.
- A high-level language is a programming language that is more human-friendly or more natural for humans to read.
- Machine language is the lowest-level language, which is directly understood by the computer.
- Assembly language is a language that resides in between the lowest-level language and higher-level language, it assigns letter codes to each machine language instruction.
- Assembler reads the assembly language code and converts it into the machine language.
Because of low-level language is a language that computer can understand almost directly so the execution time and processing time is really fast. Vice versa, with high-level programming languages, because it’s designed for human and computer just can understand it through the compiler or interpreter then the progressing time will be much slower.
Because of this article’s characteristic, we’ll dig more about some statements from these type of languages (assembly, low-level, high-level) to broaden a new horizon to see how different between them.
Low-level languages
As we talked about earlier, machine language is the lowest-level language, which just consists of binary numbers zero and one. Because just very few of people they are actual code in machine language, so we won’t go to detail how it works.
Assembly language statements
Why learn Assembly?
- Understand how things work underneath
- Learn the basic organization of the underlying machineo
- Learn how the computer actually runs a program
- Design better computers in the future
- Write faster code (even in high-level language)
- By understanding which high-level constructs are better… in terms of how efficient they are at the machine level
- Some software is still written in assembly language
- Code that really needs to run quickly
- Code for embedded systems, network processors, etc.
In order to understand how assembly works you need to learn how the computer works as well. CPU (Central processing unit) is the brain of the computer which is the electronic circuitry within a computer that carries out the instructions of a computer program by performing the basic arithmetic, logical, control and input/output operations specified by the instructions. Inside a CPU it has 3 main components:
- control unit—It tells the computer’s memory, arithmetic, and logic unit and input and output devices how to respond to the instructions that have been sent to the processor.
- ALU (Arithmetic logical unit)—which performs data processing such as bitwise operation and arithmetic.
- Register—provide storage internal to the CPU.
If you are a computer science student, it’s really beneficial to learn assembly as the first language, although it’s really challenging. But when you do it well, you understand how the computer works as well.
The assembly language consists of alphabetic instructions that are converted into machine language. Consider the following example:
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg ;length of the string
If you had some experience with some of other high-level languages, you may feel WOW, Assembly looks so cryptic, it’s hard to read and understand by humans, but easy for machine. 😀
After semicolon
In assembly, everything after the semicolon is consider the comments, which mean it will be ignored and those texts just created by you to tell how this program works.
mov
The mov statement moves values around, for example:
mov cx, 7
tells the assembler to move the value of 7 into the cx register.
add
The add instruction takes a value on the right and adds it to the value on the left.
mov cx, 2
mov dx, 7
add dx, cx
First, It moves value 2 from cx
register then moves value 7 to dx
register and the last add them together right to left and store the value–9 to the dx
register.
inc
This instruction adds (increase) one to the register being used.
inc dx
Now the value of dx
register is increased one, 10.
sub
This instruction tells assembler to subtract one number from another number.
mov cx, 7
mov dx 2
sub cx, dx
The first statement moves a value of 7 to cx
register, second move 2 to dx
register and last it subtracts the value of dx
from cx
and places back the result to cx
register but dx
register still has a value of 2.
cmp
This instruction compares two values, it works like subtracts but does not store the value in the register. Instead, the result is used to set flag bits in the flags register (FL).
mov cx, 3
mov dx, 7
cmp dx, cx
The last statement will compare the value of dx
and cx
registers. If dx
– cx
equals 0 then zero flags is set and this will determine whether or not you will jump into next instruction.
High-level programming languages
High-level programming languages allow you to write program are more understood by humans. High-level languages are easier to read, write, and maintain than low-level languages. However, high-level languages are pretty slower because they must either compiled or interpreted.
In recent years, working with high-level languages that you more or less have to use IDE (integrated development environment)—An interface provided with software development languages that incorporates all of the necessary tools needed to write, compile. And also, people are working with web development now tend to use code editor, such as Visual Studio Code—which hasn’t a compile but it built-in many extensions to do the jobs that programmers need.
I’ve given some examples about the programs in assembly language, now let’s try something in one high-level language and see how different between them.
This program will return an array with ascending order, it’s also called bubble sort. This is how this program will be written in JavaScript:
var a = [33, 103, 3, 726, 200, 984, 198, 764, 9];
function bubbleSort(a) {
var swapped;
do {
swapped = false;
for (var i=0; i < a.length-1; i++) {
if (a[i] > a[i+1]) {
var temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swapped = true;
}
}
} while (swapped);
}
bubbleSort(a);
console.log(a); // Expected output: [3, 9, 33, 103, 198, 200, 726, 764, 984]
The program’s structure
Before you begin to write a program in any programming language, you must first determine how the program should work and how the language syntax should be incorporated. Before you implement the syntax of a programming language you need to understand how a program is structured first. The structure of a program is based on algorithms, often represented using flowchart or pseudocode.
Algorithms
Do you still remember an example that I have given below that return an array in ascending order in JavaScript? This one is an algorithm with a name called Bubble Sort. When you are presented with any kind of problem, you need to figure out what is the right method to solve it. Let’s pretend that you want to get A grade in an introductory computer science course at your university, so you need to jot down a plan and follow it. A wise plan may include: attending lectures, taking notes, reading books, doing the assignments, doing puzzles and tests, completing the final exam.
This set of plan is your real life algorithm. For many things you do in your life, you might logically plan out a series steps in order to accomplish what you want to archive.
In programming, an algorithm is a word that people often tell others about their work, just kidding 😀 . An algorithm is a method for solving a problem, the algorithm consists of executable steps that you need to follow to solve the problem.
Pseudocode
Besides algorithms, pseudocode is a part of the program’s structure which is a readable description of an algorithm written in human language that can easy to understand. Pseudocode can be considered a template for what needs to be converted into programming language syntax.
Pretend we have the pseudocode entering the Celsius or Fahrenheit to convert them to each other, this is how we implement to solve this problem:
Menu:
Do you want to perform a conversion
If Yes, then:
Which conversion:
If Celsius to Fahrenheit then:
Go to Fahrenheit section
If Fahrenheit to Celsius, then:
Go to the Celsius section
Else If, No, then:
Exit the program
Celsius section:
Ask the user for Fahrenheit temp
Apply the entered temp to the formula Celsius
Temp = (5/9) * (Fahrenheit Temp - 32)
Display Fahrenheit Temp XX converted to Celsius is YY
Return to Menu
Fahrenheit section:
Ask for Celsius temp
Apply the entered temp to the formula
F temp = ((9/5) * Celsius Temp) + 32
Display Celsius Temp XX converted to Fahrenheit is YY
Return to Menu section
There are many ways that you can perform an algorithm through the pseudocode to archive your goal. But as a programmer, your job is to find out is design the best way which is better than others to solve a problem or handle projects. The selection of a particular algorithm can be based on a myriad of deciding factors. It is wise to spend time investigating all the options before you begin implementing the algorithm.
Conclusion
In this article, we’ve been through something under the hood in the programming world, see its structure and we’ve got accustomed to some concepts like low-level languages, high-level languages and machine languages, see how the program could be constructed before we implement. We take a look at the fundamental term about algorithm and pseudocode and the job of a programmer is to find out the best way to solve a problem, not just make it work.