0% found this document useful (0 votes)
3 views

PROGRAMMING-

The document provides an overview of computer programming, explaining its purpose, the role of programmers, and the importance of software. It outlines the differences between high-level and low-level programming languages, detailing examples like C++, and discusses the necessity of translators such as compilers and interpreters. Additionally, it introduces basic C++ syntax, variables, and user input methods.

Uploaded by

bridget3203
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PROGRAMMING-

The document provides an overview of computer programming, explaining its purpose, the role of programmers, and the importance of software. It outlines the differences between high-level and low-level programming languages, detailing examples like C++, and discusses the necessity of translators such as compilers and interpreters. Additionally, it introduces basic C++ syntax, variables, and user input methods.

Uploaded by

bridget3203
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PREPARED BY MR B

PROGRAMMING
Computer programming (often shortened to programming) is the process of
writing and designing computer programs. In other words, it’s a process that
leads from an original formulation of a computing problem to executable
computer programs.

The purpose of programming is to find a sequence of instructions that will


automate a specific task or solving a given problem. The process of programming
thus requires that one understands clearly in mind what they want the computer
to do. This means, you as a programmer must know how that task is done
manually. It’s the same manual process that we instruct the computer to carry
out tasks.

Computers can do such a wide variety of things because they can be


programmed. This means that computers are not designed to do just one job,
but to do any job that their programs tell them to do. A program is a set of
instructions that a computer follows to perform a task, or in other words a
program tells the computer what to do.

For example, two commonly used programs, Microsoft Word and Adobe
Photoshop. Microsoft Word is a word processing program that allows you to
create, edit, and print documents with your computer. Adobe Photoshop is an
image editing program that allows you to work with graphic images, such as
photos taken with your digital camera.

Programs are commonly referred to as software. Software is essential to a


computer because it controls everything the computer does. All the software that
we use to make our computers useful is created by individuals working as
programmers or software developers.

A programmer, or software developer, is a person with the training and skills


necessary to design, create, and test computer programs. Computer
programming is an exciting and rewarding career. Today, you will find
programmers’ work used in business, medicine, government, law enforcement,
agriculture, academics, entertainment, and many other fields.

What it takes to Program


Programming really isn't that difficult or mysterious. If you can write step-by-
step instructions directing someone to your house, even if your house has no
house number or street name, you can write a program. The same way you write
a recipe for someone on how to cook nshima, whether with Millie meal or
cassava, you can also write a program.

1|Page
PREPARED BY MR B

The hardest part about programming is identifying all the little problems that
make up the big problem that you're trying to solve. You should not assume that
the computer will do some things for you!! Because computers are completely
‘stupid’, you need to tell them how to do everything. For example, if you want to
tell your friend how to prepare a meal say, nshima, you don’t have to tell them
all the details.

For the computer however these may not be clear. You need to specify how much
Millie meal, how big should the pot be, how long should the water boil? Unless
you specify everything that you want the computer to do and exactly how to do
it, the computer on its own will not know how to do what you want it to do. Here
is another truth that you need to bear in mind: computers do not speak the same
language we speak. It’s vital for a programmer to understand how data is
represented in a computer.

Computer Programming Languages

The two major categories of programming language


There are two types of programming languages, which can be categorized into
the following ways:
✓ High level language.
✓ Low level language
Low Level Language
Low Level Language: Also called Machine Language is very difficult to learn
because it consists of 0's and 1's or very few English words.
Example: Machine Language (Binary)
Computers can only execute programs that are written in Binary (0s and 1s)
called Machine Language (Low Level Language), which makes their operations
different from the perception of humans. A program can have thousands or even
millions of binary instructions and writing such a program would be very tedious
and time consuming. Programming in machine language would also be very
difficult because putting a 0 or a 1 in the wrong place will cause an error.

Low Level Language is a collection of very detailed instructions that control the
computer’s internal circuitry. Low Level Language is the native language of the
computer.
The CPU only understands machine language, so a special program known as
an Assembler is used to translate an assembly language program to a machine
language program.

2|Page
PREPARED BY MR B

High Level Language


