Selection and Repetition Control Structures
Selection Control Structures
When a programmer is writing a program that needs to choose between two or more actions dependent upon whether the condition is true or false, a selection control structure is used. Relational operators such as less than (<), greater than (>) and equal to (< >, =) are used to express the conditions. The addition of selection control structures to a program gives a program more structure and the programmer has more flexibility in program construction.
There are four kinds of selection control structures:
1. Simple selection
2. Simple selection with null force branch
3. Combined selection
4. Nested selection
Suppose a programmer wants to process the results of a survey on how many people in San ...view middle of the document...
They are also known as iteration control structures.
There are three repetition control structures and where the decision to repeat a statement is placed determines which one of the control structures is used:
1. Leading decision loop or DOWHILE loop is used at the beginning of the loop.
2. Trailing decision loop or REPEAT…UNTIL loop is used at the end of the loop.
3. Counted repetition loops, also called DO or FOR loops are used when the number of loop iterations to repeat is known in advance.
The following is an example of a counted repetition loop to convert miles per hour to kilometers per hour. The following conversion is based on Robertson (n.d.):
1. DO mph_count = 5 to 13
2. prompt operator for mph
3. Get mph
4. compute kph = (mph * 1.6)
5. Display kph
6. Display ‘All speeds processed’ to the screen
The DO loop controls the repetition by:
* initializing the mph to 5
* increments the mph by 5 for each pass through the loop
* tests mph at the beginning of each pass to ensure it is within the 5 to 13 range
* automatically terminates the loop once the mph exceeds 13
Robertson, L. A. (n.d.). Simple program design: A step by step approach. [University of Phoenix
Custom Edition e-Text]. Mason, Ohio: Cengage Learning. Retrieved from Axia College, PRG210 - Fundamentals of Programming website.