02 Programming Languages Overview
02 Programming Languages Overview
Computer
Programming
2021
Things don’t happen automatically within a computer. Whatever a
computer does, whether it seems automatically or not, originates
from instructions given to that computer by someone.
A computer program consists of code and data. Data can be represented using different data
types. Code can be classified into functions and modules.
Data
Instructions
An instruction is an action which the computer can perform. When a computer receives a
particular instruction, it carries out that instruction in a certain, specific and predetermined way.
Functions
Sometimes, a certain set of instructions may need to be issued to a computer more than once,
for any kinds of reasons and purposes. Whatever the case, these instructions can be grouped
and discretely identified for later use.
A function is a collection of reusable instructions (code) that is given a name to identify it.
Anytime that collection of instructions (function) needs to be used, it is invoked or called using
its name.
Some functions work with extra information, also known as arguments or parameters. These
are passed to the function when it is invoked (if needed). For example, if a function is created
to add 2 numbers. Definitely, that function will need to know what the 2 numbers are.
Additionally, when a function completes its execution (depending on what the function does),
it gives back information or results. For example, if a function which adds 2 number is called,
arguments are passed as it is invoked. When that function completes its task, it is expected to
give back the result of its operation, i.e the sum of the 2 numbers.
A function which does not return results after executing is called a sub-routine in Visual Basic
and is defined using the Sub keyword. Such a function is called a void function in C# and other
C-like languages.
A function which returns results after executing is called a function in BASIC and is defined
using the Function keyword. Such a function is simply a non-void function in C# and other C-
like languages.
Identifiers
Data can be given a name to make it easy to reuse the data and to reference it in future.
Functions can also be given names for reuse.
Identifiers can be classified into variables and constants. A variable is data that can/might
change its value anytime. An example of a variable is a person’s age which changes every year
or time which changes in fractions of a second. On the other hand, a constant is data that will
not change at any time. An example of a constant is a person’s birthday, or the year a certain
country got independence because it never changes.
Data Types
Data is classified into many types. Each type has to be chosen according to the purpose. Here is
a list of the primitive data types supported in Visual Basic and C#
Boolean
Integer
An integer data type holds whole numbers only, either positive or negative
Floating Point
Floating point data types hold numbers that have fractions or a decimal point. The term
floating point is used to indicate that the values contained in floating point numbers are
actually an approximation of the real value.
For example 1/3 = 0.3333333333, in other words 1 divided by 3 is 0.3333333333. But if we are
to say 0.3333333333 x 3, we will get 0.9999999999 instead of 1.
Characters
To overcome this challenge, engineers agreed to assign a specific number to each character so
that characters like the alphabet can be represented. The computer will process them as
numbers but they will be interpreted as characters.
One of the most common character mapping used on computers is the ASCII standard
mapping.
String
A string is a group (sequence) of characters in series. A string can be used to hold a sequence
of characters, a word, a sentence, paragraph and so on.
Literals
Data can be represented in many ways inside a computer program. Data can be represented by
an identifier which is either a constant or a variable. An example is
Data can also be represented using literals. A literal is a value that should be interpreted as it is.
Instead of using names like above, a computer program can represent data directly by value
like this:
“Alexander”
19
When the compiler finds characters inside double quotes, it infers that the data type is a string
and treats it as it is. In-order to denote a variable named Alexander, the variable must be stated
without the quotes.
When a compiler finds the value 19, it just interprets it as it is, literally.
Control Flow
Flow Control
The flow of a program is the order in which statements or instructions of that program are
executed. Normally programs are executed from top to bottom in the order in which they are
written. Some instructions or statements can be used to alter this order. This can be for various
reasons listed below:
Iterative statements
Iterative statements are used to execute a group of instructions multiple times. This maybe for
various reasons. A group of instructions which can be executed multiple times is called a loop
Bounded Loops
Bounded loops are executed repetitively in a predictable way. An example is a loop which
should repeat 5 times, is predictable in that the amount of time that loop will run can be
calculated in advance. The for-loop is used for a loop that runs a specific number of times.
statements
Next
statements
The for-loop repeats a group of statements within its scope. A start value and end value are
required to use the for loop. This range of values tells the computer to count from the start
value to end value. counter is the variable which will contain the current value of the iteration.
The first value given to the counter variable is start. Then the computer either increases or
decreases the value in counter by 1 until counter is equal to end. When counter is equal to
end, then the loop terminates.
Dim i As Integer
For i = 1 To 100
Console.Write(“Iteration ”)
Console.WriteLine(i)
Next
A C# example is as follows:
int i;
For (i = 1 ; i < 100; i++) {
Console.Write(“Iteration ”);
Console.WriteLine(i);
}
The previous block of code will cause the statement to be printed 100 times. The print
statement will cause the Console to display messages as follows:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 99
Iteration 100
Infinite Loop
An infinite loop is a loop which runs endlessly. This happens because the given condition will
never be met
Unbounded Loop
An unbounded loop is a loop that has no predictable lifespan. A for-loop can be predicted
because it only runs a specific number of times. There are other loops that do not have a fixed
set of values within which to iterate. The while-loop is used to implement unbounded loops.
The while loop tells a computer to repeat a group of statements until a certain condition is met,
or while a certain condition is true. The reason why such loops are unbounded and therefore
unpredictable is that, there is no way to know when that condition will be met. For example,
When a computer program is waiting until it gets an internet connection, it may wait for
seconds, or minutes, hours or even days or weeks. There is no telling when the internet will be
connected. Therefore such loops are unpredictable, because it could be a service provider
problem, subscription issue or weather.
Conditional Statements
Conditional statements are used to control when a certain block of instructions should be
executed. This allows decisions to be made before a computer can take certain actions. An
example might be, the computer must show the documents and files only when a user is
logged in, to prevent unauthorised access. This would mean, for a user who hasn’t logged, the
computer system would show a login screen asking for a password, but if the user is logged in,
he/she is given access to documents.
Visual BASIC
If condition [ Then ]
[ statements ]
[ ElseIf elseifcondition [ Then ]
[ elseifstatements ] ]
[ Else
[ elsestatements ] ]
End If
C#
if (condition) {
[ statements ]
}
[ else if (elseifcondition) {
[ elseifstatements ] ]
}
else {
[ elsestatements ] ]
}
Procedures
A procedure is a reusable group of instructions which can be separately identified and invoked.
A procedure is written once but can be invoked many times. It is used to identify and manage
code blocks that occur many times within a program.
The advantage of procedures is that they avoid repetition, and therefore minimize errors. If a
block of code contains errors, but that block of code is repeated throughout the program, it is
difficult to completely correct the error, and the error will have to be corrected in many places.
A procedure on the other hand allows a code block to be written once and used many times.
Any correction in that block of code only happens.
Procedures also make it easy to separate and identify code blocks that perform different tasks.
This makes source code readable and easy to modify or manage.
There are 2 kinds of procedures in BASIC. The first type is called a sub-routine and the
second one is called a Function.
A sub-routine is a procedure that only performs a task, but does not have to give back a
response. It simply carries out a straightforward operation without giving feedback.
Sub SubName[(parameterList)]
' Statements of the Sub procedure.
End Sub
parameterList is a list of variables that can be sent to the subroutine. This is normally
additional information. For example. Console.WriteLine is a sub-routine. This sub-routine
accepts a string so that it prints that string. That is why we write it as
Console.WriteLine(“Hello World”)
A function unlike a sub-routine also has a ReturnType. This is the data-type of the results after
the function executes. Let us say the function adds 2 integers to produce another integer. The
resulting integer is returned after the function therefore the function’s ReturnType is integer.