Algorithm Designs Control Structures Stephen Boado 10IST2 There are 3 types of control structures: Sequence Selection Repetition
The most common form of control structure. Each step is carried out in order of their position and is only done once. In pseudocode, it always starts with ‘Begin’, then the steps are listed out and ends in ‘End’. In flowchart, it begins with a (terminator), then the steps are shown by (process) and ends with another terminator. Sequence Pseudocode BEGIN Get 2 numbers Store in num1 & num2 Calculate sum (num1 + num2) Output sum END Flowchart Begin Fill a kettle with water Boil the water in the kettle Put tea leaves in the pot Pour boiling water into the pot End
Selection Allows a choice to be made in an algorithm. Decides a particular answer from a set of variable answers and carries out the steps that proceeds it. In flowchart, the query/ dicussion / problem is represented by and the choices as a process. There are two types of selection control structures: 1) binary selection 2) case selection (also called multiple selection) . Binary selection is where there are two possible choices to choose. Binary uses IF ... ELSE ... ENDIF. If the condition is met (true) then one path is taken, otherwise (false) the other path is taken. Pseudocode IF the telephone rings THEN Answer the phone ELSE Continue reading ENDIF Flowchart Telephone rings? Continue reading Answer phone F T
Case selection is where there is more than one possible choices to choose when trying to solve the problem. Only one process can be carried out. Case selection uses CASE ... ENDCASE. The condition is checked and if the first choice is true then it is carried out. If the first choice is false, then the second will be checked. If it’s true then it is carried out. If no choice is found to be true then the otherwise choice will be carried out. Pseudocode CASEWHERE traffic light is Red: Stop vehicle Green: Drive through intersection OTHERWISE: Slow down and prepare to stop ENDCASE Flowchart Signal? Stop Drive Slow and stop Red Green Otherwise
Repetition Carries out a particular action over and over again until the condition is met. A loop is created to return the program to where the repetition has started for as long as it takes until the condition is met. There are two ways of testing to see if the end condition is met: 1) pre-test loops 2) post-test loops. In pre-test loops the condition is tested at the start. If the condition is false the first time, the processes will never carry out. Pre-test loops end when the condition Is false. Uses WHILE … ENDWHILE. Counted loops are special types of pre-test loops. They are used when a known number of repetitions will occur. Pre-test loops are also known as guarded loops because the loop is only operated when the condition is met. Pseudocode WHILE there is pressure on the mat Sound the bell ENDWHILE Flowchart Pressure on mat? Sound bell T F
Post-test loops tests the condition at the end of the loop. Uses REPEAT … UNTIL. The process in the loop is executed first whether it is true or false and will keep repeating until the condition is met. Post-test loops end when the condition is true but the loop always do the loop at least once, even if the end condition is originally true. Post-test loops are also called unguarded loops because no check is made before the algorithm begins the loop structure. Pseudocode REPEAT Take out one item UNTIL bag is empty Is bag empty? Flowchart Take out item T F

Algorithm Designs - Control Structures

  • 1.
    Algorithm Designs ControlStructures Stephen Boado 10IST2 There are 3 types of control structures: Sequence Selection Repetition
  • 2.
    The most commonform of control structure. Each step is carried out in order of their position and is only done once. In pseudocode, it always starts with ‘Begin’, then the steps are listed out and ends in ‘End’. In flowchart, it begins with a (terminator), then the steps are shown by (process) and ends with another terminator. Sequence Pseudocode BEGIN Get 2 numbers Store in num1 & num2 Calculate sum (num1 + num2) Output sum END Flowchart Begin Fill a kettle with water Boil the water in the kettle Put tea leaves in the pot Pour boiling water into the pot End
  • 3.
    Selection Allows achoice to be made in an algorithm. Decides a particular answer from a set of variable answers and carries out the steps that proceeds it. In flowchart, the query/ dicussion / problem is represented by and the choices as a process. There are two types of selection control structures: 1) binary selection 2) case selection (also called multiple selection) . Binary selection is where there are two possible choices to choose. Binary uses IF ... ELSE ... ENDIF. If the condition is met (true) then one path is taken, otherwise (false) the other path is taken. Pseudocode IF the telephone rings THEN Answer the phone ELSE Continue reading ENDIF Flowchart Telephone rings? Continue reading Answer phone F T
  • 4.
    Case selection iswhere there is more than one possible choices to choose when trying to solve the problem. Only one process can be carried out. Case selection uses CASE ... ENDCASE. The condition is checked and if the first choice is true then it is carried out. If the first choice is false, then the second will be checked. If it’s true then it is carried out. If no choice is found to be true then the otherwise choice will be carried out. Pseudocode CASEWHERE traffic light is Red: Stop vehicle Green: Drive through intersection OTHERWISE: Slow down and prepare to stop ENDCASE Flowchart Signal? Stop Drive Slow and stop Red Green Otherwise
  • 5.
    Repetition Carries outa particular action over and over again until the condition is met. A loop is created to return the program to where the repetition has started for as long as it takes until the condition is met. There are two ways of testing to see if the end condition is met: 1) pre-test loops 2) post-test loops. In pre-test loops the condition is tested at the start. If the condition is false the first time, the processes will never carry out. Pre-test loops end when the condition Is false. Uses WHILE … ENDWHILE. Counted loops are special types of pre-test loops. They are used when a known number of repetitions will occur. Pre-test loops are also known as guarded loops because the loop is only operated when the condition is met. Pseudocode WHILE there is pressure on the mat Sound the bell ENDWHILE Flowchart Pressure on mat? Sound bell T F
  • 6.
    Post-test loops teststhe condition at the end of the loop. Uses REPEAT … UNTIL. The process in the loop is executed first whether it is true or false and will keep repeating until the condition is met. Post-test loops end when the condition is true but the loop always do the loop at least once, even if the end condition is originally true. Post-test loops are also called unguarded loops because no check is made before the algorithm begins the loop structure. Pseudocode REPEAT Take out one item UNTIL bag is empty Is bag empty? Flowchart Take out item T F