0% found this document useful (0 votes)
12 views

Lesson 4 PS and PD (Algorithm-IF Construct)

The document outlines the objectives and concepts related to algorithm design using IF statements in pseudocode. It explains control structures, including sequence, selection, and repetition, and provides examples of IF-THEN and IF-THEN-ELSE statements. Additionally, it includes assignments and activities for practical application of these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lesson 4 PS and PD (Algorithm-IF Construct)

The document outlines the objectives and concepts related to algorithm design using IF statements in pseudocode. It explains control structures, including sequence, selection, and repetition, and provides examples of IF-THEN and IF-THEN-ELSE statements. Additionally, it includes assignments and activities for practical application of these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PROBLEM SOLVING AND

PROGRAM DESIGN

Algorithm-Pseudocode with IF
construct
OBJECTIVES:

General objective(s):
⦿ Understand when and how to include
IF statement within a pseudocode
algorithm.

Specific objectives /learning outcomes:

◼Define control structures


◼State the three types of control structures
◼Represent algorithms utilizing IF
statements
CONTROL STATEMENTS
Control Statements
These statements are used to control the
amount of time a statement or sequence of
statements is carried out based on some
condition.

There are three main types of control


structures that are used in designing
programs:
⮚ Sequence
⮚ Selection
⮚ Repetition
CONTROL STATEMENTS
⦿ Sequence -This is the structure that allows
statements to be executed in the order in which
they are written in your program. Each
statement will only be executed once by the
computer.

⦿ Selection control structures allow for decisions


to be made in a program based on a condition.
Examples of those constructs are the IF-THEN
statement and the IF-THEN-ELSE statement.

⦿ Repetition-This allows statements to be


repeated a fix number of time until some
condition evaluates to be false.
IF Then:
⦿To read a number P. If the number is
greater than 50 it should add 20 to P
and print the results.

IF Then Else:
⦿To read a number P. If the number is
greater than 50 it should add 20 to P.
If not, it should subtract 20 from P.
Print the result. They will be
encouraged to use their text book as
well as the internet to guide them.
ASSIGNMENT
⦿ Read and make notes on the following
Selection statements:
⦿ IF –THEN
⦿ IF-THEN-ELSE
IF-THEN STATEMENTS

The IF-Then Statement suggest that one or


more statements will only be considered based
on a condition or the answer to the question

IF (the condition is true)


THEN (carry out one or more statement)
ENDIF

The keyword ENDIF is used to indicate the end


of the IF-THEN statement. Any statement below
ENDIF In the pseudocode is therefore part of the
general pseudocode and not specific to the IF-
THEN statement.
IPO CHART
⦿ Write a program to read a mark and
determine if the mark is greater than or
equal to 80. If the condition is true,
output the statements “ Excellent”,
“Please see your teacher for a reward”.
INPUT PROCESSING OUTPUT

READ Mark READ Mark Print “Excellent”


Print “Please see
IF Mark>=80 Then your teacher for a
Print “ Excellent” reward”
Print “Please see your
teacher for a reward”
ENDIF
EXAMPLE
PASCAL FOR
DEMONSTRATION
Program Exam_Results;
{Developer: Mrs. Jane Doe)
{Date:Nov. 19.2020}
{This program determines if Mark is greater than or equal to 80}
Var mark:real;

Begin
Writeln ('Please enter mark');
READln (mark);

IF mark >= 80 THEN


Begin
Writeln ('EXCELLENT');
Writeln('Please see your teacher');
END;
END.
IF THEN ELSE STATEMENT

The IF-THEN-ELSE Statement directs the


algorithm to one or more statements if the
outcome of the condition is valid or true. The
algorithm is directed to another set of
statements if the outcome of the condition is
not valid or false.

IF (the condition is true) THEN


(carry out one or more statement)
ELSE (carry out one or more statement)
ENDIF
IPO CHART
Write a program to read a mark and determine if the
mark is greater than or equal to 80. If the condition is
true, output the statements “ Excellent”, “Please see
your teacher”. ELSE output“Try harder next time”
INPUT PROCESSING OUTPUT

Print
READ Mark“Try harder next
READtime”
Mark Print “Excellent”
Print “Please see your
IF Mark>=80 Then teacher”
Print “Try harder next time”
Print “ Excellent”
Print “Please see your
teacher”
ELSE

Print “Try harder next time”

ENDIF
EXAMPLE
PASCAL FOR
DEMONSTRATION
Program Exam_Results;
{Developer:Mrs. JaneDoe)
{Date:Nov. 19.2020}
{This program determines if Mark is greater than or equal to 80}
Var mark:real;

Begin
Writeln ('Please enter mark');
READln (mark);

IF mark >= 80 THEN


