SELECTION

Selection is used in a computer program or algorithm to determine which particular step or set of steps is to be executed. This is also referred to as a ‘decision’. A selection statement can be used to choose a specific path dependent on a condition. There are two types of selection: binary selection (two possible pathways) and multi-way selection (many possible pathways).

Binary Selection

In binary selection, if the condition is met then one path is taken, otherwise the second possible path is followed. In each of the examples below, the first case described requires a process to be completed only if the condition is true. The process is ignored if the condition is false. In the second case, there is an alternative process if the condition is false.

Pseudocode example:

IF Raining THEN
Take umbrella
ENDIF

or

IF Raining THEN
Take umbrella
ELSE
Put on suncream
ENDIF

Flowchart example:

Multi-way selection

Multi-way selection allows for any number of possible choices, or cases. The path taken is determined by the evaluation of the expression. Multi-way selection is often referred to as a case structure.

REPETITION

Repetition allows for a portion of an algorithm or computer program to be executed any number of times depednet on some condition being met. An occurrance of repetition is usually known as a loop.

House Foldable Lou Socks Slippers Indoor Shoes Travel Medium Slipper Yoga amp; Best Flexible Women's Nufoot Paisley Exercise Slippers Betsy Dance Flats Black An essential feature of repetition is that each loop has a termination condition to stop the repetition, or the obvious outcome is that the loop never completes execution. This is known as an infinite loop and is obviously undesirable. The termination condition can be checked or tested at the beginning or end of the loop, and is known as a pre-test or post-test, respectively. Following is a description of each of these types of loop.

Repetition pre-test loop

A pre-tested loop is so named because the condition has to be met at the very beginning of the loop or the body of the loop is not executed. This construct is often called a guarded loop. The body of the loop is executed repeatedly while the termination condition is true.

Pseudocode example:

WHILE condition is true
processes
ENDWHILE

Flowchart example:

Repetition post-test loop

A post-tested loop executes the body of the loop before testing the termination condition. This construct is often referred to as an unguarded loop. The body of the loop is repeatedly executed until the termination condition is true. An important difference between a pre-test and post-test loop is that the statements of a post-test loop are executed at least once, even if the condition is originally true, whereas the body of the pre-test loop may never be executed if the termination condition is originally true. A close look at the representations of the two loop types makes this point apparent.

Pseudocode example:

REPEAT
process
UNTIL condition is true

Flowchart example:

MINI TASK

Discuss where a procedure to "beat egg whites until fluffy" would fit in this kind of loop. Submit below:

FOR NEXT or counted loop

Counted loops or FOR NEXT loops can be regarded as special cases of repetition and, depending on the language in which they are implemented, are implemented as either pre-test or post-test repetitions.

To demonstrate a FOR NEXT loop as a flowchart it must have a purpose, our example will print the 12 times table.

Pseudocode example:

FOR i = 1 to 12 STEP 1
Display "12 x " i " = " (12 * i)
NEXT i

Flowchart example:

SUBPROGRAMS

Subprograms, as the name implies, are complete part-programs that are used from within the main program section. They use refinement develop solutions to problems that are easy to follow. Sections of the solution are developed and presented in understandable chunks, and because of this, subprograms are particularly useful when using the top-down method of solution development.

When using subprograms it is important that the solution expression indicates where the main program branches to a subprogram. It is equally important to indicate exactly where the subprogram begins. In pseudocode, the statement in the main program that is expanded in a subprogram is underlined (in the example below the word "read" should be underlined, but this system does not allow underlining) to indicate that further explanation follows. The expanded subprogram section should be identified by using the keyword BEGIN followed by the underlined title used in the main program. The end of the subprogram is marked by the keyword END and the underlined title used in the main program.

When using flowcharts, a subprogram is shown by an additional vertical line on each side of the process box. This indicates that the subprogram is expanded elsewhere. The start and end of the subprogram flowchart uses the name of the subprogram in the termination boxes.

In many cases a subprogram can be written to do the same task at two or more points in an algorithm. Each time the subprogram is called, it may operate on different data. To indicate the data to be processed, one or more parameters are used. The parameters allow the author to write a general algorithm using the formal parameters. When the subprogram is executed, the algorithm carries out its task on the actual parameters given at the call. The parameters to be used by a subprogram are provided as a list in parentheses after the name of the subprogram. There is no need to include them at the end of the algorithm.

Pseudocode

BEGIN MAINPROGRAM
read (name)
read (address)
END MAINPROGRAM
BEGIN read (array)
Set pointer to first position
Get a character
WHILE there is still more data AND there is room in the array
Store data in the array at the position given by the pointer
Increment the pointer
Get next character
ENDWHILE
END read