7.2 Selection Structure


7.2 Selection Structure

As mentioned earlier, the selection design structure is also called alternation, because alternate paths are considered based on a condition. This design structure is easier to understand in a flowchart. Figure 7.1 shows two possible paths for the execution flow. The condition is examined (or evaluated), and a decision is made to select one of the paths. If the condition is true, then the left path is taken and the instructions on this path (Block1) are executed. If the condition is not true, then the other path is taken and the instructions on this path (Block2) are executed. Thus, the selection structure provides the algorithm capability for decision-making.

click to expand
Figure 7.1: Flowchart segment general selection structure.

7.2.1 Pseudo-Code and the IF Statement

In the pseudo-code notation, the selection structure is written with an if statement, also called an if-then-else statement. This statement includes several keywords; recall that these are reserved words because the programmer cannot use any of these words for other purposes. The keywords are: if, then, else, and endif.

The informal pseudo-code for the if statement that corresponds to the general selection structure illustrated in Figure 7.1 is:

        if condition is true           then               perform instructions in Block1           else               perform instructions in Block2        endif 

In KJP, the general structure of the if statement is:

        if  condition            then                statements in Block1            else                statements in Block2         endif 

Note that the keywords used in the pseudo-code are written in bold to clarify their usage. The if statement is considered a compound statement.

Note

All the instructions in Block1 are said to be in the then section of the if statement. In a similar manner, all the instructions in Block2 are said to be in the else section of the if statement.

When the if statement executes, the condition is evaluated and only one of the two alternatives will be carried out: the one with the statements in Block1 or the one with the statements in Block2.

7.2.2 Conditions and Operators

The condition consists of an expression that evaluates to a truth-value, true or false. These types of expressions are also known as Boolean expressions. A simple Boolean expression compares the value of two data items.

A simple Boolean expression is composed of two data items and a relational operator to compare the two data items. There are six relational operators:

  • Equal, ==

  • Not equal, !=

  • Less than, <

  • Less or equal to, <=

  • Greater than, >

  • Greater or equal to, >=

Examples of simple conditions that can be expressed with the relational operators in KJP are:

                 x >= y                 time  ! =  start_t 

Note

Instead of applying the mathematical symbols shown previously for the relational operators, additional keywords can be used for the operators in KJP. For example:

                 x greater or equal to y                 time not equal start_t                 a greater than b 

7.2.3 A Simple Example of Selection

As a more concrete example, consider a portion of an algorithm in which the decision whether variable j should be incremented or decremented depends on the condition: x > 0. Figure 7.2 illustrates the flowchart portion of the algorithm that includes this selection structure for this simple example.

click to expand
Figure 7.2: Example of selection structure.

For this example, the portion of the algorithm written in pseudo-code is:

                 if x > 0 then                      increment j                    else                      decrement j                 endif 

In the selection statement, only one of the two paths will be taken; this means that in the example, the statement increment j or the statement decrement j will be executed. This depends on the evaluation of the condition x > 0.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net