Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Selection Logic

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

SELECTION LOGIC

Solving Problems using Selection Logic (I.P.O. Chart and Pseudocode Algorithm)
Selection Logic
◦ We have learnt to solve problems using sequence /straight line
logic -solutions whose statements are executed in a sequence from
the first to the last.

◦ Now we would learn to solve problems that would use selection


logic – solutions that would test some condition and take different
courses of action based on whether the condition is true or false .
Conditional Structure
◦ Decisions in a solution are written in a more formal way.

◦ They are written as a conditional structure.

◦ A conditional structure depend on Logical Tests.

Just Click for the Caribbean 3rd Edition Howard Lincoln, Alison Page Et al.
Logical test/Condition
◦ A Logical test is one that has the answer yes or no, or true or false.

◦ ‘How old are you?’ is not a logical test, because it can have many different
answers.

◦ ‘Are you under 20?’ is a logical test, because it has the answer yes or no.

Just Click for the Caribbean 3rd Edition Howard Lincoln, Alison Page Et al.
Logical test
◦ Logical test use a relational operator to compare two values.
Relational Operators Meaning

= equals to

<>  not equal to

>  greater than

>= greater than or equal to

<  less than

<= less than or equal to

◦ The comparison gives a yes/no response which decides which of two actions is taken.

Just Click for the Caribbean 3rd Edition Howard Lincoln, Alison Page Et al.
Decisions
In pseudocode, the conditional structure begins with the words:

If … then

The Logical test goes between the words ‘If’ and ‘then’.

If <<Logical test>> then

Just Click for the Caribbean 3rd Edition Howard Lincoln, Alison Page Et al.
Decisions
The instruction(s) that need(s) to be executed if the Logical Test is true is inserted on the next line but indented.
The indentation shows the instruction(s) that need to be executed if the condition is met.

If <<Logical test>> then


<<Statement if Logical Test is true>>

OR

If <<Logical test>> then


<<Statement#1 if Logical Test is true>>

<<Statement#last if Logical Test is true>>
Decisions
Else comes next

If <<Logical test>> then


<<Statement if Logical Test is true>>
Else

OR

If <<Logical test>> then


<<Statement#1 if Logical Test is true>>

<<Statement#last if Logical Test is true>>
Else
Decisions
The instruction(s) that need(s) to be executed if the Logical Test is false is inserted on the next line but indented. The indentation
shows the instruction(s) that need to be executed if the condition is not met.

If <<Logical test>> then


<<Statement if Logical Test is true>>
Else
<<Statement if Logical Test is false>>

OR
If <<Logical test>> then
<<Statement#1 if Logical Test is true>>

<<Statement#last if Logical Test is true>>
Else
<<Statement#1 if Logical Test is false>>

<<Statement#last if Logical Test is false>>
Decisions
The word EndIf (or End If) indicates the end of the conditional structure

If <<Logical test>> then


<<Statement if Logical Test is true>>
Else
<<Statement if Logical Test is false>>
EndIf

OR
If <<Logical test>> then
<<Statement#1 if Logical Test is true>>

<<Statement#last if Logical Test is true>>
Else
<<Statement#1 if Logical Test is false>>

<<Statement#last if Logical Test is false>>
EndIf
Let Us Create a Pseudocode Solution With A Problem

Problem-
Write a program that accepts a student’s mark. If the mark is greater
than or equal to 50 the student passes and fails if their mark is less
than 50. This program must print if the student passed or failed.
I.P.O. Chart/Defining Diagram
Input Processing Output

A student’s mark (1) Read a student’s mark. Message either

  (2) Determine if the student Student passed


passed or failed. or
Student failed
(3) Print appropriate
message either student
passed or student failed.
Pseudocode Algorithm
Pseudocode Algorithm
ACTIVITIES
Activity #1
Problem-
Write a program could tell a user whether they are old enough to learn how to drive a car. If the user's age
meets the required driving age which is 17 years old the program would print the Message You are old
enough to drive a car. Otherwise, it would print the Message You are not old enough to drive a car.

Create the I.P.O. Chart First


Then Create the Pseudocode Algorithm
Activity #2
Problem-
Write a program that accepts two whole numbers. It then determines which number is larger and prints the
larger number of the two numbers.

Create the I.P.O. Chart First


Then Create the Pseudocode Algorithm
THE END

You might also like