### Modular Program Components; If-Else and While Loops. # Now that we've learned how to write boolean expressions, we can # use them to form if statements, if-else statements, and while loops. x = input("Enter a number: ") if x<0: print "x is negative" if x>0: print "x is positive" if x==0: print "x is zero" # The statement after the : will only be executed if the boolean expression # evaluates to True. Each if statement will be tested and executed in turn. # A if statement can also optionally have an else part: if x!=0: print 1/x else: print "can't divide by zero" # The else part will be executed if the boolean condition is false: thus in # an if-else statement something will always be executed. # What does the following print? x, y = 4,2 # simultaneously assigns x to 4 and y to 2 if x>y: print "x is bigger" if xy: print "x is bigger" else: if x0: if b>0: print "ok" # This is equivalent to: if (a==b and a>0 and b>0): print "ok" # As general practice, do not nest more if statements than necessary. # QUIZ: what does the following print? a,b = 2,2 if a != b: # same as if not(a==b): if a>b: print "a is bigger" else: print "a is not bigger" print "a and b are the same" # ??????? #### while loops. # A while loop is like an if statement (with no else: part), except it # retests the boolean condition after each execution of its body, until # the boolean condition becomes false, which is possible because we can # change the value of variables. x = 1 while x<10: print x*x x = x+1 # end while x # While loops will usually contain more than one statement, properly aligned. # The comment at the end is REQUIRED, otherwise your code will become # unreadable when it gets large. # If the boolean condition is true, it will execute the body, then retest # the condition, jumping out of the loop when it becomes false. It # always executes the entire body of the while loop before retesting the # boolean condition: it does not terminate in the middle. The following # prints something slightly different. x = 1 while x<10: x = x+1 print x*x # end while x # count the number of lines printed: you should notice that this time # it printed 10 numbers, not 9. Why? Understanding such details is # crucially important. The trickiest part of writing while loops is to # understand HOW IT STARTS and WHEN IT ENDS. The beginning and the end # are usually where mistakes are made. # The following prints 1, 4, 9, etc.. up to 100. x = 0 while x<10: x = x+1 print x*x # end while x # The Fibonacci sequence is 1 1 2 3 5 8 13 21 ... The following prints the # the first 30 Fibonacci numbers: n = 30 # counts downward how many numbers are left to be printed. a,b = 0,1 while n>0: print b, " ", # The , at the end means there's no line break. a,b = b,a+b n -= 1 # this is shorthand for n = n-1 # while n print "" # prints line break AFTER the while loop. ## The body of while loops can be arbitrarily large and complex. It can # contain if statements, as well as nested while loops. x = 1 while x<10: y = 1 while y<10: print x*y, "\t", # \t is the tab character y = y+1 # inner while print "" # prints blank, then line break x = x+1 # outer while # This prints a multiplication table. ### At this point, it is important for you to realize that a program is much # more complex than just a sequence of commands. The program must be # STRUCTURED correctly. It's not just one line followed by another: how # these lines relate to eachother is crucially important. # To better understand a nested while loop such as above, you can't just # read the code line-by line. It is important to be able to identify the # five principal components that makes up a while loop: # 1. preamble: sets up initial conditions before loop begins # 2. boolean expression: determines whether loop continues or ends # 3. update: condition must be updated for loop to stop eventually # 4. main body of the while loop # 5. postamble: final or clean-up action after the loop stops. # The postamble is sometimes empty, but conceptually we should still # think that it's present. # Exercise: take any of the sample loops below, and identify the component # that each line belongs to. ### More sample while loops: # The following loop computes the greatest common divisor of a and b # by implementing Euclid's algorithm, which can be described as follows. # Given two numbers: # 1. divide the larger number by the smaller number and find the remainder. # 2. replace the larger number by the remainder found above. # 3. repeat steps 1 and 2 until one of the two numbers is zero # 4. the gcd is the remaining (non-zero) number. # 5: clarification, if two numbers are equal, divide any one by the other. # and if one number is zero to begin with, the other is the answer. a,b = input("enter two numbers: ") while (a!=0 and b!=0): if a>b: a = a%b else: b = b%a # while gcd = a+b # one of a or b will be zero and automatically canceled print "the gcd is",gcd # The following while loop checks if a number is a prime number. A number # is prime if it is evenly divisible (does not leave remainder) only by # one and itself. n = input("enter a number: ") # number to test for primality testcase = 2 # first number to test against answer = true while testcase= n/2, or even sqrt(n).