First Summer 2008


Lecture Four: Exercises with if statements.
Explain how this works, what it does and what it prints:
x = 3
if 2 > x:
  print 1,
else: 
  print 2,
if x < 2:
  print 3
print 4, 
Explain how this works, what it does and what it prints:
x = 3
if 2 > x:
  print 1,
else: 
  print 2,
if x < 2:
  print 3
  print 4, 
Explain how this works, what it does and what it prints:
x = 3
if x > 5:
  if x < 10:
    print 1, 
else:
  print 2,
print 3,
Explain how this works, what it does and what it prints:
x = 3
if x > 5:
  if x < 10:
    print 1, 
  else:
    print 2,
print 3,
Explain how this works, what it does and what it prints:
if 2 <= 3:
  if 0 != 1:
    print 0,
  else:
    print 1,
print 2,
if 2 > 3:
  if 0 == 1:
    print 3,
  else:
    print 4,
print 5,
Explain how this works, what it does and what it prints:
x = 1 > 2
if True:
  print 0,
else:
  print 1,
x = (1 < 2) and (4 < 3)
if x:
  print 2,
else:
  print 3, 
Explain how this works, what it does and what it prints:
x = 1 < 2
if True:
  print 0,
else:
  print 1,
x = (1 < 2) and (4 < 3)
if x:
  print 2,
else:
  print 3, 
Assume the following code:
if 2 > 1:
    print "Hey!"
else: 
    print "Ouch..."
Questions:

Explain how this works, what it does and what it prints:
x = False
if True:
  print 0,
else:
  print 1,
x = x or (not x)
if x:
  print 2,
else:
  print 3,
Explain how this works, what it does and what it prints:
x = False
if True:
  print 0,
else:
  print 1,
  x = x or (not x)
  if x:
    print 2,
  else:
    print 3,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,
else:
  if x > 1:
    print x,
  else:
    if x > 2:
      print x - 1,
    else:
      if x > 3:
        print 2 * x,
      else:
        print x * x,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,

  if x > 1:
    print x,
  else:
    if x > 2:
      print x - 1,
    else:
      if x > 3:
        print 2 * x,
      else:
        print x * x,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,

  if x > 1:
    print x,
  
    if x > 2:
      print x - 1,
    else:
      if x > 3:
        print 2 * x,
      else:
        print x * x,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,

  if x > 1:
    print x,

  if x > 2:
    print x - 1,
  else:
    if x > 3:
      print 2 * x,
    else:
      print x * x,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,

  if x > 1:
    print x,
else: 
  if x > 2:
    print x - 1,
  else:
    if x > 3:
      print 2 * x,
    else:
      print x * x,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,
if x > 1:
  print x,
if x > 2:
  print x - 1,
if x > 3:
  print 2 * x,
else:
  print x * x,
Explain how this works, what it does and what it prints:
x = 3
if x > 0:
  print x + 1,
if x > 1:
  print x,
if x > 2:
  print x - 1,
if x <= 3:
  print 2 * x,
print x * x,
Explain how this works, what it does and what it prints:
if False and False or True:
  print False,
else:
  print True
Explain how this works, what it does and what it prints:
if False and (False or True):
  print False,
else:
  print True
If x holds an integer value are these two code fragments equivalent:
// fragment 1    |         // fragment 2
                 |
if x == 5:       |         if x == 5:
   x = x + 1     |            x = x + 1
else:            |         if x != 5:
   x = 8         |            x = 8
If y has the value of 2 at the end of this code fragment what do we know about x?
if x > 3:
  if x <= 5:
    y = 1
  else:
    if x != 6:
      y = 2
    else:
      y = 3
else:
  y = 4
If y has the value of 2 at the end of this code fragment what do we know about x?
if x > 3:
  if x > 7:
    y = 1
  else:
    if x != 6:
      y = 2
    else:
      y = 3
else:
  y = 4
Take a look at this code:
if x > 3:
  if x > 10:
    y = 1
  else:
    if x > 12:
      y = 2
    else:
      y = 3
else:
  y = 4
For what initial values of x does y end up with a value of 2?


Last updated: May 09, 2008 by Adrian German for A201/A597