Professional Documents
Culture Documents
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
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
CHAR
The Char data type can be used to store one single character.
BOOLEAN
The Boolean data type can have only two possible values, these are TRUE
or FALSE.
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:
INPUT age
ELSE
yearsToWait 17 – age
ENDIF
Program
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:
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
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
In programming, the term selection means that the program can choose to run
different commands based on a condition being true or false.