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

Study Unit 4 - Selection Control Structures

The document discusses selection control structures in programming. It covers simple IF statements, IF/ELSE statements, combined IF statements using AND and OR operators, and CASE statements. Examples are provided to demonstrate how each structure can be used to make decisions based on different conditions being true or false.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Study Unit 4 - Selection Control Structures

The document discusses selection control structures in programming. It covers simple IF statements, IF/ELSE statements, combined IF statements using AND and OR operators, and CASE statements. Examples are provided to demonstrate how each structure can be used to make decisions based on different conditions being true or false.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Problem

Solving (ITSP 111)


SU 4 - SELECTION CONTROL STRUCTURES

Mr DC Taole
REVISION:

• You have learned to:


• Which three techniques in terms of problem solving
did you learn in the previous SU?
• Draw up problem defining diagrams (IPO tables)
• Write solution algorithms in pseudo code
• Desk check the solution algorithm
QUESTION:

• How do you decide what to wear in the morning?


• Did you consider the weather?
• What influences your decision?
• Do you think that decision making is important in
programming?
• Remember that it is 5th basic operation of a computer.
SU 4 – OUTCOMES FOR LESSON

You will be able to :


1. Recognize the role of sequence
2. Recognize the role of selection control structure
3. Apply knowledge to real world problems
SU 4 – SELECTION STRUCTURE P.43

The selection structure :

• Represents decision making abilities of computer

• Is used to illustrate a choice between two or


more actions

• Depends on whether condition is true or false


SU 4 – SELECTION STRUCTURE P.43

• The IF statement is based on the comparison of 2


statements

The following operators can be used :


• < less than
• > greater than
• = equal to
• <= less than or equal to
• >= greater than or equal to
• <> not equal to
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT Structure :

IF condition = true THEN


statement
ELSE (when condition is false)
statement
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Account balance = R 4500

IF acount_balance > 3000 THEN


service_charge = 5
ELSE
service_charge = 2
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Account balance = R 4500
IS THE CONDITION TRUE?
IF 4500 > 3000 THEN YES, SO THIS LINE IS EXECUTED

service_charge = 5
ELSE
service_charge = 2
ENDIF ENDIF

DOES ELSE GET EXECUTED?


NO, SO THIS LINE IS NOT EXECUTED
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Account balance = R 1500

IF acount_balance > 3000 THEN


service_charge = 5
ELSE
service_charge = 2
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Account balance = R1500
IS THE CONDITION TRUE?
IF 1500 > 3000 THEN NO, SO THIS LINE IS NOT EXECUTED

service_charge = 5
ELSE
service_charge = 2
ENDIF
DOES ELSE GET EXECUTED?
YES, SO THE FOLLOWING LINES GET EXECUTED
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT VARIATION :

When ELSE is omitted, the IF will be bypassed.

IF condition = true THEN


statement is executed
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Student status = “part time”

IF student_status = “part time” THEN


part_time_count  part_time_count + 1
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Student status = “part time”

IF student_status = “part time” THEN IS THE CONDITION TRUE?


part_time_count  part_time_count + 1
YES, SO THIS LINE IS EXECUTED
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Student status = “parttime”

IF student_status = “part time” THEN


part_time_count  part_time_count + 1
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Student status = “parttime”

IF student_status = “part time” THEN IS THE CONDITION TRUE?


part_time_count  part_time_count + 1
ENDIF NO, SO THIS LINE IS EXECUTED
PRINT part_time_count
SU 4 – SELECTION STRUCTURE
ACTIVITY :

Orange pies  grey_table


Blue pies  yellow_table
SU 4 – SELECTION STRUCTURE
ACTIVITY IF STATEMENT :

IF pie_color = “blue” THEN


put pie on yellow_table
ELSE (IF pie_color = “orange” THEN)
put pie on grey_table
ENDIF
SU 4 – SELECTION STRUCTURE
ACTIVITY IF STATEMENT :

Now can you count the number of blue and orange


pies?

Add statements to your IF statement in order to


