PROGRAM CONTROL STRUCTURES.ppt

  PROGRAM CONTROL STRUCTURES Sequence Selection

  Repetition (looping)

  

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.
  • Write algorithm using control structures in solving given problems.

  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

  BLOCK OF CODE BLOCK OF CODE BLOCK SEPARATOR BLOCK SEPARATOR

  

is a group of statements that performs one particular task

void welcome ( ) { printf (“****************************”); printf (“********WELCOME **********”); printf (“****************************”); }

  FUNCTION : a block of code used as reference and execution a program

  • Combination of blocks of code will produce a perfect program to be executed in a computer.

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 • Do……while

1. Sequential control structure

  • In this control, every step will be executed one by one from top to bottom.
  • Every box in control structure is a process.

  Every process is done sequentially.

  • Format:

  Flowchart : Pseudocode:

  START

  Start Statement A Statement B

  Statement A

  End

  Statement B END Example Problem:

  • Mathematical operation: Get two numbers, then do adding, subtracting, multiplying and dividing operations.

  Algorithm: Problem analysis: Input:

  number_1, number_2

  1. Enter 2 numbers Process:

  2. Add 2 numbers

  Add 2 numbers:

  Sum = number_1 + number_2

  Sum = number_1 + number_2

  3. Minus 2 numbers

  Minus 2 numbers:

  Subtract = number_1 – number_2

  Subtract = number_1 – number_2

  4. Multiply 2 numbers

  Multiply 2 numbers:

  Multiple = number_1 * number_2

  Multiple = number_1 * number_2

  5. Division of 2 numbers

  Division 2 numbers: Divide = number_1 / number_2

  Divide = number_1 / number_2 Output:

  6. Display Sum, Subtract, Multiple and

  Sum, Subtract, Multiple and Divide

  Divide

  • Flowchart:

  Pseudocode: START

  START

  Input number_1,

  Input number_1, number_2

  number_2

  Sum = number_1 + number_2 Subtract = number_1 - number_2

  Sum = number_1 + number_2

  Multiple = = number_1 * number_2 Divide = number_1 / number_2

  Subtract = number_1 – number_2

  Output Sum, Subtract, Multiple, Divide END

  Multiple = number_1 * number_2 Divide = number_1 / number_2 Output Sum, Subtract, Multiple, Divide

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 instruction within the FALSE loop operation.
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

  False

  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.

    If not he / she is not qualified to have a driving license.

  START Input age False True

  Output “Qualified” Output “Not Qualified” If age > 21

  3 Type of selection / decision control structure:

  1. If……endif

  2. If……else

  3. Nested if

  Seelction 1: IF…….END IF

  • Rules: If (condition) Instruction (do this instruction if condition is true) Endif

    If condition is not true, no instruction will be executed

  Pseudocode:Flowchart: START

  If (condition) True statement

  True

  Endif

  Conditio Statement n False

  END

  

Example IF…ENDIF

  • Workers who work on shift 3 will receive additional Bonus RM50, where basic salary is entered by workers.

  Problem analysis: Algorithm: Input:

  1. Shift

  1. Enter Basic_salary, Shift

  2. Basic_salary

  2. Bonus equals to RM 50

  Process: Bonus equals RM 50

  3. Check workers Shift If Shift equals to 3:

  3.1 If Shift equals to 3 Salary equals Bonus plus Basic_salary

  Salary= Basic_salary + Bonus

  Output: Salary

  4. Display Salary

  Pseudocode: Flowchart:

  START

  START

  Input Shift, Basic_salary Bonus = 50 If (Shift ==3)

  Input Shift, Basic_salary

  Salary = Basic_salary + Bonus End if

  Bonus = 50

  Output Salary END

  True If Shift = 3 Salary = Basic_salary + Bonus False

  Output Salary

  Selection 2: IF…….ELSE

  • • A selection of control structure is used to

    select between two options

  Rules: Pseudocode:

  If (condition)

  If (condition)

  True statement

  True statement

  Else

  Else

  False statement

  False statement

  Endif

  Endif

  Flowchart:

  

START

False True

Conditio

n

  Statement 2 Statement 1

  

