Ch1 - Introduction To Programming
Ch1 - Introduction To Programming
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
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:
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.
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
2
Introduction to programming Eng. Omar El Safty
Logic Operators
The following table shows some Logic operators:
Comparison Operators
Operator Symbol
Equal =
Assignment Operator
Cost 10
Price Cost * 2
3
Introduction to programming Eng. Omar El Safty
1.2 Design
Flowcharts
Pseudocode
INPUT Keywords
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
KEY TERM
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:
Example 1.3:
Welcome to CSTEAM
Hello World!
Variables
Rules for naming variables:
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.
Hello World,
'String A sequence of characters
A312_@odq
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
Example 1.4:
Design a program that takes any name from the user and displays a customized hello message.
Hello [any name]
Example 1.5:
Design a program that reads from the user two numbers and displays their sum
INPUT Num1
INPUT Num2
7
Introduction to programming Eng. Omar El Safty
Flowcharts
Flowcharts have some shapes that we use to draw the diagram with:
A parallelogram represents
Input / Output
output or input
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
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