High Level Language: Is easy to learn because it consists of simple English
words, so we can easily understand and do programming using HLL.
Example: C++, Java, Python
In the 1950s, a new generation of programming languages known as high-level
languages began to appear. A high-level language allows you to create powerful
and complex programs without knowing how the CPU works, and without writing
large numbers of low-level instructions. In addition, most high-level languages
use words that are easy to understand.
For example, if a programmer were using Python, he or she would write the
following instruction to display the message Hello world on the computer screen:

PRINT "Hello world"

In C++ you would display the message Hello world with the following instruction:

Cout <<'Hello world'>>;

Doing the same thing in low level programming language would require several
instructions, and an intimate knowledge of how the CPU interacts with the
computer’s output device. As you can see from the examples above, high-level
languages allow programmers to concentrate on the tasks they want to perform
with their programs rather than the details of how the CPU will execute those
programs. Since the 1950s, thousands of high-level languages have been
created.
Think of a programming language the same way you think of the different
languages you know e.g. Bemba, Tonga, English, Lozi, French etc. Each of these
languages can be used to make the same instructions but not using the same
grammar. That is the idea behind programming languages. A programming
language has a vocabulary and set of grammatical rules for instructing a
computer to perform specific tasks.

Types of programming languages (High Level)

The term programming language usually refers to high-level languages, such as


C, C++, Pascal, Java, Python, Java Script, COBOL, FORTRAN, Ada, and so on.
These are quite advanced languages, at least above machine code, because they
are able to use languages which can readily be real to humans. Just as people
speak many languages, you should understand there are different kinds of
programming languages.

3|Page
PREPARED BY MR B

C++ Programming Language

C++ is a cross-platform language that can be used to create high-performance


applications. C++ was developed by Bjarne Stroustrup, as an extension to the C
language.

C++ gives programmers a high level of control over system resources and
memory. The language was updated 3 major times in 2011, 2014, and 2017 to
C++11, C++14, and C++17. C++ is one of the world's most popular programming
languages. C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.

The C++ language consists of two basic elements:

• Semantics: This is a vocabulary of commands that humans can


understand and that can be converted into machine language, fairly easily.
• Syntax: This is a language structure (or grammar) that allows humans to
combine these C++ commands into a program that actually does
something.

Think of the semantics as the building blocks of your C++ program and the
syntax as the correct way to put them together.

To start using C++, you need two things:

• A text editor, like Dev-C++, Code Blocks, Notepad and so on, to write C++
code
• A Translator

However, all editors include a compiler (or interpreter) and memory handling
features.

Need for a Translator


A Translator is itself a computer program. It accepts a program written in a high-
level language as input and generates a corresponding machine-language
program as output. The original high-level program is called the Source Code.

A program that is written in a high-level language must, however, be translated


into machine language before it can be executed. So, a small program (which
comes with every text editor) comes into picture which is called as translator. A
translator converts High level language into low level language and vice – versa.
There are two types of translators: Interpreters and Compilers.

4|Page
PREPARED BY MR B

Interpreters

Interpreters proceed through a program by translating and then executing single


instructions or small group of instructions. An Interpreter takes a program and
its input at the same time. It translates the program, implementing operations
as it encounters them, and doing input/output as necessary. One main
advantage of an interpreter is that execution as well as syntax errors are detected
as each statement is encountered, thus debugging is easier in interpreted
languages.

With an interpreter, the language comes as an environment, where you type in


commands at a prompt and the environment executes them for you. For more
complicated programs, you can type the commands into a file and get the
interpreter to load the file and execute the commands in it. If anything goes
wrong, many interpreters will drop you into a debugger to help you track down
the problem. The advantage of this is that you can see the results of your
commands immediately, and mistakes can be corrected readily.

The biggest disadvantage comes when you want to share your programs with
someone. They must have the same interpreter, or you must have some way of
giving it to them, and they need to understand how to use it. Also, users may
not appreciate being thrown into a debugger if they press the wrong key! From a
performance point of view, interpreters can use up a lot of memory, and generally
do not generate code as efficiently as compilers.

Compilers

