5 Basic Concepts of any Programming Language

A computer program is a collection of instructions that performs a specific task when executed by a computer. A computer requires programs to function, and typically executes the program’s instructions in a central processing unit.

A computer program is usually written by a computer programmer in a programming language. From the program in its human-readable form of source code, a compiler can derive machine code—a form consisting of instructions that the computer can directly execute. Alternatively, a computer program may be executed with the aid of an interpreter.

The first programmable computers required the programmers to write explicit instructions to directly manipulate the hardware of the computer. This “machine language” was very tedious to write by hand since even simple tasks such as printing some output on the screen require 10 or 20 machine language commands. Machine language is often referred to as a “low level language” since the code directly manipulates the hardware of the computer.

By contrast, higher level languages such as “C”, C++, Pascal, Cobol, Fortran, ADA and Java are called “compiled languages”. In a compiled language, the programmer writes more general instructions and a compiler (a special piece of software) automatically translates these high level instructions into machine language. The machine language is then executed by the computer. A large portion of software in use today is programmed in this fashion.

There are two different models of programming-

  1. Structured programming– In this type, code is executed one after another. Control statements change which blocks of code are executed next. It is aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops
  2. Object oriented programming-It is based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object’s procedures can access and often modify the data fields of the object with which they are associated. There is no single “flow” of the program as objects can freely interact with one another by passing messages.

1. Program Structure

The overall form of a program, with particular emphasis on the individual components of the program and the interrelationships between these components. Programs are frequently referred to as either well-structured or poorly structured. With a well-structured program the division into components follows some recognized principle such as information hiding, and the interfaces between components are explicit and simple. By contrast, with a poorly structured program the division into components is largely arbitrary (or even non-existent), and interfaces are implicit and complex. At a finer level, a well-structured program employs appropriate data structures and program units with a single entry point and a single exit point, while a poorly structured program has arbitrary data structures and flow of control.

Virtually all structured programs share a similar overall pattern:

  • Statements to establish the start of the program
  • Variable declaration
  • Program statements (blocks of code)

This is the basic “hello world” example used in all programming languages.

Language Example program
“C” #include <stdio.h>
void main() {
printf(“Hello World”);
}
C++ #include <iostream>
int main() {
cout << “Hello World”;
return 0;
}
Pascal program helloworld (output);
begin
writeln(‘Hello World’);
end
Oracle PL/SQL CREATE OR REPLACE PROCEDURE helloworld AS
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Hello World’);
END;

2. Variable Declaration

variable

A variable or scalar is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value. Variables are the backbone of any program, and thus the backbone of any programming language.in simple terms, a variable is simply a way to store some sort of information for later use, and we can retrieve this information by referring to a “word” that will describe this information.

Different types of variables include:

  • Integer – to store integer or “whole” numbers
  • Real – to store real or fractional numbers (also called float to indicate a floating point number)
  • Character – A single character such as a letter of the alphabet or punctuation.
  • String – A collection of characters

In order to use a variable within a program, the compiler needs to know in advance the type of data that will be stored in it. For this reason, we declare the variables at the start of the program. Variable declaration consists of giving a new name and a data type for the variable. This is normally done at the very start of the program..

For example:

#include <stdio.h>
void main() {
int age;
float salary;
char middle_initial;
age = 25;
salary = 196578.89;
middle_initial = "K";
printf("I am %d years old ", age);
printf("I make %f per year " salary);
printf("My middle initial is %c ", middle_initial);
}

3. Looping structures

Loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.

The following illustration shows a loop structure that runs a set of statements until a condition becomes true.

Running a set of statements until a condition becomes true

An infinite loop is one that lacks a functioning exit routine . The result is that the loop repeats continually until the operating system senses it and terminates the program with an error or until some other event occurs (such as having the program automatically terminate after a certain duration of time).

4. Control structures

A control structure is a block of programming that analyses variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control “flows”). Hence it is the basic decision-making process in computing; flow control determines how a computer will respond when given certain conditions and parameters.

When a program is running, the code is being read by the computer line by line (from top to bottom, and for the most part left to right), just like you would read a book.  This is known as the “code flow“, now as the code is being read from top to bottom, it may hit a point where it needs to make a decision, this decision could make the code jump to a completely different part of the program, or it could make it re-run a certain piece again, or just plain skip a bunch of code.  The computer program has a strict set of rules to decide which direction to go. So, this decision that must be made, that will in turn affect the flow of code, is known as a control structure.

5. Syntax

In computer science, the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. Documents that are syntactically invalid are said to have a syntax error. The concept of syntax in programming language is similar to the concepts of grammar and spelling in spoken language. When a sentence in English has very poor grammar and spelling, it becomes difficult or even impossible to understand. Similarly, when code has syntax errors, the program will not execute.

The difference is that when you read a sentence in English with a minor error, you can typically still understand its meaning. Very small syntax errors in code, however, make the program unusable. Coders, therefore, have to pay great attention to detail to make sure their code is not only logical but also free of syntax errors. There are a number of aspects to syntax, including statements, variables and keywords.

Computer language syntax is generally distinguished into three levels:

  • Words – the lexical level, determining how characters form tokens;
  • Phrases – the grammar level, narrowly speaking, determining how tokens form phrases;
  • Context – determining what objects or variables names refer to, if types are valid, etc.

An example of this in the case of Java would be round brackets (), curly brackets {}, and variables, among other things.

Now,after learning the 5 basic concepts of any programming language.

Also find out:  5 Most common Programming mistakes to avoid