0% found this document useful (0 votes)
410 views29 pages

Selection and Repetition Control Structure, Pseudocode Algorithms Using Sequence, Selection and Repetition

This document covers selection and repetition control structures in pseudocode algorithms. It discusses if/else statements, case structures, do-while loops, repeat-until loops, and counted repetition. Examples are provided to illustrate sorting characters with if statements and using control structures to repeat a block of code a known or unknown number of times. The learning objectives are to apply program development processes and design applications using program design methods. Flowcharts are also introduced as an alternative way to represent algorithms.

Uploaded by

Muhammad Rafif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
410 views29 pages

Selection and Repetition Control Structure, Pseudocode Algorithms Using Sequence, Selection and Repetition

This document covers selection and repetition control structures in pseudocode algorithms. It discusses if/else statements, case structures, do-while loops, repeat-until loops, and counted repetition. Examples are provided to illustrate sorting characters with if statements and using control structures to repeat a block of code a known or unknown number of times. The learning objectives are to apply program development processes and design applications using program design methods. Flowcharts are also introduced as an alternative way to represent algorithms.

Uploaded by

Muhammad Rafif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Course : Program Design Methods

Effective Period : July 2018

Selection and Repetition Control


Structure, Pseudocode Algorithms
Using Sequence, Selection and
Repetition

Session 05
(Tutorial)
Acknowledgement

These slides have been adapted from


Simple Program Design : A Step by step
approach., Lesley Anne Robertson, Fifth
Edition,
Course Technology. ISBN: 978-1-4239-0132-7.
Chapter 4 - 6
Learning Objectives
LO 2 : Apply the process of program developing
LO 3 : Design the application using program
design
method
Contents
• The Selection Control Structure
• Algorithm using Selection
• The Case Structure
• Repetition using the DO-WHILE Structure
• Repetition using the REPEAT ...UNTIL
Structure
• Counted Repetition
• Solution Algorithms
• Flowchart
The Selection Control Structure
The Selection Control Structure
• The selection control structure represents the
decision – making abilities of a computer
• The condition in the IF statement is based on
comparison of two items, and is usually expressed
with on of the following relational operators :
< less than
> greater than
= equal to
<= less than or equal to
>= greater than or equal to
<> not equal to
Variations of the Selection
Structure
1. Simple selection (Simple IF Statement)
2. Simple selection with null false branch
(null ELSE statement)
3. Combined selection (combined IF
statement)
4. Nested selection (nested IF statemen)
Algorithm using Selection
Algorithm using Selection
• Case: Design an algorithm that will prompt a
terminal operator for three characters, accept
those characters as input, sort them into
ascending sequence and output them to the
screen
• Solution:
A. Defining Diagram
Input Processing Output
char_1 Prompt for characters char_1
char_2 Accept three characters char_2
char_3 Sort three characters char_3
Output three characters
Example (cont’d)
B. Solution Algorithm
The solution algorithm requires a series of IF statements to sort the three characters into ascending
sequence
Read_three_characters
1 Prompt the operator for char_1, char_2, char_3
2 Get char_1, char_2, char_3
3 IF char_1>char_2 THEN
temp = char_1
char_1 = char_2
char_2 = temp
ENDIF
4 IF char_2>char_3 THEN
temp = char_2
char_2 = char_3
char_3 = temp
ENDIF
5 IF char_1>char_2 THEN
temp = char_1
char_1 = char_2
char_2 = temp
ENDIF
6 Output to the screen char_1, char_2, char_3
END
Example (cont’d)
C. Desk Checking
Two sets of valid characters will be used to check the
algorithm; the characters k ,b , and g as the first and z, s, and
a as the second
1. Input data
first data set second data set
char_1 k z
char_2 b s
char_3 g a

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

The terminal symbol indicates the starting or stopping point in the


logic.
Every flowchart should begin and end with a terminal symbol

Input / Output symbol

The input / output symbol represents an input or output process in


an algorithm, such as reading input or writing output

Process symbol

The process symbol represents any single process in an algorithm


such as assigning a value or performing a calculation. The flow of
control is sequential

Predefined process symbol

The predefined process symbol represents a module in an algorithm


– that is , a predefined process that has its own flowchart

Decision Symbol

The decision symbol represents a decision in the logic involving the


comparison of two values. Alternative paths are followed,
depending on whether the decision symbol is true or false

Flowlines

Flowlines connect various symbols in a flowchart and contain an


arrowhead only when the flow of control is not from top to bottom
or left to right
References
• Simple Program Design : A Step by step approach.,
Lesley Anne Robertson, Fifth Edition,
Course Technology. ISBN: 978-1-4239-0132-7.
• Selection structure,
http://www.thevbprogrammer.com/Ch05/05-03-
SelectionStructure.htm
• Getting Control,
http://aelinik.free.fr/c/ch10.htm/05-03-
SelectionStructure.htm
• Repetition Structure,
http://www.thevbprogrammer.com/Ch05/05-04-
RepetitionStructure.htm
Q&A

You might also like