END

  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: Algorithm:

  1. Enter Grade Input: Grade Process:

  2. Check Grade If Grade is equal to 1

  2.1 If Grade = 1 Output “Qualified to further study”.

  2.1.1 Output “Qualified to If not further study”

  2.2 If not Output “Not qualified to further study”.

  Flowchart: Pseudocode:

  START START Input Grade If (Grade==1)

  Input Grade Output “Qualified to further study” Else Output “Not qualified to further study” Endif

  False True If END

  Grade == 1 Output “Not Output qualified to further “Qualified to study” further study”

  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 Result = num2 – num1

  Pseudocode:

  START

  Flowchart:

  Input num1, num2

  START

  If num1 > num2 Result = num1 – num2

  Input num1, num2

  Else Result = num2 – num1 Endif

  False True

  Output Result

  If num1 >

  END

  num2 Result = num2 – num1 Result = num1 – num2 Output Result

  Selection 3: NESTED IF

  • There are 3 types:

1. Type 1:

  If (condition1) If (condition2)

  If (condition3) True statement Endif

  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 Statement that will be executed if condition1 is false Endif

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 End if

  

Example 1 – NESTED IF

Problem:

  • • To determine whether a candidate is qualified or not

    to get a scholarship based on his / her study years,

    guardian’s salary and student CGPA. If the study

    year is more than 1, student’s CGPA is not less than

    3.00 and guardian’s salary is below than RM500,

    student will be considered for a scholarship.

  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

  2.1.1.1 If salary <= RM500 Output “Your application is under consideration”

  Flowch art: START Input Year, CGPA, Salary

  False True If Year > 1 False True If CGPA

  >= 3.00 Output “Your False True

  If Salary application is under <= 500 consideration”

  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 END

  

Example 2 – NESTED IF

Problem:

  • • To determine whether a candidate is qualified or not

    to get a scholarship based on his / her study years,

    guardian’s salary and student CGPA. If the study

    year is more than 1, student’s CGPA is not less than

    3.00 and guardian’s salary is below than RM500,

    student will be considered for a scholarship. If the

    student is not qualified the message “Not success”

    will be displayed.

  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”

  Output: Student status

  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 Output “Not success”

  Flowchart: False START

  True Input Year, CGPA, Salary Output “Your application is under consideration” If Year &gt; 1

  If CGPA &gt;= 3.00 If Salary &lt;= 500

  False False True True Output “Not success” Output “Not success” Output “Not success”

  Pseudocode: Input CGPA, Salary, Year If (year &gt; 1)

  If (CGPA &gt;= 3.00) If (salary &lt;= RM500) Output “Your application is under consideration” Else Output ”Not success” Endif

  Else Output ”Not success” Endif Else Output ”Not success”

  GPA Status 3.50-4.00 Dean List 2.00-3.49 Pass 1.80-1.99 Conditional Pass 0.00-1.79 Fail

  

Example 3 – NESTED IF

