Date post: | 10-Apr-2018 |
Category: |
Documents |
Upload: | kamarul-hafifi |
View: | 217 times |
Download: | 0 times |
of 16
8/8/2019 Algo Selection WEEK4
1/16
AlgorithmAlgorithm::
Selection Control StructuresSelection Control Structures
ISB10103
8/8/2019 Algo Selection WEEK4
2/16
ObjectivesObjectives
In this chapter you will be able to:
Elaborate on the uses of simple selection, multipleselection, and nested selection in algorithms
Develop algorithms using variations of theselection control structure
8/8/2019 Algo Selection WEEK4
3/16
8/8/2019 Algo Selection WEEK4
4/16
Simple selection occurs when a choice is made between
two alternate paths, depending on the result of a
condition being true or false
The structure is represented in pseudocode using thekeywords IF, THEN, ELSE, and ENDIF
Only one of the THEN or ELSE paths will be followed,
depending on the result of the condition in the IF clause
1 Simple Selection (Simple IF1 Simple Selection (Simple IF
Statement)Statement)
8/8/2019 Algo Selection WEEK4
5/16
2 Simple Selection with Null2 Simple Selection with NullFalse Branch (Null ELSEFalse Branch (Null ELSE
Statement)Statement)
The null ELSE structure is a variation of thesimple IF structure
It is used when a task is performed only whena particular condition is true
If the condition is false, then no processing
will take place and the IF statement will bebypassed
8/8/2019 Algo Selection WEEK4
6/16
3 Combined Selection3 Combined Selection
(Combined IF Statement)(Combined IF Statement) A combined IF statement is one that contains multiple
conditions, each connected with the logical operatorsAND or OR
If the connector AND is used to combine the conditions,then both conditions must be true for the combinedcondition to be true
If the connector OR is used to combine any twoconditions, then only one of the conditions needs to betrue for the combined condition to be considered true
8/8/2019 Algo Selection WEEK4
7/16
The NOTOperatorThe NOTOperator
The NOT operator can be used for the logical negationof a condition, as follows:
IF NOT (grade = F) THEN
Display Congratulations! Youve passed
ENDIF
Note that the AND and OR operators can also be usedwith the NOT operator, but great care must be takenand parentheses should be used to avoidambiguity/uncertainty.
8/8/2019 Algo Selection WEEK4
8/16
4 Nested Selection (Nested4 Nested Selection (Nested
IF Statement)IF Statement) Nested selection occurs when the word IF appears more
than once within an IF statement
Nested IF statements can be classified as
i. Linear
ii. Non-linear
8/8/2019 Algo Selection WEEK4
9/16
i. Linear Nested IFi. Linear Nested IF
StatementsStatements The linear nested IF statement is used when a
field is being tested for various values and a
different action is to be taken for each value
This form of nested IF is called linear, because
each ELSE immediately follows the IF
condition to which it corresponds
8/8/2019 Algo Selection WEEK4
10/16
ii. Nonii. Non--Linear Nested IFLinear Nested IF
StatementsStatements A non-linear nested IF occurs when a number
of different conditions need to be satisfied
before a particular action can occur
It is termed non-linear because the ELSE
statement may be separated from the IF
statement with which it is paired
8/8/2019 Algo Selection WEEK4
11/16
Algorithms Using SelectionAlgorithms Using Selection
Let us look at some programming examples
that use the selection control structure
In each example, a solution algorithm will bedeveloped using pseudocode and flowchart
8/8/2019 Algo Selection WEEK4
12/16
Example 1: ProblemExample 1: Problem
Calculate the area of a triangle.
If the area is greater than 100m2,
display This is a BIG triangle.
Otherwise, display
This is a SMALL triangle.
8/8/2019 Algo Selection WEEK4
13/16
Example 1 : SolutionExample 1 : SolutionAlgorithmAlgorithm
Area_of_triangle
Prompt for base, height
Get base, height
area 0.5 x base x height
Ifarea > 100 Then
Display This is a BIG triangleElse
Display This is a small triangle
End If
End
Stop
Display This is aBIG triangle
Start
Get base, height
area 0.5 x base x height
If area > 100?
False
True
Prompt for base, height
Display Thisis a small
triangle
Pseudocode
Flowchart
8/8/2019 Algo Selection WEEK4
14/16
Example 2 : ProblemExample 2 : Problem
Get a value in miles and determine the charges. Convertmiles into kilometers (km).
If value of km is less than 10km, display Local. Thecharge is RM25.
If distance is between 10 and 100km (inclusive), displayOutsider with a charge of RM50.
Otherwise charge RM100, display Alien.
(Formula : 1 mile = 2.6 km)
8/8/2019 Algo Selection WEEK4
15/16
Example 2: SolutionExample 2: SolutionAlgorithmAlgorithm
Area_of_originPrompt for milesGet miles
km miles x 2.6Ifkm < 10 Then
charge 25
Display LocalElse
If km >=10 AND km
8/8/2019 Algo Selection WEEK4
16/16
ExerciseExercise
What is the difference between Sequential andSelection statements?
List all SIX Relational Operators.
List all THREE Logical Operators. Briefly explain the purpose of each keyword
used in selection statements