You are on page 1of 12


Algorithm: Repetition Control Structures

Objectives • In this chapter you will be able to: • Develop algorithms that use the DOWHILE control structures • Introduce a pseudocode structure for counted repetition loops • Develop algorithms using variations of the repetition construct PCPOOP-L5-AlgoRepCtrlStruct 2 .

and each way is determined by where the decision to repeat is placed: – at the beginning of the loop (leading decision loop) – at the end of the loop (trailing decision loop) – a counted number of times (counted loop) PCPOOP-L5-AlgoRepCtrlStruct 3 .Repetition Using the DOWHILE Structure • There are three different ways that a set of instructions can be repeated.

the testing of the condition is at the beginning of the loop • Second. the condition is tested before any statements are executed • There are two important considerations of which you must be aware before designing a DOWHILE loop • First.Leading Decision Loop • The DOWHILE construct is a leading decision loop – that is. the only way to terminate the loop is to render the DOWHILE condition false PCPOOP-L5-AlgoRepCtrlStruct 4 .

a counter can be used in pseudocode. which is initialized before the DOWHILE statement and incremented just before the ENDDO statement PCPOOP-L5-AlgoRepCtrlStruct 5 .Using DOWHILE to Repeat a Set of Instructions a Known Number of Times • When a set of instructions is repeated a specific number of times.

so another way of controlling the repetition must be used A sentinel value is used to signify the end of a loop This sentinel is a special value set by the programmer and user will be guided by the prompt message. This will cause loop to terminate. User will enter the sentinel value when there is no more data to be entered. PCPOOP-L5-AlgoRepCtrlStruct • • • 6 . you cannot use a counter.Using DOWHILE to Repeat a Set of Instructions an Unknown Number of Times • When there are an unknown number of items to process.

Initialize to the required initial_value 2. Increment by 1 for each pass through the loop 3. Test the value at the beginning of each loop to ensure that it is within the stated range of values  If the condition is TRUE  Execute the loop body while the loop counter is still within the specified final value  If the condition is FALSE  Terminate the loop when the loop counter has exceeded the specified final value PCPOOP-L5-AlgoRepCtrlStruct 7 .Loop Counter • A loop counter has to do the followings: 1.

After the task is finished. display “Done !” PCPOOP-L5-AlgoRepCtrlStruct 8 .Example 1 : Problem Display “Hello” ten times.

Example 1 : Algorithm Star t counter  1 Display_Hello counter  1 DOWHILE counter <= 10 Display “HELLO ” counter  counter + 1 ENDDO Display “Done !” End Display “Done !” Counter<=10 ? True False Display “HELLO ” counter  counter +1 Sto PCPOOP-L5-AlgoRepCtrlStruct p 9 .

Display “You are very tall” if the student’s height is greater than 1.Example 2 : Problem Get the height of 30 students. display “Ordinary height” PCPOOP-L5-AlgoRepCtrlStruct 10 . Otherwise.7meters.

7 ? False Display “Ordinary” Display “You are very tall” student  student + 1 ENDDO End PCPOOP-L5-AlgoRepCtrlStruct student  student +1 Stop 11 .Example 2 : Algorithm Start Height_Category student  1 False student  1 DOWHILE student <= 30 Prompt for height Get height If height > 1.7 Then Display “You are very tall” Else Display “Ordinary” End If student<=30 ? True Prompt for height Get height True If height > 1.

Tutorial • What are the keywords used in pseudocode for Repetition Control Structure ? • List the THREE tasks of a loop counter. • What happens if the boolean expression of a loop results TRUE? • What happens if the boolean expression of a loop results FALSE? PCPOOP-L5-AlgoRepCtrlStruct 12 .