PROGRAM CONTROL
STRUCTURES
Sequence
Selection
OBJECTIVE
At the end of this topic, the students should be able to:
3.1 Understand Program Control Structures
•
Explain the control structures in problem solving:
• Sequence • Selection
• Repetition (looping)
•
Use flowcharts (graphic algorithm) to show the flow of control
using these structures.
CONTENTS
•
Blocks of Code
•
Program Flow (Control Structure)
•
Sequential Control Structure
•
Selection Control Structure
•
If……Endif
•
If……Else
•
Nested If
•
Loop Control Structure
•
For
•
While
BLOCK OF CODE
is a group of statements that performs one particular task
void welcome ( ) {
printf (“****************************”); printf (“********WELCOME **********”); printf (“****************************”); reference and execution a program
PROGRAM FLOW
•
Program flow in computer is controlled by the control structure.
•
Control structure is a logical structure that controls the flow of
instruction to be executed by computer.
•
There are 3 types of control structures:
1. Sequential control structure
2. Selection / decision control structure
• If……endif
• If……else
• Nested if
3. Looping control structure
• For
• While
1. Sequential control structure
• In this control, every step will be executed
one by one from top to bottom.
• Format:
Flowchart :
START
Statement A
END Statement B
Pseudocode:
Start
Example
Problem:
•
Mathematical operation:
Algorithm:
1. Enter 2 numbers
2. Add 2 numbers
Sum = number_1 + number_2
3. Minus 2 numbers
Subtract = number_1 – number_2
4. Multiply 2 numbers
Multiple = number_1 * number_2
5. Division of 2 numbers
Divide = number_1 / number_2
6. Display Sum, Subtract, Multiple and
Divide
Problem analysis: Input:
number_1, number_2
Process:
Add 2 numbers:
Sum = number_1 + number_2 Minus 2 numbers:
Subtract = number_1 – number_2 Multiply 2 numbers:
Multiple = number_1 * number_2 Division 2 numbers:
Divide = number_1 / number_2
Output:
Flowchart:
START
Input number_1, number_2
Sum = number_1 + number_2
Output Sum, Subtract, Multiple, Divide
END
Subtract = number_1 – number_2
Multiple = number_1 * number_2
Divide = number_1 / number_2
2. Selection / Decision Control
Structure
• This type of control structure is usually used in structured
programming
• This control structure will execute an instruction
based on
result of a condition or comparison
.
• A condition will result either
TRUE or FALSE
.
• If the condition result is true, the control program will
execute the instruction within the TRUE loop operation.
• Otherwise, it will execute the next instruction or the
flowchart of control structure:
Statement that will be executed if condition is
not true Condition
Statement that will be executed if condition is
true True
Example 1:
•
Condition:
A person can obtain a license
when he/
she is above 21 years old
•
Decision:
If true then she / he is qualified to have driving license.
START
Input age
END False
True
Output “Qualified”
3 Type of selection / decision
control structure:
Seelction 1: IF…….END IF
•
Rules:
If (condition)
Instruction (do this instruction if condition is true)
Endif
Flowchart:
START
Conditio
n Statement
END False
True
Pseudocode:
If (condition)
Example IF…ENDIF
• Workers who work on shift 3 will receive
additional Bonus RM50, where basic salary is
entered by workers.
Problem analysis: Input: 1. Shift
2. Basic_salary
Process: Bonus equals RM 50 If Shift equals to 3:
Salary equals Bonus plus Basic_salary
Output: Salary
Algorithm:
1. Enter Basic_salary, Shift 2. Bonus equals to RM 50 3. Check workers Shift
3.1 If Shift equals to 3
START
If Shift = 3 Salary = Basic_salary + Bonus
END False
True Bonus = 50
Input Shift, Basic_salary
Output Salary
Pseudocode:
START
Selection 2: IF…….ELSE
• A selection of control structure is used to
select between two options
Rules:
If (condition)
True statement Else
False statement Endif
Pseudocode:
If (condition)
True statement Else
Flowchart:
START
Conditio n
Statement 1
END
False True
Example IF…ELSE
• Prepare a problem analysis, algorithm, flowchart
and pseudocode to identify whether a student is
qualified to further her / his studies in any local
university using his / her SPM grade equal to 1.
Problem analysis:
Input: Grade Output: Display message qualified or not
Algorithm:
Flowchart: “Qualified to further study” Output “Not
Example 2 - IF…ELSE
Problem:
• Prepare the problem analysis, algorithm, flowchart and
pseudocode to find subtraction between two numbers that
users enter.
Problem analysis:
Input: num1, num2
Process: If num1 greater than num2 Result = num1 – num2 If not
Result = num2 – num1 Output: Result
Algorithm:
Enter num1, num2
Compare the 2 numbers
If num1 greater than num2 Result = num1 – num2 If not
Flowchart:
Input num1, num2
Selection 3: NESTED IF
• There are 3 types:
1. Type 1:
If (condition1)
If (condition2)
If (condition3) True statement Endif
Endif
2. Type 2:
If (condition1)
If (condition2) If (condition3)
Statement that will be executed if condition1, condition2 and condition3 are true Else
Statement that will be executed if condition1, and condition2 are true but condition3 is false
Endif
Else
Statement that will be executed if condition1 is true but condition2 and condition3 is false
Endif
Else
3. Type 3
If (condition1)
Statement that will be executed if condition 1 is true Else
If (condition 2)
Statement that will be executed if condition2 is true but condition1 is false Else
If (condition3)
Statement that will be executed if condition3 is true but condition1 and condition2 are false
Else
Statement that will be executed if condition1, condition2 and condition3 are false
Endif
Endif
Example 1 – NESTED IF
Problem:
Problem analysis:
Input: CGPA, Year, Salary. Process:
1. Check if the student application’s can be considered or not for a scholarship. 1.1 If Year greater than 1
1.1.1 If CGPA greater than or equal to 3.00
1.1.1.1 If Salary is less than or equal to RM500 Output “Your application is under consideration”.
Output: Student status
Algorithm:
1. Enter CGPA, Salary and Year
2. Check if the student application’s can be considered for a scholarship 2.1 If year > 1
2.1.1 If CGPA >= 3.00
START
END False True
Input Year, CGPA, Salary
Output “Your application is under
consideration” If Year > 1
If CGPA >= 3.00
If Salary <= 500 False
False True
True
Pseudocode:
START
Input Year, CGPA, Salary
If Year >1
If CGPA >= 3.00
If Salary <= RM500
Output “Your application is under consideration”
Endif
Endif
Endif
Example 2 – NESTED IF
Problem:
Problem analysis:
Input:
CGPA, Year, Salary
Process:
Check if a student can be considered for a scholarship
1.1 If Year > 1
1.1.1
If CGPA >= 3.00
1.1.1.1 If Gaji <= 500
Output “You application is under consideration”
1.1.1.2 If not
Output “Not success”
1.1.2
If not
Output “Not success”
If not
Output “Not success”
Algorithm:
1. Enter CGPA, Year, Salary
2. Check if a student can be considered for a scholarship
2.1 If Year > 1
2.1.1
If CGPA >= 3.00
2.1.1.1 If Salary <= RM500
Output “Your application is under consideration”.
2.1.1.2 If not
Output “Not success”
2.1.2
If not
Output “Not success”
2.2 If not
Flowchart:
False
START
END True
Input Year, CGPA, Salary
Output “Your application is under
consideration” Output “Not
success”
Output “Not success”
Pseudocode:
Input CGPA, Salary, Year
If (year > 1)
If (CGPA >= 3.00)
If (salary <= RM500)
Output “Your application is under consideration”
Else
Output ”Not success”
Endif
Else
Output ”Not success”
Endif
Else
Example 3 – NESTED IF
Problem:
• Education status is
determined based on
the GPA achievement
under the following
scheme:
GPA
Status
3.50-4.00
Dean List
2.00-3.49
Pass
Problem analysis: Input: GPA
Process:
1. If (GPA < 0.00 AND GPA > 4.00) Output “Invalid data”
2. If not
2.1 If (GPA >=3.5 AND GPA <= 4.00) Output “Dean List”
2.2 If not
2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass”
2.2.2 If not
2.2.2.1 If (GPA >= 1.80 AND GPA< 2.00) Output “Conditional Pass” 2.2.2.2 If not
Output “Fail”
Algorithm:
1. Enter student GPA
2. Compare student’s GPA to determine his/ her education status. 2.1 If (GPA < 0.00 AND GPA > 4.00)
Output “Invalid data” 2.2 If not
2.2.1 If (GPA >=3.50 AND GPA <= 4.00) Output “Dean List”
2.2.2 If not
2.2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass”
2.2.2.2 If not
2.2.2.2.1 If (GPA >= 1.80 AND GPA < 2.00) Output “Conditional Pass”
oFlowchart: Output “Invalid
data”
Output “Dean List” If GPA < 0.00 &&
“Conditional Pass” Output “Fail”
Output “Pass”
False True
Pseudocode:
START Input GPA
If ((GPA < 0.00) AND (GPA > 4.00)) Output "Invalid Data"
Else
If ((GPA >= 3.50) AND (GPA <= 4.00)) Output "Dean List"
Else
If ((GPA >=2.00) AND (GPA < 3.50)) Output "Pass"
Else
If ((GPA >= 1.80) AND (GPA < 2.00)) Output "Conditional Pass” Else
Output "Fail" Endif
Endif Endif
Symbol
Pseudo code
Symbol
C Language Example Result Description
AND && (1 > 3) && (10 < 20) If ((a < b) && (c > d)) printf(“Print a and c”);
FALSE Both sides of the condition must be true
OR || (1 > 3) || (10 < 20) must be true
3. Looping Control Structure
•
A programming control structure that
allows a
series of instructions
to be
executed more than
once
. The similar statement is repeated several
times
until the conditions are fulfilled
.
•
Three are 3 types of loop:
1. For
2. While
For For (initialize; condition; counter) True statement if condition is fulfilled Endfor
While While (condition) True statement Endwhile
Do…while Do
True statement While (condition)
• Initialize value: a value to start a loop.
• Counter: update-to increase or decrease the initialize value. • Rules for the condition:
1. If the condition is true / fulfilled, the process will be performed.
2. Then, the program loops back and recheck the condition, if the condition is true / fulfilled, repeat the process.
False
Example of loops usage:
Problem:
• Prepare the problem analysis, algorithm,
Problem analysis:
• Input: 5 numbers
• Process:
The process of adding
numbers will repeat until the
condition to exit the loop is
met.
• Output: Average of 5 numbers
Algorithm:
1. Initialize Counter=0; Average = 0; Total = 0
2. Input number
3. Add Total using formula:
Total = Total + number
4. Add Counter using formula:
Counter = Counter + 1
5. Compare whether Counter is greater than 5
If yes , go to step 6
If not, go to step 2
6. Calculate Average of numbers using formula;
Average = Total/5
Pseudocode:
For loop
While loop
Do…while loop
oFlowchart for For and While loops:
False
True
START
no = 0
Total = 0
For / While no <= 5
Input Num
Total = Total + Num
no = no + 1
Avg = Total/5
Output Avg
Note:
no: a variable to control loop structure whether to continue or exit from the loop
no + 1: a counter and it is very important. Without a counter, the loop will be infinite
Total = Total + Num: a variable to compute the value
Note:
no: a variable to control loop structure whether to continue or exit from the loop
no + 1: a counter and it is very important. Without a counter, the loop will be infinite
Total = Total + Num: a variable to compute the value
Avg = Total/5
Output Avg
END Avg = 0
Example
* Algorithm for For and While loops:
1.Initialize Counter = 0, Salary = 0;
2.Compare whether Counter is greater than 20 or not
If yes , out from the loop
If not , go to step 3
3. Enter Basic_salary, Claim, O_time
4. Calculate EPF:
EPF = Basic_salary * 0.09
5. Calculate Salary using this formula:
Salary = Basic_salary + Claim + O_time – EPF
6. Display Salary
7. Add Counter using the formula:
Counter = Counter + 1
Flowchart for For and While loops:
False True
START no = 0
For / While no <= 20
Input Basic_salary, Claim, O_time
Salary = Basic_salary + Claim + O_time – EPF
no = no + 1
EPF = Basic_salary * 0.09
Output Salary
Pseudocode for For loop:
START
no = 0 Salary = 0
For (no = 0, no <=20, no ++)
Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09
Salary = Basic_salary + Claim + O_time – EPF Output Salary
Endfor END
Pseudocode for While loop:
START
no = 0 Salary = 0 While no <=20
* Algorithm for Do……while loop:
1. Initialize Counter = 0, Salary = 0;
2. Input Basic_salary, Claim, O_time
3. Calculate EPF
EPF = Basic_salary * 0.09
4. Calculate Salary using this formula:
Salary = Basic_salary + Claim + O_time – EPF
5. Add Counter using this formula:
Counter = Counter + 1
6. Compare whether the Counter is greater than 20 or not
If yes , out of loop
Flowchart for Do…..while loop:
False True
START
no = 0
Salary = 0
no <= 5
Input Basic_salary, Claim, O_time
EPF = Basic_salary * 0.09
no = no + 1
Salary = Basic_salary + Claim + O_time – EPF
Output Salary
Pseudocode for Do…..while loop: