PROGRAMMING-
PROGRAMMING-
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.
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.
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.
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
In C++ you would display the message Hello world with the following instruction:
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.
3|Page
PREPARED BY MR B
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.
Think of the semantics as the building blocks of your C++ program and the
syntax as the correct way to put them together.
• 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.
4|Page
PREPARED BY MR B
Interpreters
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
C++ Syntax
#include <iostream>
int main() {
return 0;
1. #include <iostream>
3. int main() {
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.
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 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).
7|Page
PREPARED BY MR B
Example
// This is a comment
cout << "Hello St Paul’s!";
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!";
C++ Variables
Variables are containers for storing data values. In C++, there are
different types of variables (defined with different keywords), for example:
Syntax
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:
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
Other Types
Example
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
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 (>>)
The data type specifies the size and type of information the variable will store:
10 | P a g e
PREPARED BY MR B
11 | P a g e
PREPARED BY MR B
Boolean
Floating point
Double and so on.
The if Statement
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
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
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
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 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