Begin
Writeln ('EXCELLENT');
Writeln('Please see your teacher');
End
ELSE
Begin
Writeln ('GOOD EFFORT BUT YOU NEED TO WORK HARDER');
END;
END.
YOUR TIME TO TRY !
Work in small groups to construct the IPO
chart and write the pseudocode for the
following question:

⦿ To read a number P. If the number is


greater than 50 it should add 20 to P. If
not, it should subtract 20 from P. Print
the result. You are encouraged to use
your text book as well as the internet to
guide you.
PSEUDO CODE (IF THEN
ELSE)
Pseudocode Value_Of_P
Developer: Mrs. Jane Doe
Date: November 19,2020
This algorithm determines if a number is greater than or less 50.

Declare Result, P as integer

Start
Print “Please enter a number”
Read P

IF P>50 THEN
Result🡨P+20
ELSE
Result🡨P-20
EndIF

Print “The result is:”, Result

Stop
IF THEN ELSE
Pseudocode Value_Of_P
Developer: Mrs. Jane Doe
Date: November 19,2020
This algorithm determines if a number is greater than or less 50.

Declare Result1, Result2 P as integer

Start
Print “Please enter a number”
Read P

IF P>50 THEN
Result1🡨P+20
Print “The result is:”, Result1

ELSE
Result2🡨P-20
Print “The result is:”, Result1
EndIF

Stop
TWO IF STATEMENTS
Pseudocode Value_Of_P
Developer: Mrs. Jane Doe
Date: November 19,2020
This algorithm determines if a number is greater than or less 50.

Declare Result, P as integer

Start
Print “Please enter a number”
Read P

IF P>50 THEN
Result🡨P+20
ENDIF

IF P<50 THEN

Result🡨P-20
EndIF
Print “The result is:”, Result

Stop
NESTED IF(IF THEN ELSE
IF )
Pseudocode Value_Of_P
Developer: Mrs. Jane Doe
Date: November 19,2020
This algorithm determines if a number is greater than or less 50.

Declare Result, P as integer

Start
Print “Please enter a number”
Read P

IF P>50 THEN
Result🡨P+20
ELSE

IF P<50 THEN
Result🡨P-20
EndIF
ENDIF
Print “The result is:”, Result
Stop
PASCAL FOR
DEMONSTRATION
Program Value_Of_P;
Developer: Mrs.Jane Doe}
{Date: November 19,2020}
{This algorithm determines if a number is greater than or less 50.}

Var Result, P:integer;

Begin
Writeln ('Please enter a number');
Readln (P);

IF P>50 THEN
begin
Result:=P+20;
END
ELSE
Begin
Result:=P-20;

End;
Writeln ('The result is:', Result);
END.
PASCAL FOR
DEMONSTRATION
Program Value_Of_P;
{Developer: Mrs. Jane Doe}
{Date: November 19,2020}
{This algorithm determines if a number is greater than or less 50.}

Var Result1, Result2,P:integer;

Begin
Writeln ('Please enter a number');
Readln (P);

IF P>50 THEN
begin
Result1:=P+20;
Writeln ('The result is:', Result1);
END
ELSE
Begin
Result2:=P-20;
Writeln ('The result is:', Result2);
End;
END.
ACTIVITY
1. Students are awarded extra credit points depending on how
they perform in their coursework; if they received 80 or over,
they are given a bonus. Read the mark and add 1 to the total
number of credits if the condition is right and print the total.
NB:Accumulating: gradual gathering of something.
Total🡨Total + 1

2. Read the cost of a meal at a restaurant. If the meal is over


JMD10000 the customer is given a discount of 10%.
Calculate the discounted cost and print the Price of the
meal.

3. Write an algorithm to read an employee’s first name, last


name and hours worked for the week and find the weekly
wage. The hourly rate is JMD $250. If the hours worked is
greater than 40 then the customers overtime is calculated at
time and a half (1.5). Print the Employees name, hours
worked and Total Pay.
4. Write a pseudocode to read the name and average score for a student in any
given subject. Your algorithm should determine the students letter grade if the
student gets 90 or above, they get an A, 80 or above they get a B, 70 or above
they get a C and anything lower than 70 is a D. Print the student’s name and
their letter grade.

5. Write an algorithm to calculate the discount price of a computer. If the


computer costs more than $2,000, the discount is 17%. If the computer cost
more than $1500, the discount is 14%. Otherwise, the discount is 10% Print the
original price, discount and the price after the discount.

6. Write a pseudocode algorithm to read two numbers and the user choice of
arithmetic operations. If user enter A perform Addition, M for Multiplication, D
for Division and S For Subtraction. Print two numbers, the user’s choice of
arithmetic operation and the result of the operation.

You might also like