3.4 Control Statements

  • Usually, statements in a program execute in the order in which they’re written.
    • Called sequential execution.
  • Various Python statements enable you to specify that the next statement to execute may be other than the next one in sequence.
    • Called transfer of control and is achieved with Python control statements.

Forms of Control

  • All programs can be written using three forms of control
    • Sequential execution
    • The selection statement
    • The iteration statement.
  • Python statements execute one after the other “in sequence,” unless directed otherwise.

Flowcharts (1 of 3)

  • Graphical representation of an algorithm or a part of one.
  • Drawn using rectangles, diamonds, rounded rectangles and small circles connected by arrows called flowlines.
  • Clearly show how forms of control operate.

Flowcharts (2 of 3)

  • Flowchart segment showing sequential execution: Flowchart segment showing sequential execution of two statements

Flowcharts (3 of 3)

  • Rectangle (or action) symbol indicates any action
  • Flowlines show the order in which the actions execute.
  • In a flowchart for a complete algorithm, the first symbol is a rounded rectangle containing the word “Begin.” The last symbol is a rounded rectangle containing the word “End.”
  • In a flowchart for only a part of an algorithm, use small circles called connector symbols.

Selection Statements

  • Three selection statements that execute code based on a condition—an expression that evaluates to either True or False:
    • if performs an action if a condition is True or skips the action if the condition is False.
    • ifelse statement performs an action if a condition is True or performs a different action if the condition is False.
    • ifelifelse statement performs one of many different actions, depending on the truth or falsity of several conditions.
  • Anywhere a single action can be placed, a group of actions can be placed.

Iteration Statements

  • Two iteration statements
    • while repeats an action (or a group of actions) as long as a condition remains True.
    • for statement repeats an action (or a group of actions) for every item in a sequence of items.

Keywords

  • Notice that a few (shown in bold) start with an uppercase letter.
         
and as assert async await
break class continue def del
elif else except False finally
for from global if import
in is lambda None nonlocal
not or pass raise return
True try while with yield

Control Statements Summary

  • You form each Python program by combining as many control statements of each type as you need for the algorithm the program implements.
  • With Single-entry/single-exit (one way in/one way out) control statements, the exit point of one connects to the entry point of the next.
    • Similar to the way a child stacks building blocks—hence, the term control-statement stacking.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 3 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.