You are on page 1of 60

Introduction to Programming

What is a computer program?


set of instructions that a computer can execute to perform a
specific task.

(input)  (processing)  (output)


[execution]

e.g. instructions to add two numbers:

1. get 1st number


2. get 2nd number
3. add (1st number, 2nd number)
4. display answer
What is a programming language?
a structured set of syntactic rules applied to write semantically
correct program statements.
(formal/artificial language)

Syntax & Semantics

grammar of a meaning from a


programming language program statement
Generations of Languages (GLs)
Fifth Generation Programming Language (5GL)

High level
Fourth Generation Programming Language (4GL)
languages

Third Generation Programming Language (3GL)

Compiler /
interpreter

Second Generation Programming Language (2GL)

Low level assembler


languages

First Generation Programming Language (1GL)


1st Generation Language (1GL)

• actual machine language.

• binary code - patterns of 0s and 1s – 10011001.

• lowest programming level.

• other generations have to be converted to 1GL for the


computer to execute the instructions.

• converted code is called object code.


2nd Generation Language (2GL)

• Assembly language.

• mnemonics are used to construct code e.g. MOV.

• Assembler program is used to convert 2GL to machine language.

• structure is close to that of machine language.

• low level languages are machine dependent.


3rd Generation Language (3GL)

• Procedural language.

• also called an imperative language.

• lowest level of the high level languages.

• structure is similar to that of the human natural language.

e.g. C and C++


4th Generation Language (4GL)

• non-procedural language.

• also called a declarative language.

• structure is closer to that of the natural language than 3GL.

e.g. SQL
5th Generation Language (5GL)

• different aspects are used at this level.

• language with visual tools to aid the development process and


design the user interface.

• language may have AI functionality.

e.g. Prolog
Programming terms
• Data

– input values to be processed by a program.

• Data type

– classification of the type of data e.g. integer.


– two attributes: size and range.

• Variable

– a defined/named memory location which can store data


values of a particular type.
– has name and type.
Programming terms cont.

• Constant

– a value that does not change in a program.

• Identifier

– Variable name

• Operator / Operand
– … ????

• Reserved word

– a word with a pre-defined meaning and cannot be used as


an identifier.
Programming terms cont.

• Statement

– a single complete instruction for a specific action to be


carried out.
– a program comprises blocks of statements.

• Expression

– … ????

• Compiler / Interpreter

– … ????
Programming terms cont.

• Algorithm

– an unambiguous finite set of step by step instructions used


to carry out a specific task.

• Flowchart

– a diagram (visual/graphical representation) that depicts the


sequence of steps and decisions in an algorithm.

• Source code / code

– a program written in high level language.


Programming terms cont.

• Bug

– a defect in the source code.

• Debugging

– … ???

• Comment

– explanations of a program (or part of) that are not


executed as part of the program statements.
Programming terms cont.

• Imperative language

– a programmer has to specify all the steps on how a program is


to perform a specific task.

• Declarative language

– a programmer has to specify what actions are to be carried


out.
C++
C vs C++

• C++ is derived from C  C++ is a superset of C.

• earlier versions of C++ known as C with classes.

• C is a procedural programming language and does not support


classes and objects

• C++ is a combination of both procedural and object oriented


programming (OOP) language.
C vs C++

• most C Programs can be compiled in C++ compiler.

• C++ expressions are the same as C expressions.

• all C operators are valid in C++.


C++ Reserved Words
auto const double float int

short struct unsigned break continue


else for long signed switch void
case default enum goto register

sizeof typedef volatile char do

extern if return static union while

• first set (above) - carried over from C into C++ (32).

• 30+ more reserved words that are new to C++ (not in C).
program structure
1. // my first program in C++

2. #include <iostream>
3. using namespace std;
4. int main ()
5. {
6. cout << "Hello World!";
7. return 0;
8. }

Hello World!
program structure cont.

// my first program in C++

a comment line:

// for a single line comment

&

/* ….multiple
lines
commenting …. */
program structure cont.

#include <iostream>

a preprocessor line

i.e. #include <iostream> tells the preprocessor to include


the iostream standard file.

<iostream> file is used for basic standard input-output.


program structure cont’d

using namespace std;

• namespace std is used to allow access to elements of the


standard C++ library.

• most programs use the C++ standard library hence the line is
included in most of the programs.
program structure cont’d

int main ()

• definition of the main function.

• the point where all programs start their execution.


program structure cont.

cout << "Hello World!";

• Line 6 is an example of a program statement.

• cout represents the standard output stream in C++.

• line 6 means the program is to insert a sequence of characters


into the standard output stream, which is usually the screen.

• in this case the Hello World! sequence of characters will be


displayed.
program structure cont.

• cout is declared in the iostream standard file within the std


namespace.
• need to include that specific file.
• declaration comes before use in the program.

• a semicolon character (;) is used to mark the end of the


statement.

• Whitespaces do not affect the program.


program structure cont.

return 0;

• return statement causes the main function to finish.

• Return is followed by a return code – 0 in our example.

• in general a return code of 0 for the main function is interpreted


as the program worked as expected without any errors during its
execution.

• It is the most usual way to end a C++ console program.


Standard Output (cout)

• by default, the standard output of a program is the screen.

• cout is defined to access the output.

• cout is used in conjunction with the insertion operator ( << ).


Standard Output (cout)

• << is used to insert data that follows it into the stream