Problem:

  • Education status is determined based on the GPA achievement under the following scheme:

  Problem analysis: Input: GPA Process:

  1. If (GPA &lt; 0.00 AND GPA &gt; 4.00) Output “Invalid data”

  2. If not

  2.1 If (GPA &gt;=3.5 AND GPA &lt;= 4.00) Output “Dean List”

  2.2 If not

  2.2.1 If (GPA &gt;= 2.00 AND GPA &lt; 3.50) Output “Pass”

  2.2.2 If not

  2.2.2.1 If (GPA &gt;= 1.80 AND GPA&lt; 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 &lt; 0.00 AND GPA &gt; 4.00) Output “Invalid data”

  2.2 If not

  2.2.1 If (GPA &gt;=3.50 AND GPA &lt;= 4.00) Output “Dean List”

  2.2.2 If not

  2.2.2.1 If (GPA &gt;= 2.00 AND GPA &lt; 3.50) Output “Pass”

  2.2.2.2 If not

  2.2.2.2.1 If (GPA &gt;= 1.80 AND GPA &lt; 2.00) Output “Conditional Pass”

  2.2.2.2.2 If not Output “Fail”

  3. Print status oFlowchart: False START

  False True Input GPA False True True

  Output “Invalid data” Output “Dean List” If GPA &lt; 0.00 &amp;&amp;

  GPA &gt; 4.00 If GPA &gt;= 3.50 &amp;&amp; GPA &lt;= 4.00 If GPA &gt;= 2.00 &amp;&amp;

  GPA &lt; 3.50 If GPA &gt;= 1.80 &amp;&amp; GPA &lt; 2.00 Output

  “Conditional Pass” Output “Fail” Output “Pass”

  False True Flowchart:

  Pseudocode:

  START Input GPA

  If ((GPA &lt; 0.00) AND (GPA &gt; 4.00)) Output "Invalid Data"

  Else If ((GPA &gt;= 3.50) AND (GPA &lt;= 4.00))

  Output "Dean List" Else

  If ((GPA &gt;=2.00) AND (GPA &lt; 3.50)) Output "Pass"

  Else If ((GPA &gt;= 1.80) AND (GPA &lt; 2.00))

  Output "Conditional Pass” Else

  Output "Fail" Endif

  Endif

  Symbol Pseudo code Symbol C Language Example Result Description AND &amp;&amp; (1 &gt; 3) &amp;&amp; (10 &lt; 20)

  If ((a &lt; b) &amp;&amp; (c &gt; d)) printf(“Print a and c”); FALSE Both sides of the condition must be true

  OR || (1 &gt; 3) || (10 &lt; 20) If ((sales &gt; 5000) || (hoursworked&gt; 81)) bonus = 500; else bonus = 0;

  TRUE Either one of the condition must be true

  

NOT ! ! (10 &lt; 20) FALSE Change the operation either

from true to false or vise versa

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

  3. Do…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,

  False START Initialize

  Test conditio n A END

  True False START

  Initialize Statement A Test condition A

  END True

  Format for the For and While loops:Format for Do …… while loop:

  Example of loops usage: Problem:

  • Prepare the problem analysis, algorithm, flowchart and pseudocode for the average of 5 numbers. Data will be entered by user.

  Problem analysis:

Algorithm:

  1. Initialize Counter=0; Average = 0; Total = 0

  2. Input number

  • Input: 5 numbers

  3. Add Total using formula:

  • Process:

  Total = Total + number The process of adding

  4. Add Counter using formula: numbers will repeat until the Counter = Counter + 1 condition to exit the loop is met.

  5. Compare whether Counter is greater than 5 If yes , go to step 6

  • Output: Average of 5 numbers If not, go to step 2

  6. Calculate Average of numbers using formula; Average = Total/5

  7. Display Average

  Pseudocode: For loop While loop Do…while loop

  START no = 0 Total = 0 Avg = 0

  For (no=0; no&lt;=5; no++){

  Input Num Total = Total + Num }Endfor Avg = Total/5 Output Avg END START no = 0 Total = 0 Avg = 0

  While (no &lt;= 5){

  Input Num Total = Total + Num no = no + 1 }Endwhile Avg = Total/5 Output Avg END START no = 0 Total = 0 Avg = 0

  Do {

  Input Num Total = Total + Num no = no + 1 } While (no &lt;= 5) Avg = Total/5 Output Avg END oFlowchart for For and While loops:

  

False

True START no = 0

  Total = 0 For / While no &lt;= 5

  Input Num Total = Total + Num Avg = Total/5 Output Avg

  END Avg = 0

  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

  False True START no = 0 Total= 0 no &lt;= 5

  Input Num Total = Total + Num no = no + 1 Avg = Total/5

  Output Avg Avg = 0

  oFlowchart for Do……while loop:

  Example

  • To compute salary of 20 employees

  • * 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

  8. Back to step 2

  Flowchart for For and While loops: False True

  START no = 0 For / While no &lt;= 20 Input Basic_salary, Claim, O_time

  Salary = Basic_salary + Claim + O_time – EPF EPF = Basic_salary * 0.09 Output Salary

  END Salary = 0

  Pseudocode for For loop: START no = 0 Salary = 0 For (no = 0, no &lt;=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 &lt;=20 Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Gaji = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 Endwhile END

  • * 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 If not, go to step 2

  8. Display Salary

  Flowchart for Do…..while loop: START no = 0

  Salary = 0 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: START no = 0 Salary = 0 Do { Input Basic_salary, Claim, O_time

  EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1