Compilers translate the entire program into machine language before executing
any of the instructions. Compilers translate source code into machine-oriented
target code called object code. After source code is compiled into object code, no
further reference is made to the source language.
First of all, you write your code in a file (or files) using an editor. You then run
the compiler and see if it accepts your program. If it did not compile, grit your
teeth and go back to the editor; if it did compile and gave you a program, you
can run it either at a shell command prompt or in a debugger to see if it works
properly. Program compilation is a complicated process. A compiler is a software
program that translates a high-level source language program into a form ready
to execute on a computer.
Early in the evolution of compilers, designers introduced IRs (intermediate
representations, also commonly called intermediate languages) to manage the
complexity of the compilation process. The use of an IR as the compiler's internal
representation of the program enables the compiler to be broken up into multiple
phases and components, thus benefiting from modularity.

5|Page
PREPARED BY MR B

A comparison between Compilers and Interpreters

An interpreter directly runs the source code, so it can allow a program to be


changed whenever required, to add features or correct errors. Furthermore, an
interpreter works with the source text, so it can pinpoint an error in the source
text and report it accurately.
With a compiler all translation is completed before the object code is run, which
prevents the object file from being readily adapted as it runs. It can also be
observed that programs which are compiled run faster compared to those which
are interpreted. This is because the interpreter still does the translation during
run time, as opposed to the compiler which does no translation during runtime.

Note: A compiler is a program that changes source code to object code, an


interpreter on the other hand translates source code one line at a time and
executes the instruction.

C++ Syntax

#include <iostream>

using namespace std;

int main() {

cout << "Hello St Paul’s!";

return 0;

Break up the above code to understand it better:

1. #include <iostream>

2. using namespace std;

3. int main() {

4. cout << "Hello St Paul’s!";

5. return 0;

6. }

6|Page
PREPARED BY MR B

Line 1: #include <iostream> is a header file library that lets us work with input
and output objects, such as cout (used in line 5). Header files add functionality
to C++ programs.

Line 2: using namespace std means that we can use names for objects and
variables from the standard library.

A blank line. C++ ignores white space. But we use it to make the code more
readable.

Line 3: Another thing that always appear in a C++ program, is int main (). This
is called a function. Any code inside its curly brackets {} will be executed.

Line 4: cout (pronounced "see-out") is an object used together with


the insertion operator (<<) to output/print text. In our example it will output
"Hello St Paul’s".

Note: Every C++ statement ends with a semicolon (;).

Note: The body of int main () could also been written as:
int main () {cout << "Hello World! "; return 0;}

Remember: The compiler ignores white spaces. However, multiple lines makes
the code more readable.

Line 5: return 0 ends the main function.

Line 6: Do not forget to add the closing curly bracket } to actually end the main
function.

C++ comments

Comments can be used to explain C++ code, and to make it more readable. It
can also be used to prevent execution when testing alternative code. Comments
can be singled-lined or multi-lined.

Single-line Comments

Single-line comments start with two forward slashes (//). Any text
between // and the end of the line is ignored by the compiler (will not be
executed).

This example uses a single-line comment before a line of code:

7|Page
PREPARED BY MR B

Example

// This is a comment
cout << "Hello St Paul’s!";

C++ Multi-line Comments

Multi-line comments start with /* and ends with */. Any text
between /* and */ will be ignored by the compiler:

Example

/* the code below will print the words Hello St Paul’s! to the screen, and it is
amazing */
cout << "Hello St Paul’s!";

Single or multi-line comments?

It is up to you to choose which comment you want to use. Normally we use //


for short comments and /* */ for longer comments.

C++ Variables

Variables are containers for storing data values. In C++, there are
different types of variables (defined with different keywords), for example:

• int - stores integers (whole numbers), without decimals, such as 123 or -


123.
• double - stores floating point numbers, with decimals, such as 19.99 or -
19.99.
• char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes.
• string - stores text, such as "Hello St Paul’s". String values are surrounded
by double quotes.
• bool - stores values with two states: true or false.

Declaring (Creating) Variables

To create a variable, specify the type and assign it a value:

Syntax

Type variableName = value;

8|Page
PREPARED BY MR B

Where type is one of C++ types (such as int), and variableName is the name of
the variable (such as x or myName). The equal sign is used to assign values to
the variable.

To create a variable that should store a number, look at the following example:

Example

Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;


cout << myNum;

