Control Structures. The if Statement

Document Sample
scope of work template
							Control Structures.                                                 CS103



        Sometimes it is necessary to alter the sequential
                                                              Computer
        flow of control in a program.                          Programs

        This is achieved by control flow statements.           Simple Programs

                                                              Numbers
        Apart from the normal sequential flow, there are
                                                              Strings
        two principal nonsequential control structures:       Functions
        conditional flow and repetition flow.                   Graphics

                                                              Decisions

                                                              Loops

      statement-1

                             test?                  test?
      statement-2




                           statement              statement

      statement-n




The if Statement.                                                   CS103




                                                              Computer
         S YNTAX An if statement has the form                 Programs

                                                              Simple Programs
         if <condition>:
                                                              Numbers
             <block>
                                                              Strings
        where <condition> is an expression that
                                                              Functions
        evaluates to either True or False, and <block> is
                                                              Graphics
        a sequence of one or more statements, indented        Decisions
        under the if header.                                  Loops

          S EMANTICS

  1     <condition> is evaluated.
  2     If True, the statements in <block> are executed
        (and control passes on to the next statement).
  3     If False, the <block> is skipped and control
        passes immediately to the next statement.
2-Way Decisions: The if-else Statement.                                     CS103



    S YNTAX An if-else statement has the form                         Computer
    if <condition> :                                                  Programs

                                                                      Simple Programs
        <block 1>
                                                                      Numbers
    else :
                                                                      Strings
        <block 2>                                                     Functions

    S EMANTICS Evaluate <condition>. If True                          Graphics

   execute <block 1> under the if. If False execute                   Decisions

                                                                      Loops
   <block 2> under the else. In any case, control
   then passes to the next statement.



                                 test?



                   statement 1           statement 2




Multi-Way Decisions: if-elif-else.                                          CS103


    S YNTAX An if-elif-else statement has the form
                                                                      Computer
    if <condition 1> :                                                Programs

        <block 1>                                                     Simple Programs

    elif <condition 2> :                                              Numbers

        <block 2>                                                     Strings

                                                                      Functions
    ...
                                                                      Graphics
    elif <condition n> :
                                                                      Decisions
        <block n>                                                     Loops
    else :
        <block 0>


      test1?       test2?        ...          testn?




    statement1   statement2                 statementn   statement0

                                 ...
Relational Expressions.                                        CS103




                                                         Computer
    A relational expression has the form                 Programs

     <expr1> <relop> <expr2>                             Simple Programs

    where <expr1> and <expr2> are (arithmetical)         Numbers

    expressions and the relational operator <relop> is   Strings

                                                         Functions
    one of the following:
                                                         Graphics

                                                         Decisions
           <      a<b   less than                        Loops
          <=      a b   less than or equal
          ==      a=b   equal
          >=      a b   greater than or equal
           >      a>b   greater than
          !=      a=b   not equal
          in      a∈b   membership test
          is            object identity




Boolean Expressions.                                           CS103




    Python provides a data type bool and the literals    Computer
                                                         Programs
    True and False for truth values.                     Simple Programs
     S YNTAX The boolean operators                       Numbers

     <expr1> and <expr2>                                 Strings

     <expr1> or <expr2>                                  Functions

                                                         Graphics
     not <expr>
                                                         Decisions
    are used to build boolean expressions out of
                                                         Loops
    expressions which evaluate to True or False.
     S EMANTICS The value of a boolean expression is
    defined in terms of truth tables:
                       P       Q     P and Q    P or Q
     P    not P      True    True      True       True
   True   False      True   False     False       True
  False    True     False    True     False       True
                    False   False     False      False
Boolean Algebra.                                               CS103



    Commutative Laws.
                                                         Computer
                   P and Q == Q and P                    Programs

                    P or Q == Q or P                     Simple Programs

                                                         Numbers
    Laws of 0 and 1.
                                                         Strings
            P and False == False (p · 0 = 0)             Functions
               P and True == P (p · 1 = p)               Graphics

               P or False == P (p + 0 = p)               Decisions

           P or True == True (p + 1 = 1 ???)             Loops

    Double Negation.
             not (not P) == P (−(−p) = p)
    De Morgan’s Laws.
       not (P or Q) == (not P) and (not Q)
       not (P and Q) == (not P) or (not Q)
    Distributive Laws.
    P or (Q and R) == (P or Q) and (P or R)
    P and (Q or R) == (P and Q) or (P and R)

Exception Handling: try-except.                                CS103




                                                         Computer
     S YNTAX A try-except statement has the form         Programs

     try :                                               Simple Programs

                                                         Numbers
          <block>
                                                         Strings
     except <error>:                                     Functions
          <handler>                                      Graphics
    where <block> and <handler> are sequences of         Decisions
    one or more statements and <error> is an error       Loops
    type.
    A try statement can have several except clauses.
     S EMANTICS Attempt to execute the statements in
    <block>. If no error occurs, control passes to the
    next statement. Otherwise find a matching error
    type <error> and execute the statements
    <handler> of its except clause.
Summary: Decision Structures.                                      CS103




    Decision structures . . .                                Computer
                                                             Programs

    . . . are control structures that allow a program to     Simple Programs

    execute different sets of instructions under different   Numbers

    circumstances.                                           Strings

                                                             Functions
    . . . are implemented in Python with if statements.
                                                             Graphics
    . . . are based on the evaluation of conditions.         Decisions

    . . . can be nested.                                     Loops


    Conditions can involve relational operators and
    boolean operators.
    The value of a boolean expression is either True
    or False.
    Python has a boolean data type bool .
    Python also provides a try-except statement for
    exception handling.

						
Related docs
Other docs by ut8wn9s0
The Basics of Nonprofit Financial
Views: 8  |  Downloads: 0
Time for Action – conference statement
Views: 2  |  Downloads: 0
S1 SEN Statement 6704
Views: 10  |  Downloads: 0
Research Statement Tamon Asonuma
Views: 13  |  Downloads: 0
2009 Board Candidate Statements
Views: 12  |  Downloads: 0