1
Introduction to
Programming
Introduction to programming Eng. Omar El Safty
Introduction
A computer is a device that follows instructions and executes commands. In order to control a
computer, you need to write programs.
KEY TERM
Program – A list of instructions that does a specific task.
Example:
Write a program that reads two numbers from the user and print out their sum.
In other words, we need to write a program to solve the following equation:
Z = A + B
Solution:
To write the program we go through four general phases:
1 Analysis
2 Design
3 Implementation
4 Testing
1.1 Analysis
In the analysis phase we try to figure out what are the inputs of the program, processing that it
should do and the outputs it shall produce.
Back to our program:
Inputs: A, B
Processing: Z = A + B
Output: Z
1
Introduction to programming Eng. Omar El Safty
Operators
Types of operators:
1 Arithmetic operators
2 Logic operators
3 Assignment operator
Arithmetic Operators
The following table shows the various Arithmetic operators and what they do:
Arithmetic operators
Operator Symbol Description
Multiplies the left operand by the right operand.
Multiplication *
Example: x * y
Divides the left operand by the right operand
Division /
Example: x/y
Subtracts the right operand from the left operand
Subtraction -
Example: x - y
Adds the left operand to the right operand
Addition +
Example: x + y
Raises the left operand to the power of the right
Power ^ operand
Example: 𝑥 𝑦 is written as x ^ y
2
Introduction to programming Eng. Omar El Safty
Logic Operators
The following table shows some Logic operators:
Comparison Operators
Operator Symbol
Greater than >
Less than <
Equal =
Greater than or equal to >=
Less than or equal to <=
Not equal to <>
Assignment Operator
This operator assigns a value or an expression to a variable.
The symbol used for this operator is an arrow pointing left:
Examples of assignment operator:
Cost 10
Price Cost * 2
Tax Price * 0.14
Service Price * 0.12
PaidAmount Tax + Service
3
Introduction to programming Eng. Omar El Safty
1.2 Design
After the analysis phase is done, we design the Algorithm.
We design algorithm using one of two methods:
Pseudocode
Flowcharts
Pseudocode
INPUT Keywords
To allow the user to input a value we use one of two keywords:
INPUT
READ
OUTPUT Keywords
To show the user the value of a variable we use one two keywords:
OUTPUT
PRINT
Example 1.1 :
Write a statement that will output the words Hello World! on the user's screen
OUTPUT “Hello World!”
KEY TERM
Algorithm – A step-by-step solution to a given problem.
Pseudocode – Uses English words and mathematical notations to design the steps of a program.
4
Introduction to programming Eng. Omar El Safty
Example 1.2:
Design a program that displays on the screen the following:
Hello World!
Welcome to CSTEAM
OUTPUT “Hello World!”
OUTPUT “Welcome to CSTeam”
Example 1.3:
Design a program that displays on the screen the following:
Welcome to CSTEAM
Hello World!
OUTPUT “Welcome to CSTeam”
OUTPUT “Hello World!”
Variables
Rules for naming variables:
Variable names cannot contain symbols including spaces
Variable names cannot start with numbers
Variable name should not be a keyword of pseudocode
Variable names should be meaningful to the value it stores
Examples of valid variable names:
number
total
count
count1
countStudents
Examples of invalid variable names:
count students
1count
OUTPUT
5
Introduction to programming Eng. Omar El Safty
Constants
Constants are often used to make the program more readable. Constants have the same
naming rules as variables.
KEY TERM
Variable – A named memory location that stores a value that can change during the execution of
aa a program.
Constant – A named memory location that stores a value that does not change during the a a a
execution of a program.
Data Types
Computers need to know the data type for every variable, to make the processing on this
variable as effective as possible.
The following table shows the data types used in programs:
Data type Definition Examples
A positive or negative whole number be used in
Integer
mathematical operations 150, -100, 0
A positive or negative number that has a fractional
Real
part that can be used in mathematical operations 100.5, -15.2
Char A single character from the keyboard H
Hello World,
'String A sequence of characters
A312_@odq
Boolean Data with two possible values TRUE/FALSE
6
Introduction to programming Eng. Omar El Safty
Declaration
When declaring a variable you have to do two things:
Give your variable a valid name
Give it a data type
Declaration syntax in pseudocode:
DECLARE [VARIABLE NAME] : [VARAIBLE DATA TYPE]
Example 1.4:
Design a program that takes any name from the user and displays a customized hello message.
Hello [any name]
DECLARE AnyName : STRING
OUTPUT “What is your name?”
INPUT AnyName
OUTPUT “Hello “, AnyName
Example 1.5:
Design a program that reads from the user two numbers and displays their sum
DECLARE Num1: REAL
DECLARE Num2: REAL
DECLARE Sum: REAL
OUTPUT “Please enter two numbers to add”
INPUT Num1
INPUT Num2
Sum Num1 + Num2
OUTPUT “The number is ”,Sum
7
Introduction to programming Eng. Omar El Safty
Flowcharts
Flowcharts have some shapes that we use to draw the diagram with:
Shape Name Description
An oval represents a start or
Start/End
end point
A line is a connector that
Arrows shows the relationship
between the shapes
A parallelogram represents
Input / Output
output or input
Process A rectangle is a process
A diamond indicates a
Decision
decision
KEY TERM
Flowchart – Diagram that designs the steps of a program by using a standard set of symbols
aa joined by lines to show the direction of flow.
8
Introduction to programming Eng. Omar El Safty
Example 1.6:
Design a flowchart to read two numbers from the user and display their sum
1.3 Implementation
After designing the algorithm, the algorithm is transformed into code using a programming
language (for example: c++, java, python).
1.4 Testing
The last phase of the programming, this phase is done after implementation by testing how the
program works, we will discuss this later.
9
Introduction to programming Eng. Omar El Safty
Sequential Exercise
Design all the programs using Pseudocode and Flowchart
1 Design a program that reads from the user two numbers and displays their product
2 Design a program that reads the the length of the side of a square and output its area.
3 Design a program that takes from the user the length of a side of a square and the program
calculates and displays the perimeter of the square.
Perimeter of square = Side * 4
4 Design a program that inputs from the user two numbers and displays their division
5 Design a program that takes from the user 3 numbers and the program calculates and
displays both the product and average.
6 Design a program that takes any 5 numbers and prints their average
7 Write a program to input the radius of a circle, the program then displays the area.
Area of a circle = π * Radius * Radius
8 Write a program to input length, width and depth of a pool in cm3 . The program should
display the volume of the pool.
Volume of pool= Length * Width * Depth
9 Write a program to input length, width and depth of a pool. The program should output
the volume and price of pool.
The price of the pool is dependent on the volume: $1 per 100 cm3.
10 Write a program that converts a person’s height in inches to centimeters and their weight in
stones into kilograms [1 inch = 2.54 cm and 1 stone 6.364 kg]
11 Write a program that inputs a student mark and the exam full mark. The program should
output the percentage
10
Introduction to programming Eng. Omar El Safty
12 Write a program to input the radius of a circle, the program then displays the diameter of
the circle, the circumference, and the area of the circle.
Diameter = Radius * 2
Circumference = 2 * π * Radius
Area = π * Radius * Radius
13 Design a program that inputs hours, minutes and seconds from the user and then prints the
total duration in seconds.
Note : 1 minute is 60 seconds, and 1 hour is 60 minutes, or 3600 seconds.
11