G8 Algorithms and Programming Review
G8 Algorithms and Programming Review
Algorithms &
Programming
Walid Sassi
Variables
Datatypes
Changing data type
Data type can be changed using a convert function .. Example is int( ) to
change any data type to an integer type :
Selection
Conditional statement
expressions that give either a true or false result.
if COND:
Statement if cond. Is
True
Selection
Branching Selection
if COND:
Use if – else to branch
selection in case 2 options Statement if cond. Is True
available else:
Statement if cond.
Is false
Rashid
Hello Rashid
Activity
Multiple Selections
Use if – elif - else to branch many selections in case 3 or more options available
Example :
if COND1:
Statement if cond. Is True
elif COND2 :
Statement if
cond2. Is True
else:
Statement if all cond
False
Nested selection
When a selection block is placed within another
selection block,
Example :
Activity
Activity
Lower
Higher
Iteration
A computer can repeat the same block of code as many times as
needed.
The statements used to repeat code are called loops. Loops can be used
to repeat commands in different ways:
While loops
A while loop can be used to repeat code blocks for as long as the result
of a conditional statement is true. These loops are useful when a
programmer does not know the number of times a code block will need
to be repeated.
0, 3, 6, 9, 12
-20, -18, -16, -14, -12, -
10, -8, -6, -4, -2, 0, 2, 4
range(-1, 4, 1)
range(0, 30, 5)
Activity 18
https://www.mauthor.com/present/6270231395303424
Functions &
Methods
Functions
A function (also called a procedure) is a group of commands with a
name used to do a specific task in a program. The commands in a
function can be executed whenever you call the function by using its
name in a program.
The purpose of functions is to organize the code in programs, make it
re-useable and more efficient. This is helpful when programming tasks
that need to be done many times.
Output:
Methods
Some methods used by functions are parameter passing and returning a
value. When you call a function you can give it data to use, this is also
called passing parameters. When a function gives data back to a
program this is called returning a value. Below is an example using both
methods:
Exit card on
LMS
Or
On Paper
Comments and attribution
Python program
Comment : additional information
Print : to display output
Variables : to store data
Input : to ask the user to enter data
Math Expression : to calculate
If selection : to take decision
Loops : to repeat
THANK YOU
See you in class…..