Professional Documents
Culture Documents
2
Program Design: Control Structures
Sequence: is the order which the instructions are
executed by the computer.
Selection: select which path of an algorithm to
3
Program Design: Control Structures
Sequence
A sequence control structure is simply a series of
procedures that follow one another.
4
Program Design: Control Structures
Selection
The selection (if-then-else) control structure involves a choice: if
a certain condition is true, then follow one procedure; else, if
false, follow another.
When more than two possible choices exist, the case control
structure can be used instead.
True Statement 1
Condition ...
Statement
False Statement 2
5
Program Design: Control Structures
Iteration
Loop is an operation that repeats until a certain condition is met.
A looping (iteration) control structure can take two forms.
With the do-while structure, the loop is executed as long as a
condition is true; with the do-until structure, the loop continues until a
certain condition becomes true.
False
...
Condition
Tr u
e
Statement
6
Algorithm Design Techniques:
Flowchart
use geometric symbols and familiar relational operators
to provide a graphic display of the sequence of steps
involved in a program.
7
Draw a flowchart that asks the user to enter the
length of the square side and then calculate the area.
Start
read the
side length
Area = Length2
Stop
8
The flowchart segment and code below show
how a decision structure in a flowchart is
expressed in C++ as an if/else statement.
NO YES
x < y?
Calculate a Calculate a
as x plus y. as x times 2.
9
If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
If years_employed = 1, If years_employed is
CASE
bonus is set to 100 years_employed any other value, bonus
is set to 800
1 2 3 Other
10
The flowchart segment below shows a
repetition structure expressed in C++ as a
while loop.
YES
x < y? Add 1 to x
11
•The “A” connector
indicates that the second START A
END
A
12
Algorithm Design Techniques:
Pseudocode
uses English-like statements in place of the graphic
symbols of the flowchart.
13
Write a pseudocode algorithm that computes the area of
the circle
Problem: Calculate and display the area of the circle
Discussion: Uses mathematical knowledge, that is area = r2
pseudocode algorithm
input radius
set area to 3.14 * radius * radius
output area
14
Write a pseudocode algorithm that display passed if student
grade is greater than or equal to 60 and display failed if
student grade is less than 60
pseudocode algorithm
If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"
endif
15
Write a pseudocode algorithm that computes the sum of the
numbers between 1 and 10
pseudocode algorithm
set counter to 1
set sum to 0
while counter <= 10
set sum to sum + counter
increment counter
output sum
16
17
Calculate Pay with Overtime
Begin
input hours, rate
if hours <= 40 then
pay = hours * rate
else
pay = 40 * rate + (hours – 40) * rate * 1.5
print pay
End
Pseudocode
flowchart
18
Average of 10 Numbers
Begin
i = 0
sum = 0
while i < 10
input x
sum = sum + x
Increment i
avg = sum / 10.0
print avg
End
Pseudocode
flowchart
19
Solving the equation 2x2 + x + 5 for
all x between 5 and 10
Begin with x = 5. Next, substitute 5
for each value of x in equation
and then solve it (answer=60).
Now take the next x, which is 6
and so on till x = 10.
Pseudocode
Flowchart 20
In the last 10 years, Python has become one of the most important
languages for data science, machine learning, and general software
development in academia and industry.
21
Python was developed in the early 1990s by Guido
van Rossum (1991).
He needed to carry out repetitive tasks for
administering computer systems.
He was dissatisfied with other available languages
that were optimized for writing large and fast
programs.
He needed to write smaller programs that didn’t have to run at
optimum speed.
Therefore, he designed a language that made it very easy to work
with complex data.
Python has evolved considerably since its beginnings. Van Rossum
is still the principal author of the language, but the effort now
includes many volunteers.
22
Python has become popular for business, scientific, and
academic applications and is very suitable for the
beginning programmer
Python has become one of the most popular interpreted
programming languages, along with Perl, Ruby, and
others.
Python is often called scripting language, as it can be
used to quickly write small programs or scripts to
automate other tasks.
23
From IEEE spectrum 2017: programming languages
ranking
24
From IEEE spectrum 2020: programming languages
ranking
25
From Stackoverflow.com 2021:
insights.stackoverflow.com/trends
26
There are many reasons for the success of Python:
Python has a much simpler and cleaner syntax than other
28
3. To run Python, you can use either:
A. Official Python Shell which is IDLE
Open command window, then type IDLE through
B. (Optional) Install third party IDE such as
i. Pycharm .. Link: https://www.jetbrains.com/pycharm/
OR
ii. Spyder .. Link: https://www.spyder-ide.org/
OR
iii. PyDev .. Link: https://www.pydev.org/
30
From Python File (containing source code)
Open IDLE python shell
Click on File then New File
(Or press Ctrl+N)
31