Está en la página 1de 8

Selection Control Structures

Selection and Repetition Control Structures By: Sibbonai Williams PRG/210 3/5/10

Selection Control Structures

Selection Control Structures A control structure is transfer of program control is based upon a decision, the result of which is a truth statement (returning a Boolean true or false). You create an expression and then test whether its result is true (www.microsoft.com). There are two types of control structures those two are selection control and repetition control structures. Selection control structure specifies alternate courses of program flow, creating a junction in your program , (www.microsoft.com). Some examples of a selection control structures are a fork in the road, a bypass, or a alternative route. These structures represent how a computer makes decisions. Selection control structures are used in psuedocodes to demonstrate a choice between two or more actions, depending on whether a certain decision is true or false. There are four selection structures that are used. They are:

the single-selection structure (IF) simple selection with a null false branch (null ELSE statement) combined selection (combined IF statement) nested selection (nested IF statement)

Selection Control Structures

The condition in the IF statement is based on comparing two items and it is usually shown with one of the following symbols:

< less than > greater than = equal to < = less than or equal to > = greater than or equal to < > not equal to

Simple Selection (if statement) occurs when a choice is made between two alternative paths, depending on if the condition is true or false. The structure is represented in the psuedocodes by using the words IF, THEN, ELSE AND ENDIF. An example of this is :

IF card balance is <150.00, then service charge is = 2.50 , ELSE service charge is = 5.00 , ENDIF

Simple selection with a null false branch (null ELSE statement) is just more of a variation of the simple if structure. It is only used on a task when that particular task is TRUE. If the condition is false then no processing will ensue and the IF statement will not be used it will be bypassed.

Selection Control Structures

For example:

IF employees schedule = full time, THEN add 1 to full time employee list

ENDIF

The next selection structure is the combined structure. A combined if statement just adds more than one condition and is connected with the keywords AND or OR. If AND is used to combined two tasks both have to be true for the total condition to be true. An example is:

IF employee schedule = full time, AND employee is a = high school student, THEN add 1 to high school student full time count

The last selection control structure is a nested selection this is when IF is used more than once within an IF statement.

IF student is under 18 = under age THEN parent permission is required, IF student is over 18 THEN parent permission is not required ENDIF.

Repetition Control Structures The repetition control structure is another type of control structure. The repetition structure specifies the repetition of an action while some condition remains true. When the conditions of the control statement have been met (usually after some specific number of iterations), control passes to the next statement beyond the repetition structure, (www.microsoft.com).

Selection Control Structures

There are three types of repetition control structures. They are:

Leading decision loop Trailing decision loop Counted repetition

A leading decision loop has incorporation of the DOWHILE structure. This condition must be tested before any statement is executed. In this loop certain actions take place. These are:

The logical P is tested. If P is true the statements in the block are executed once, ENDDO then prompts a control retesting of condition p.

If the condition is still true then the statement is performed again , so the repetition continues until the condition is found false.

If the condition p is false, control moves to the next statement after ENDDO an no further processing will take place in the loop.

Example of the leading decision loop is:

Input

Processing

Output

English

enter into converter to get Spanish

Spanish statement

Spanish

enter to converter to get English

English statement

Selection Control Structures

Display Spanish statement

Display English statement

A trailing decision loop is another type of repetition structure. The REPEAT.UNTIL structure is similar to the DOWHILE structures, in these statements are repeated with a specified condition. The DOWHILE structure tests the condition at the beginning of the loop, a REPEAT...UNTIL structure tests the condition at the end of the loop. This means that the statements within the loop will be executed, (Robertson, Lesley Ann). For example:

REPEAT, statement, statement --------UNTIL condition is TRUE.

The last repetition statement is counted repetition. Counted repetition is when the number of loop variations is known in advance. The loop is controlled by a loop index, and instead of using DOWHILE, or REPEAT.....UNTIL, DO is used. An example of this repetition statement is : DO loop_ index = initial value to final value , statement block, ENDDO.

As you can see programming has an array of choices to add you your program. You may get the same result but with a different path to get there.

Selection Control Structures

References

Microsoft Library, http://msdn.microsoft.com/en-us/library/.aspx, 2010

Simple Program Design, A Step by Step Approach, Fifth Edition, copyright 2007 McGraw-Hill, a business unit of the McGraw-Hill Companies, Inc.

Selection Control Structures

También podría gustarte