0% found this document useful (0 votes)
14 views28 pages

G8 Algorithms and Programming Review

G8 algorithms and programming Review

Uploaded by

Walid Sassi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views28 pages

G8 Algorithms and Programming Review

G8 algorithms and programming Review

Uploaded by

Walid Sassi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

Grade 8

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.

Note : The difference between the (=) and (== ) Operators.


Conditional statement
control the flow of execution because a block of code will only run if the condition is True

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

Example if the user input Rashid


the output is :

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

You got it!

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.

example of a while loop represented in a flowchart


example of text-based program using a while loop
Range function
For loops can be controlled precisely using
a range() function. Knowing how to use the
range()function will make designing loops Below are some examples using the range function:
easier.
You can give the range() function the start,
end and step size values.

example of a for loop represented in a flowchart


Activity 17

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.

Simple functions are commonly used to:


• Perform repetitive tasks
• Perform calculations
A B
Activity 19 Solution:

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:

In this example, the values of a and b are given to the


function as parameters. The value of answer is then value
returned to the program.
Function and methods
def rectangle( ): def rectangle( a , b ):
a = input( ) perimeter = a*2+b*2
a = int ( a ) return perimeter
b = input( )
b = int( b ) a = input( )
perimeter = a*2+b*2 a = int ( a )
return perimeter b = input( )
b = int( b )
result= rectangle( ) result= rectangle (a, b )
print(result) Print(result)
Check your knowledge

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…..

You might also like