27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

pythonhtp1_03.fm Page 73 Saturday, December 8, 2001 9:34 AM<br />

Chapter 3 Control Structures 73<br />

These single-entry/single-exit control structures make it easy to build programs. The<br />

control structures are attached to one another by connecting the exit point of one control<br />

structure to the entry point of the next. This is similar to the way a child stacks building<br />

blocks; hence, the term control-structure stacking. Control-structure nesting also connects<br />

control structures; we discuss this technique later in the chapter.<br />

Software Engineering Observation 3.2<br />

Any <strong>Python</strong> program can be constructed from six different types of control structures (sequence,<br />

if, if/else, if/elif/else, while and for) combined in two ways (controlstructure<br />

stacking and control-structure nesting). 3.2<br />

3.5 if Selection Structure<br />

Selection structures choose among alternative courses of action. For example, suppose that<br />

the passing grade on an examination is 60. Then the pseudocode statement<br />

If student’s grade is greater than or equal to 60<br />

Print “Passed”<br />

determines whether the condition “student’s grade is greater than or equal to 60” is true or<br />

false. If the condition is true, then “Passed” is printed, and the next pseudocode statement<br />

in order is “performed.” (Remember that pseudocode is not a real programming language.)<br />

If the condition is false, the print statement is ignored, and the next pseudocode statement<br />

is performed. Note that the second line of this selection structure is indented. Such indentation<br />

is optional (for pseudocode), but it is highly recommended because indentation emphasizes<br />

the inherent hierarchy of structured programs. When we convert pseudocode into<br />

<strong>Python</strong> code, indentation is required.<br />

The preceding pseudocode if statement may be written in <strong>Python</strong> as<br />

if grade >= 60:<br />

print "Passed"<br />

Notice that the <strong>Python</strong> code corresponds closely to the pseudocode. This similarity is the<br />

reason that pseudocode is a useful program development tool. The statement in the body of<br />

the if structure outputs the character string "Passed".<br />

The flowchart of Fig. 3.3 illustrates the single-selection if structure and the diamond<br />

symbol. The decision symbol contains an expression, such as a condition, that can be either<br />

true or false. The diamond has two flowlines emerging from it: One indicates the direction<br />

to follow when the expression in the symbol is true; the other indicates the direction to<br />

follow when the expression is false. We learned, in Chapter 2, Introduction to <strong>Python</strong> <strong>Program</strong>ming,<br />

that decisions can be based on conditions containing relational or equality operators.<br />

Actually, a decision can be based on any expression. For instance, if an expression<br />

evaluates to zero, it is treated as false, and if an expression evaluates to nonzero, it is treated<br />

as true.<br />

Note that the if structure is a single-entry/single-exit structure. We will soon learn<br />

that the flowcharts for the remaining control structures also contain (besides small circle<br />

symbols and flowlines) rectangle symbols that indicate the actions to be performed and diamond<br />

symbols that indicate decisions to be made. This type of flowchart emphasizes the<br />

action/decision model of programming.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!