Download - 8.2 Selection
![Page 1: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/1.jpg)
CONTROL STRUCTURE• SELECTION
8.2 Approach in Problem Solving
1
![Page 2: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/2.jpg)
LEARNING OUTCOME
At the end of this topic, students should be able to:❑ explain the purpose of selection control
structure.❑ apply selection control structure in problem
solving.
2
![Page 3: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/3.jpg)
CONTROL STRUCTURE : SELECTION
What is Selection Structure?• Also called decision structure.• Purpose :
• statement is used to determine which of different statements to execute depending on certain conditions
• The selection structure allows instructions to be executed non-sequentially.
• Make a decision, and then takes appropriate action based on that decision.
3
![Page 4: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/4.jpg)
CONTROL STRUCTURE : SELECTION
4
SELECTION Analogy
Example 1 : the bus driver can decide whether he will stop the bus to take a passenger or just proceed a driving.
![Page 5: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/5.jpg)
CONTROL STRUCTURE : SELECTION
5
SELECTION Analogy
Example 2 : you drive a car and approach an intersection, whether you need to turn left or turn right.
![Page 6: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/6.jpg)
CONTROL STRUCTURE : SELECTION
6
SELECTION Analogy
Example 3: You need to choose a drink from a vending machine.
Pepsi??
coke??100-plus??
![Page 7: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/7.jpg)
CONTROL STRUCTURE : SELECTION
7
SELECTION Analogy
Example 4: You need to choose either you need to move A or B which is determine a different effect.
AB
If I move B.. If I move
A..
![Page 8: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/8.jpg)
CONTROL STRUCTURE : SELECTION• The way to form an expression or condition in
selection statement.
• Expression or condition is used to make a comparison, then generate a Boolean expression either TRUE or FALSE.
• To form an expression/condition, we are using 6 relational operator (=, ≠, ≥ , ≤, >, <)
• Example;12 > 15 is FALSE7 ≤ 8 is TRUEif mark is 100, then
mark < 80 is FALSEmark ≠ 80 is TRUE
8
![Page 9: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/9.jpg)
CONTROL STRUCTURE : SELECTIONRelational Operator
Expression / condition Relational operator
1. Equal =2. Greater than >
3. Less than <
4. Greater or equal ≥
5. Less or equal ≤
6. Not equal ≠
9
![Page 10: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/10.jpg)
CONTROL STRUCTURE : SELECTION• Example :
Situation/problem Expression / condition
If the mark is greater than or equal 80, the result is Pass, otherwise fail.
Mark ≥ 80
The program will receive two numbers. If the first number is greater than second number, so the message “number1 is larger than number2”.
A > BNum1>num2
The job requirement is only open for the person that age 20 and above.
Age ≠ 20
10
![Page 11: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/11.jpg)
CONTROL STRUCTURE : SELECTION
11
Type of Selection construct /statement
if If-else Nested if
If – else if If- if else
![Page 12: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/12.jpg)
SELECTION → (1) if
If construct • The simplest and most common selection structure is
the if statement which is written in a statement of the form:
if (condition/expression) statement(s)End if
• The if statement tests for a particular condition (expressed as a Boolean expression) and only executes the following statement(s) if the condition is TRUE.
• But, if the condition is FALSE, then, statement for IF is ignored and the process will execute the next statement.
12
![Page 13: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/13.jpg)
SELECTION → (1) if
Pseudocode format: flow chart format:
13
if(expression/condition) statement_1End if expression
statement_1
false
trueExample :If the student’s mark is greater than or equal 50, print “pass”
![Page 14: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/14.jpg)
EXAMPLE : If students mark is greater than or equal to 60, then, the message will display “Passed”. Problem analysis : Flow chart ;Input : markProcess : determine mark to identify whether PassedOutput : message “passed”
Pseudocode
14
Start input mark if mark greater than or equal to 60, print “passed” End ifEnd
Mark ≥ 60
Input mark
False
True
Print “passed”
start
end
![Page 15: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/15.jpg)
EXAMPLE 2 : If BMI value is greater than or equal to 20, then, the message will display “you are overweight”. Problem analysis : Flow chart ;Input : BMIProcess : determine BMI to identify whether you are overweightOutput : message “you are overweight”
Pseudocode
15
Start input BMI if BMI greater than or equal to 20, print “you are overweight” End ifEnd
BMI ≥ 20
Input BMI
False
True
Print “you are overweight”
end
start
![Page 16: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/16.jpg)
if(expression) statement-1; else statement-2;End if
SELECTION → (2) if - else
16
• Often it is desirable for a program to take one branch if the condition is TRUE or another statement if the condition is FALSE. This can be done by using an if- else selection statement:
• Statement-1 will be executed if the expression value is TRUE.• Statement-2 will be executed if the expression is FALSE
• Either statement_1 or statement_2 is executed but not BOTH.
if (expression) statement_1
else statement_2
End if
![Page 17: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/17.jpg)
SELECTION → (2) if-else
Pseudocode format: Flow chart format:
17
if (expression)
statement_1else
statement_2End if
expression
statement_1statement_2
false
true
Example :If the student’s mark is greater than or equal 50, print “pass”. Otherwise, print “fail”
![Page 18: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/18.jpg)
SELECTION → (2) if-else
Example 1: If students mark is greater than or
equal to 60, then, the message will display “Passed”. Else, the message will display “Fail”.
18
![Page 19: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/19.jpg)
SELECTION → (2) if-else
IPO Analysis:
Input: marksProcess: determine mark to identify whether Passed or FailOutput: message “passed” OR “Fail”
19
![Page 20: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/20.jpg)
SELECTION → (2) if-else
IPO Analysis:
Input: marksProcess:
if marks greater than or equal to 60print “Passed”
elseprint “Fail”
Output: message “passed” OR “Fail”
20
Mathematical operation also can be simplified by statement
if mark ≥ 60
![Page 21: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/21.jpg)
SELECTION → (2) if-elsePseudocode
21
startread marks
if (marks ≥ 60)print “Passed”
elseprint “Fail”
End ifend
![Page 22: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/22.jpg)
SELECTION → (2) if-else
Flow chart
22
start
end
Input marks
Print “Passed”
Marks ≥ 60
true Print “Fail”
false
22
![Page 23: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/23.jpg)
SELECTION → (2) if-else
Example 2: If x is greater than y, display “x is bigger than y” else display “x is smaller than y”.
23
![Page 24: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/24.jpg)
SELECTION → (2) if-else
IPO Analysis:
Input: x,yProcess: determine x to identify whether x is bigger than y or x is smaller than yOutput: message “x is bigger than y” OR “x is smaller than y”
24
![Page 25: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/25.jpg)
SELECTION → (2) if-elsePseudocode
25
startread x,y
if (x > y)print “x is bigger than y”
elseprint “x is smaller than y”
End ifend
![Page 26: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/26.jpg)
SELECTION → (2) if-else
Flow chart
26
start
end
Input x, y
Print “x is bigger than y”
x > y
true Print “x is smaller than y”
false
26
![Page 27: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/27.jpg)
SELECTION → (2) if-else
Example 3: the program will print the status. If CGPA less than 2.00, status is ‘F’. Otherwise, status is ‘P’.
Answer :if (CGPA<2.00)
status = ‘F’else
status = ‘P’Print statusEnd if
27
Status =‘F’
Print Status
CGPA<2.00
Status=‘P’
false
true
![Page 28: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/28.jpg)
• IF BMI VALUE IS GREATER THAN OR EQUAL TO 20, THEN, THE MESSAGE WILL DISPLAY “YOU ARE OVERWEIGHT”. OTHERWISE, DISPLAY “YOU ARE NORMAL”
- DO PROBLEM ANALYSIS- PSEUDO CODE- FLOWCHART
TRY THIS !!!!
28
![Page 29: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/29.jpg)
SELECTION → (3) Nested if
• The if-else construct can also be nested (placed one within another) to any depth.
• Nested if generally take they forms : if-else if and if-if else
29
![Page 30: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/30.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT)
• Occasionally a decision has to be made on the value of a variable which has more than two possibilities.
• This can be done by placing if statements within other if-else constructions.
if( expression-1 ) statement-1 else if( expression-2 ) statement-2
……… else if( expression-n ) statement-n else last statement
End if• The boolean-expression will tested from the Boolean-expression-1 until it
find the condition is True, then the statement will be executed.• Rules → Only ONE of the statements is executed which is the Boolean-
expression is meet the True condition.
30
![Page 31: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/31.jpg)
Example 1: The program will display the following message based on following BMI.
31
SELECTION → (3) Nested if(if-else if STATEMENT)
BMI message>20 “you are overweight”=20 “you are normal”<20 “ you are thin”
![Page 32: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/32.jpg)
Example 1: The program will display the following message based on following BMI.
32
SELECTION → (3) Nested if(if-else if STATEMENT)
BMI message>20 “you are overweight”=20 “you are normal”<20 “ you are thin”
ifelse if
else
![Page 33: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/33.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT)IPO Analysis:
Input: BMI
Process: Determine the BMI to identify whether overweight or normal or thinOutput: message /display “you are overweight” or “you are normal” or “you are thin”
33
![Page 34: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/34.jpg)
SELECTION → (3) Nested if (if-else if STATEMENT)
Pseudocode
34
startread BMI
if (BMI > 20) print “you are overweight”else if (BMI = 20) print “you are normal”
else print “you are thin”End if
end
![Page 35: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/35.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT)
start
end
Input BMI
BMI > 20true Print
“you are overweight
”false
BMI = 20 Print “you are normal”false
Print “you are thin”
true
35
![Page 36: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/36.jpg)
Example 2: The program will display the grade based on following mark.
36
SELECTION → (3) Nested if(if-else if STATEMENT)
Mark grade ≥ 80 A ≥ 60 B ≥ 50 C<50 F
![Page 37: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/37.jpg)
Example 2: The program will display the grade based on following mark.
37
SELECTION → (3) Nested if(if-else if STATEMENT)
Mark grade ≥ 80 A ≥ 60 B ≥ 50 C<50 F
ifelse if
else if
else
![Page 38: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/38.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT)IPO Analysis:
Input: mark
Process: Determine grade A or B or C or F based on markOutput: message/display “ A” or “B” or “C” or “F”
38
![Page 39: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/39.jpg)
SELECTION → (3) Nested if (if-else if STATEMENT)Pseudocode
39
startread mark
if (mark ≥ 80) print “A”else if (mark ≥ 60) print “B”
else if (mark ≥ 50) print “C” else print “F”End if
end
![Page 40: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/40.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT)
start
end
Input mark
mark ≥ 80true
Print “A”
false
mark ≥ 60
mark ≥ 50
Print“B”
Print“C”
false
falsePrint “F”
true
true
40
![Page 41: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/41.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT
Example 3:
The program read a number, if the number is greater than 0, then display a message "x is positive“, if the number is less than zero, then display the message "x is negative“, and otherwise, display the message “x is 0”.
41
![Page 42: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/42.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT
IPO Analysis:
Input: x
Process: Determine x is positive or negative or 0 number
Output: display/ message “x is positive“ or "x is negative“ or “x is 0”
42
![Page 43: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/43.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT
Pseudo code
43
startread x
if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0“ End if
end
![Page 44: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/44.jpg)
SELECTION → (3) Nested if(if-else if STATEMENT
Flow chart
44
start
end
Input x
(x > 0)true Print "x
is positive”
false(x < 0)
Print “x is
negative”falsePrint “x is
0”
true
![Page 45: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/45.jpg)
• The next nested if ; if-if else • This can be done by placing if statements within other if
statement. if(expression-1 ) if(expression-2 )
if(expression-3) statement-1
elsestatement-2
end ifelse
statement-3 end if
elsestatement-4
End if
SELECTION → (3) Nested if(if-if else STATEMENT)
45
• Expression-1 is evaluated. If it is FALSE, statement-4 is executed and the entire if statement is terminated. If TRUE, control goes to the second if and expression-2 is evaluated.
• If it is FALSE, statement-3 is executed; if TRUE, control goes to third if and expression-3 is evaluated.
• If it is FALSE, statement-2 is executed; if TRUE, statement-1 is executed.
![Page 46: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/46.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• Example 1; The job vacancy is open for the person that is Male and have more than 5 years experience. Display a message “eligible” if the person meet both requirement. Otherwise, display “not eligible”.
46
![Page 47: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/47.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• IPO ANALYSISInput : sex, year experienceProcess : Determine sex and year experience to identify whether eligible or not eligible
Output :display/message “eligible” or “not eligible”
47
![Page 48: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/48.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• pseudocodeStart
Input sex and year experienceif (sex=‘m’)
if(year experience>5)display “eligible”
elsedisplay “not eligible”
end ifEnd if
End
48
![Page 49: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/49.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• flowchart
49
Input sex, year experience
Sex=‘m’
Year experience =
5Print “not eligible”
Print “eligible”
True
True
false
false
start
end
![Page 50: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/50.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• Example 2; The program will display the message based on CGPA and Program enrolled by student.
50
CGPA PROGRAM MESSAGE<2.00 PST GO TO PDT
PDT FAIL TO CONTINUE
≥ 2.00 PST PASS
PDT PASS
![Page 51: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/51.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• IPO ANALYSISInput : CGPA and ProgramProcess : Determine CGPA and program to identify whether go to PDT or fail to continue.Output :message “pass” or “go to PDT” or “fail to continue”
51
![Page 52: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/52.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• pseudocodeStart
Input CGPA and Programif (CGPA<2.00)
if(program=PST)display “go to PDT”
elsedisplay “fail to continue”
End ifelse
display “pass”End if
End
52
![Page 53: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/53.jpg)
SELECTION → (3) Nested if(if-if else STATEMENT)
• flowchart
53
Input CGPA, Program
CGPA<2.00
Program= PST
Print “Pass”
Print “fail to
continue”
Print “Go to PDT ”
True
True
false
false
start
end
![Page 54: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/54.jpg)
54
1. Prepare a program that enter marks for student sit for a test. Passing marks is 50. Pass students were been identify by two groups which above and equal to 75 is excellent, else is good.
2. Student enter their CGPA and the system print or identify CGPA for student. If CGPA below than 2.0 the status is failed else the status is pass.
3. Prepare a program that user can enter two number and the program will identify which number is bigger.
4. User inserts two numbers in the program. The program will determine if the two numbers is not less than 0.0 the program will calculate and display three mathematical calculations – addition, multiplication and modulus based on the numbers given.
5. Prepare a program that reads marks and attendance for student. Student sit for a test and the status is pass if the student get marks not less than 40 and attendance to class is not less than 30 times. If not pass give reasons. Display the mark and attendance of student.
6. Requirement for membership of an exclusive club is not earning less than RM5000 per month and the age of members not less than 20 years. Prepare a program that can determine a person is eligible or not to join the club if given monthly income and age. If not eligible, give reasons.
CONTROL STRUCTURE• SELECTION
![Page 55: 8.2 Selection](https://reader036.vdocument.in/reader036/viewer/2022062503/5899c28e1a28ab45548b4ec9/html5/thumbnails/55.jpg)
55
CGPA Range Degree Class
3.75<=CGPA<=4.00 First Class
3.0<=CGPA<3.75 Second Class Upper
2.75<=CGPA<3.0 Second Class Lower
2.0<=CGPA<2.75 General Degree
CGPA<2.0 Failed
•Prepare a program that can determine undergraduate class for universities student base on the CGPA table below: