If Selection Structure

Selection structures choose among alternative courses of action. For example, suppose that the passing grade on an examination is 60. Then the pseudocode statement

If student's grade is greater than or equal to 60 Print"Passed"

determines whether the condition "student's grade is greater than or equal to 60" is true or false. If the condition is true, then "Passed" is printed, and the next pseudocode statement in order is "performed." (Remember that pseudocode is not a real programming language.) If the condition is false, the print statement is ignored, and the next pseudocode statement is performed. Note that the second line of this selection structure is indented. Such indentation is optional (for pseudocode), but it is highly recommended because indentation emphasizes the inherent hierarchy of structured programs. When we convert pseudocode into Python code, indentation is required.

The preceding pseudocode if statement may be written in Python as if grade >= 50: print "Passed"

Notice that the Python code corresponds closely to the pseudocode. This similarity is the reason that pseudocode is a useful program development tool. The statement in the body of the if structure outputs the character string "Passed".

The flowchart of Fig. 3.3 illustrates the single-selection if structure and the diamond symbol. The decision symbol contains an expression, such as a condition, that can be either true or false. The diamond has two flowlines emerging from it: One indicates the direction to follow when the expression in the symbol is true; the other indicates the direction to follow when the expression is false. We learned, in Chapter 2, Introduction to Python Programming, that decisions can be based on conditions containing relational or equality operators. Actually, a decision can be based on any expression. For instance, if an expression evaluates to zero, it is treated as false, and if an expression evaluates to nonzero, it is treated as true.

Note that the if structure is a single-entry/single-exit structure. We will soon learn that the flowcharts for the remaining control structures also contain (besides small circle symbols and flowlines) rectangle symbols that indicate the actions to be performed and diamond symbols that indicate decisions to be made. This type of flowchart emphasizes the action/decision model of programming.

74 Control Structures

Selection Structure Flow Chart

Fig. 3.3 if single-selection structure flowchart.

We can envision six bins, each containing control structures of one of the six types. These control structures are empty—nothing is written in the rectangles or in the diamonds. The programmer's task, then, is assembling a program from as many of each type of control structure as the algorithm demands, combining those control structures in only two possible ways (stacking or nesting), then filling in the actions and decisions in a manner appropriate for the algorithm. We will discuss the variety of ways in which actions and decisions may be written.

Was this article helpful?

0 0


  • michael
    How to create selection structure using python?
    8 years ago

Post a comment