You can also declare a variable without assigning the value, and assign the
value later:

Example

int myNum;
myNum = 15;
cout << myNum;

Note that if you assign a new value to an existing variable, it will overwrite the
previous value:

Example

int myNum = 15; // myNum is 15


myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10

Other Types

A demonstration of other data types:

Example

int myNum = 5; // Integer (whole number without decimals)


double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)

9|Page
PREPARED BY MR B

Display Variables

The cout object is used together with the << operator to display variables. To
combine both text and a variable, separate them with the << operator:

Example

int myAge = 35;


cout << "I am " << myAge << " years old.";

C++ User Input

Cout is used to output (print) values. Cin on the other hand is used to get user
input.

cin is a predefined variable that reads data from the keyboard with the extraction
operator (>>).

In the following example, the user can input a number, which is stored in the
variable x. Then we print the value of x:

Example

int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value

cout is pronounced "see-out". Used for output, and uses the insertion operator
(<<)
cin is pronounced "see-in". Used for input, and uses the extraction operator (>>)

C++ Data Types

As explained in the variables, a variable in C++ must be a specified data type:

Basic Data Types

The data type specifies the size and type of information the variable will store:

Data Type Size Description

10 | P a g e
PREPARED BY MR B

Boolean 1 byte Stores true or false values

char 1 byte Stores a single character/letter/number, or


ASCII values

int 2 or 4 bytes Stores whole numbers, without decimals

float 4 bytes Stores fractional numbers, containing one or


more decimals. Sufficient for storing 7
decimal digits

double 8 bytes Stores fractional numbers, containing one or


more decimals. Sufficient for storing 15
decimal digits

Reserved Words in C++ Programming Language


Within C++ the reserved words are also known as “keywords”. These are words
that should not be used in any other way. The words are not used in any other
way meaning that you should not for example, name a variable the same as the
one of these words.
The following are examples of reserved words; if, private, this, double, Boolean,
else, public, case, false, string, class, for, catch, break, true, return, switch,
namespace, void, const, auto, while, do, register, int, float, operator cout,
include, main, or, delete, static, using, struct, short, signed, default,
volatile, union, template and many others.
Pre-defined Data Types
Pre-defined Data Type is a datatype that directly interacts with machine
instructions. Datatypes describes what type of data is stored in the variables and
describe the amount of memory required to be allocated for the data.
Examples of pre-defined
Integer
Character

11 | P a g e
PREPARED BY MR B

Boolean
Floating point
Double and so on.
The if Statement

Use the if statement to specify a block of C++ code to be executed if a condition


is true.

Syntax

if (condition) {
// block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.

In the example below, we test two values to find out if 20 is greater than 18. If
the condition is true, print some text:

Example

if (20 > 18) {


cout << "20 is greater than 18";
}

We can also test variables:

Example

int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}

Example explained
In the example above we use two variables, x and y, to test whether x is greater
than y (using the > operator). As x is 20, and y is 18, and we know that 20 is
greater than 18, we print to the screen that "x is greater than y".

12 | P a g e
PREPARED BY MR B

The else Statement

Use the else statement to specify a block of code to be executed if the condition
is false.

Syntax

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."

Example explained

In the example above, time (20) is greater than 18, so the condition is false.
Because of this, we move on to the else condition and print to the screen "Good
evening". If the time was less than 18, the program would print "Good day".

C++ Loops

Loops can execute a block of code as long as a specified condition is reached.


Loops are handy because they save time, reduce errors, and they make code
more readable.

While Loop

The while loop loops through a block of code as long as a specified condition
is true:

Syntax

13 | P a g e
PREPARED BY MR B

while (condition) {
// code block to be executed
}

In the example below, the code in the loop will run, over and over again, as long
as a variable (i) is less than 5:

Example

int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}

Note: Do not forget to increase the variable used in the condition, otherwise the
loop will never end!

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code
block once, before checking if the condition is true, then it will repeat the loop
as long as the condition is true.

Syntax

do {
// code block to be executed
}
while (condition);

The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:

Example

int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);

14 | P a g e
PREPARED BY MR B

Do not forget to increase the variable used in the condition, otherwise the loop
will never end!

15 | P a g e

You might also like