Selection and Repetition Control Structure, Pseudocode Algorithms Using Sequence, Selection and Repetition
Selection and Repetition Control Structure, Pseudocode Algorithms Using Sequence, Selection and Repetition
Session 05
(Tutorial)
Acknowledgement
2. Expected result
first data set second data set
char_1 b a
char_2 g s
char_3 k z
Example (cont’d)
3. Desk check table
Line numbers have been used to identify each
statement within the program. Note that when
desk checking the logic each IF statement is
treated as a single statement .
statement
number char_1 char_2 char_3 temp
first pass
1,2 k b g
3 b k k
4 g k k
5
6 output output output
second pass
1,2 z s a
3 s z z
4 a z z
5 a s s
6 output output output
The Case Structure
The case structure
• The case control structure in pseudocode is another
way of expressing a linear nested IF statement
• Example :
CASE OF tax_code
0 : sales_tax = 0
1 : sales_tax = purch_amt * 0.03
2 : sales_tax = purch_amt * 0.05
3 : sales_tax = purch_amt * 0.07
ENDCASE
Repetition using the dowhile
Structure
Repetition
• There are three different ways in which a set of
instructions can be repeated, and each way is
determine 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)
DOWHILE
• The DOWHILE construct is a leading decision loop which means
the condition is tested before any statements are executed.
• DOWHILE Format :
DOWHILE condition p is true
statement block
ENDDO
• The following processing takes place :
1. The logical condition p is tested
2. If condition p is found to be true, the statements within the
statement block are executed once. The delimiter ENDDO then
triggers a return of control to the retesting of condition p
3. If condition p is still true, the statements are executed again, and
so the repetition process continues until the condition is found to
be false
4. If condition p is found to be false, control passes to the next
statement after the deliminiter ENDDO and no further processing
takes place within the loop
DOWHILE
• Consideration before designing a DOWHILE loop :
– The testing of condition is at the beginning of
the loop. Meaning that it may be necessary to
perform some initial processing before it can be
tested
– The only way to terminate the loop is to render
DOWHILE condition false
DOWHILE
• Using DOWHILE to repeat a set of instructions an unknown
number of times
– When a trailer record or sentinel exists
• The sentinel is a special record or value placed at the
end of valid data to signify the end of the data
– When a trailer record or sentinel does not exists
• When there is no trailer record or sentinel to signify
the end of the data, it is necessary to check for an end-
of-file marker (EOF). The following expressions can be
used :
– DOWHILE more data
– DOWHILE more records
– DOWHILE records exist
– DOWHILE NOT EOF
Repetition using the Repeat . . . Until
Structure
REPEAT . . . UNTIL
• a REPEAT . . . UNTIL structure test the
condition at the end of the loop. Meaning
that the statements within the loop will be
executed once before the condition is tested.
• The format
REPEAT
statement
statement
….
UNTIL
Counted Repetition
Counted repetition
• Counted repetition occurs when the exact number of loop
iteration is known in advance
• The execution of the loop is controlled by a loop index, as
follow
DO loop_index = initial_value to final_value
statement block
ENDDO
• The DO loop does more than repeat the statement block. It will :
– Initialise the loop_index to the required initial_value
– Increment the loop_index by 1 for each pass through the loop
– test the value of loop_index at the beginning of each loop to ensure
that it is within the stated range of values
– Terminate the loop when the loop_index has exceeded the specified
final_value
Solution Algorithm
Solution Algorithms
• Each solution will be consist of :
1. Defining the problem
2. The control structures required
3. The solution algorithm
4. Desk checking
Flowchart
• Flowchart is an alternative method of
representing algorithms
Terminal symbol
Process symbol
Decision Symbol
Flowlines