count the number of blue and orange pies.
SU 4 – SELECTION STRUCTURE
ACTIVITY IF STATEMENT :

IF pie_color = “blue” THEN


put pie on yellow_table
blue_pie_count  blue_pie_count + 1
ELSE
put pie on grey_table
orange_pie_count  orange_pie_count +1
ENDIF
SU 4 – SELECTION STRUCTURE
ACTIVITY IF STATEMENT REMEMBER:
SET blue_pie_count  0
SET orange_pie_count  0
IF pie_color = “blue” THEN
put pie on yellow_table
blue_pie_count  blue_pie_count + 1
ELSE
put pie on grey_table
orange_pie_count  orange_pie_count +1
ENDIF
SU 4 – SELECTION STRUCTURE
COMBINED IF STATEMENT :
The keywords AND/OR can be added.

IF cond1 = true AND cond2 = true THEN


statement
ENDIF

IF cond1 = true OR cond2 = true THEN


statement
ENDIF
SU 4 – SELECTION STRUCTURE
COMBINED IF STATEMENT :
Student status = “part time” gender = “female”

IF stud_status = “part time” AND gender = “female”


THEN
part_time_count  part_time_count + 1
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Student status = “part time” gender = “female”

IF stud_status = “part time” AND gender = “female”


THEN IS THE CONDITION TRUE?
YES, SO THIS LINE IS EXECUTED
part_time_count  part_time_count + 1
ENDIF
SU 4 – SELECTION STRUCTURE
COMBINED IF STATEMENT :
Student status = “full time” gender = ‘F’

IF stud_status = “full time” AND gender = “female”


THEN
part_time_count  part_time_count + 1
ENDIF
SU 4 – SELECTION STRUCTURE
SIMPLE IF STATEMENT :
Student status = “part time” gender = ‘F’
IS THE CONDITION TRUE?
IF stud_status = “part time” AND gender = “female”
THEN
part_time_count  part_time_count + 1
ENDIF NO, SO THIS LINE IS EXECUTED
SU 4 – SELECTION STRUCTURE
COMBINED IF STATEMENT :
Student status = “full time” gender = “female”

IF stud_status = “full time” OR gender = “female”


THEN
part_time_count  part_time_count + 1
ENDIF
SU 4 – SELECTION STRUCTURE
COMBINED IF STATEMENT :
Student status = “full time” gender = “female”

IF stud_status = “full time” OR gender = “female”


THEN IS THE CONDITION TRUE?
YES, SO THIS LINE IS EXECUTED
part_time_count  part_time_count + 1
ENDIF
SU 4 – CASE STRUCTURE
The case control structure in pseudocode is another way of expressing a lin­ear
nested IF statement.
It is used in pseudocode for two reasons: it can be directly translated into many
high-level languages, and it makes the pseudocode easier to write and understand.
Nested IFs often look cumbersome in pseudocode and depend on correct structure
and indentation for readability.
Example:
1 IF record_code = 'A1 THEN
2 increment counter_A
ELSE
3 IF record_code = 'B' THEN
4 increment counter_B
ELSE
5 IF record_code = 'C' THEN
6 increment counter_C
ELSE
7 increment error_counter
ENDIF
ENDIF
ENDIF
SU 4 – CASE Statement
This linear nested IF structure can be replaced with a case control structure. Case is
not really an additional control structure. It simplifies the basic selection control
structure and extends it from a choice between two values to a choice from
multiple values. In one case structure, several alternative logical paths can be
represented. In pseudocode, the keywords CASE OF and ENDCASE serve to identify
the structure, with the multiple values indented.

CASE OF record_code
1 'A' : increment counter_A
2 'B' : increment counter_B
3 'C' : increment counter_C
4 other : increment error_counter
ENDCASE
SU 4 – SELECTION STRUCTURE

What did you learn today?


A computer can compare 2 or more values using the:
• Simple IF statement
• IF ELSE
• Combined IF (OR/AND operators)
• CASE statement

You might also like