You are on page 1of 37

PSUEDOCODE

Assignment
Values are assigned to an item/variable using the ←
operator. The variable on the left of the ← is assigned the
value of the expression on the right. The expression on the
right can be a single value or several values combined with
mathematical operators.
Selection
Unit 7 Algorithm design and programming

Program construct
• There are three basic ways of controlling the order in
which program statements are carried out
• You have already used examples of all of them:
• Sequence
• Selection
• Iteration
Pseudo code
Pseudo code can be broken down into five
components.
• Variables:
• Assignment:
• Input/output:
• Selection:
• Repetition:
A variable has a name, a data type, and a value. There is a location in memory
associated with each variable. A variable can be called anything or be given any
name.

Assignment is the physical act of placing a value into a variable. Assignment can
be shown using
set= 5
set =num + set;

Input / Output both deal with an outside source (can be a user or another program)
receiving or giving information.
• Output – Write / display / print
• Input – Read / get / input
Selection construct allows for a choice between performing an action and skipping
it. It is our conditional statements.
Selection statements are written as such:
if ( conditional statement)
statement list
else
statement list
Repetition is a construct that allows instructions to be executed multiple times (IE
repeated).
In a repetition problem
– Count is initialized
– Tested
– incremented
Repetition problems are shown as:
while ( condition statement)
statement list
TASK1
Explain the types of programming. Mention examples for each
VARIABLES & CONSTANTS

Variables are the names you give to computer memory


locations which are used to store values in a computer
program.

Eg: Letters ,numbers etc.

Constant are also areas in memory to store information


in which whose value cannot change once it has been
assigned.
Eg a=20
pi=3.14
DATA TYPE

It is the type of data and its associated operations

STRING
The String data type is used for data that can be used to store
a number of letters, digits and symbols. The data can be a
combination of these three items e.g. "345ISS@", or it can be
just one type
e.g. s="Hello World".

INTEGER
An Integer can be used to store a positive whole number (No
decimals numbers)

Eg: i=20
REAL
The Real data type will allow you to store decimal numbers (Positive or
negative) e.g. Cost = 5.50

​Real data can be used in mathematical operations

CHAR
The Char data type can be used to store one single character.

​e.g. Gender = "M"

BOOLEAN
The Boolean data type can have only two possible values, these are TRUE
or FALSE.

​E.g. Accepted = true


AFL

What are datatypes?


Explain the various datatypes used in python programming language?
Selection
Unit 7 Algorithm design and programming

operators
• An operator in a programming language is a symbol
that tells the compiler or interpreter to perform
specific mathematical, relational or logical operation
and produce final result.
Selection
Unit 7 Algorithm design and programming

OPERATORS:
Selection
Unit 7 Algorithm design and programming

COMPARISON OPERATOR
Selection
Unit 7 Algorithm design and programming

EG:
Selection
Unit 7 Algorithm design and programming

Sequence
• The statements are executed in the order they are
written
OUTPUT “How many hours a night do you sleep?”
INPUT HoursPerNight
HoursPerWeek  HoursPerNight * 7
OUTPUT “That’s”, HoursPerWeek, “per week!”
Selection
Unit 7 Algorithm design and programming

Selection
• IF statements and CASE statements are both selection statements
• The next statement to be executed depends on whether the condition
being tested is true or false
OUTPUT “How many hours a night do you sleep?”)
INPUT HoursPerNight
IF HoursPerNight < 8 THEN
OUTPUT “That’s not enough!”
ELSE
OUTPUT “That’s plenty!”
ENDIF
THREE PROGRAMMING STRUCTURES

SEQUENCE

SELECTION-----------IF STATEMENT
CASE STATEMENT

ITERATION---------

For Loop
While loop
Repeat Until Loop
Selection
Unit 7 Algorithm design and programming

TASK 1
• Design algorithm in form of pseudocode to check if it
is suitable age for driving.Provide appropriate
message else. Share your code in classkick.
Selection
Unit 7 Algorithm design and programming

Pseudocode:

OUTPUT “Please enter your age: ”

INPUT age

IF age >= 17 THEN

OUTPUT “You are old enough to drive”

ELSE

OUTPUT “You are too young to drive”

yearsToWait  17 – age

OUTPUT “Wait another ”, yearsToWait, “years”

ENDIF
Program

Age=int(input("Please enter your age: "))


if Age>=17:
print("You are old enough to drive")
else:
print("you are too young to drive")
year=17-Age
print("Wait another", year, "years")

o/p
Please enter your age
12 You are too young to drive
Selection
Unit 7 Algorithm design and programming

TASK 2
• Write pseudocode for a program which asks the user
to enter the cost of two items, adds the two costs
and if the cost is greater than $10.00, displays a
message “Sorry, too much”. Otherwise it displays the
change due from $10.00
Selection
Unit 7 Algorithm design and programming

Pseudocode solution
OUTPUT “Please enter price of first item:”
INPUT item1
OUTPUT “Please enter price of second item:”
INPUT item2
total  item1 + item2
IF total > 10 THEN
OUTPUT “Sorry, too much”
ELSE
change  10 – item1 - item2
OUTPUT “Change from $10.00 is $”, change
ENDIF
Program:

Price1= int(input("Please enter price of first item:"))


Price2=int(input("Please enter price of second item:"))
Sum=Price1+Price2
if Sum > 10:
print("Sorry, too much")
else:
change = 10-Sum
print( "Change from $10.00 is $ ",change)
Write a Python program to enter total marks in five
subjects, each out of 100. Calculate percentage and
assign grade according to the following:
Percentage Grade
>= 90 A
>= 80 and < 90 B
>= 70 and < 80 C
>= 60 and < 70 D
<60 F
A formula for calculating the body mass index (BMI) is:

Calculate the BMI for a person whose weight is 80kg and height is 2 meters.

 Using pseudocode or otherwise, write an algorithm that will input the ID, weight

(kg) and height (m) of 30 students, calculate their body mass index (BMI) and output

their ID, BMI and a comment as follows:

A BMI greater than 25 will get the comment ‘OVER WEIGHT’, a BMI between 25 and 19

(inclusive) will get ‘NORMAL’ and a BMI less than 19 will get ‘UNDER WEIGHT’
Weight= int(input("Please enter weight:"))
height=int(input("Please enter height in
meters:"))
bmi=Weight/(height*height)
print(bmi)
if bmi >= 25:
print("overweight")
elif bmi>=19 and bmi<25:
print ("normal")
else:
print("underweight")
Selection
Unit 7 Algorithm design and programming

Case statement
• A CASE statement is useful if there are more than two
alternative paths through the program
OUTPUT “Which performance do you want tickets for?”
OUTPUT “Enter 1,2 or 3”
IN{PUT Performance
CASE Performance OF
1: PricePerTicket  3.00
2: PricePerTicket  6.50
3: PricePerTicket  8.00
ELSE
OUTPUT “Please enter 1, 2 or 3”
ENDCASE
Selection
Unit 7 Algorithm design and programming

MIDPLENARY

Explain Selection statements.

In programming, the term selection means that the program can choose to run
different commands based on a condition being true or false.

Write the syntax for if and case statement


Selection
Unit 7 Algorithm design and programming

CONVERT THE FOLLOWING


CODE TO IF

You might also like