preceding it.

• << may be used more than once in a single statement.

• cout does not add a line break after its output unless explicitly
indicated.
Standard Input (cin)

• usually the standard input device is the keyboard.

• cin is defined to read input.

• the overloaded operator of extraction ( >>) is used on the cin


stream.

• >> is followed by a variable that will store the data that will
extracted from the stream.
Standard Input (cin)

• cin can only process the input from the keyboard once the enter
key has been pressed.

• cin can be used to request for more than one datum input from
the user.
examples
Hello World
// my first program in C++

#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}

Hello World!
Sum of two integers
1. #include <iostream>

2. using namespace std;

3. int main() {

4. int num_1, num_2, sum;

5. cout << "Enter two integers: ";

6. cin >> num_1 >> num_2;

7. sum = num_1 + num_2; // add the two integers

8. cout << " Sum is " << sum;

9. return 0;

10. }
Variable declaration
• all variables must be declared before use.

• C++ is case sensitive,

• declaration is done after the opening brace of main().

format

data_type var,var,…

e.g.
int i,j,k;

float length,height;

char firstname;
Flowchart
Algorithm

an unambiguous finite set of step by step instructions used to


carry out a specific task.

Flowchart

a diagram (visual/graphical representation) that depicts the


sequence of steps and decisions in an algorithm.

will show the execution flow of the program statements


Flowchart components
There are some commonly used components in programming:

Source: components, tips and example

https://www.tutorialspoint.com/programming_methodologies/programmin
g_methodologies_flowchart_elements.htm
Flowchart drawing tips
• only one start and one stop symbol

• flow arrows should not cross

• general flow is from top to bottom or left to right

• on-page connectors are referenced using numbers

• off-page connectors are referenced using alphabets


Flowchart example
a flowchart to show the algorithm to compute the average of
two numbers entered by user:
Operators
• Assignment operator

• Arithmetic operator

• Relational operator

• Logical operator

• Bitwise operator
Assignment Operator
• used to give a variable the value of an expression.

e.g.

sum = num_1 + num_2

pin = ‘P’
Arithmetic Operator
• 6 primary operators:

• Addition +

• Subtraction / negation -

• Division /

• Modulus %

• Multiplication *
increment/decrement
• prefix form:

++i;  i=i+1;

--i;  i=i-1;

• postfix form:

i++;  i=i+1;

i--;  i=i-1;

what is the difference ???


Relational operator
• used for comparison of two expressions

• equal to ==

• not equal to !=

• less than <

• less than or equal to <=

• greater than >

• greater than or equal to >=


Logical operator
• used to create conditional statements

• logical AND &&

• logical OR ||

• logical NOT !
Program Constructs
• used to control the order in which the program statements are
executed.

• there are three main constructs:

• sequence

• selection

• repetition / iteration / looping


Sequence
• statements are executed in sequential order.

e.g. a program to compute sum of two integers

int main() {

int num_1, num_2, sum;

cin >> num_1 >> num_2;

sum = num_1 + num_2;

cout << " Sum is " << sum;

return 0;
}
Selection
• provides for selection between alternatives based on some
condition(s).

e.g. a program to check if a number is even or odd

int main() {
int number, remainder;

cout << "Enter number : ";


cin >> number;
remainder = number % 2;
if (remainder == 0)
cout << number << "is an even integer ";
else
cout << number << "is an odd integer ";

return 0;
}
Flowchart exercise
• Draw a flowchart for a program that checks if the entered
number is even or odd
Selection constructs

if

if – else

switch
if statement

• a block of statements is executed or skipped depending on a


test condition.

syntax

If (test expression)
{
block of statements;
}
- statements will be executed if expression is true

- statements will be skipped if expression is false


if example
a program to confirm that a positive number is entered by the
user.

int main() {
int number;
cout << “enter an integer: ";
cin >> number;
if ( number > 0) {
cout << "entered number is positive";

}
cout << “no positive number is entered.";
return 0;
}
If-else statement
• executes block of statements inside the if - if test expression
is true.

• executes block of statements inside else - if test condition is


false.

syntax

If (test expression) {
block of statements;
}
else {
block 1 of statements;
}
If-else example
a program to check if a number is even or odd

int main() {
int number, remainder;
cout << "Enter number : ";
cin >> number;
remainder = number % 2;
if (remainder == 0){
cout << number << "is an even integer ";
}
else {
cout << number << "is an odd integer ";
}
return 0;
}
tutorial
Let us write a program that prompts a user to enter
three integers.

The program will then find and display the largest of


the three numbers.

- using if statement

- using if – else statement


using if statement
int main() {
int num1, num2, num3;
cout << “enter three numbers: ";
cin >> num1 >> num2 >> num3;
if(num1 >= num2 && num1 >= num3) {
cout << “largest number is " << num1;
}
if(num2 >= num1 && num2 >= num3) {
cout << “largest number is " << num2;
}
if(num3 >= num1 && num3 >= num2) {
cout << “largest number is " << num3;
}
return 0;
}
using if-else statement
int main()
{
int num1, num2, num3;
cout << “enter three numbers: ";
cin >> num1 >> num2 >> num3;
if((num1 >= num2) && (num1 >= num3))
cout << “largest number is " << num1;
else if ((num2 >= num1) && (num2 >= num3))
cout << “largest number is " << num2;
else
cout << “largest number is " << num3;

return 0;
}

You might also like