Docsity

Conditional and Recursion - Introduction to Programming | EECS 12, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
Chapter 4
Conditionals and recursion
4.1 The modulus operator
The modulus operator works on integers (and integer expressions) and yields
the remainder when the first operand is divided by the second. In Python, the
modulus operator is a percent sign (%). The syntax is the same as for other
operators:
>>> quotient = 7 / 3
>>> print quotient
2
>>> remainder = 7 % 3
>>> print remainder
1
So 7 divided by 3 is 2 with 1 left over.
The modulus operator turns out to be surprisingly useful. For example, you
can check whether one number is divisible by another—if x%yis zero, then x
is divisible by y.
Also, you can extract the right-most digit or digits from a number. For example,
x%10yields the right-most digit of x(in base 10). Similarly x % 100 yields
the last two digits.
4.2 Boolean expressions
Aboolean expression is an expression that is either true or false. In Python,
an expression that is true has the value 1, and an expression that is false has
the value 0.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Conditional and Recursion - Introduction to Programming | EECS 12 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Chapter 4

Conditionals and recursion

4.1 The modulus operator

The modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for other operators:

quotient = 7 / 3 print quotient 2 remainder = 7 % 3 print remainder 1

So 7 divided by 3 is 2 with 1 left over.

The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another—if x % y is zero, then x is divisible by y.

Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits.

4.2 Boolean expressions

A boolean expression is an expression that is either true or false. In Python, an expression that is true has the value 1, and an expression that is false has the value 0.

36 Conditionals and recursion

The operator == compares two values and produces a boolean expression:

5 == 5 1 5 == 6 0

In the first statement, the two operands are equal, so the expression evaluates to 1 (true); in the second statement, 5 is not equal to 6, so we get 0 (false).

The == operator is one of the comparison operators; the others are:

x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y

Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols. A common error is to use a single equal sign (=) instead of a double equal sign (==). Remember that = is an assignment operator and == is a comparison operator. Also, there is no such thing as =< or =>.

4.3 Logical operators

There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10.

n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that is, if the number is divisible by 2 or 3.

Finally, the not operator negates a boolean expression, so not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.

Strictly speaking, the operands of the logical operators should be boolean ex- pressions, but Python is not very strict. Any nonzero number is interpreted as “true.”

x = 5 x and 1 1 y = 0 y and 1 0

38 Conditionals and recursion

If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that effect. If the condition is false, the second set of statements is executed. Since the condition must be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution.

As an aside, if you need to check the parity (evenness or oddness) of numbers often, you might “wrap” this code in a function:

def printParity(x): if x%2 == 0: print x, "is even" else: print x, "is odd"

For any value of x, printParity displays an appropriate message. When you call it, you can provide any integer expression as an argument.

printParity(17) printParity(y+1)

4.6 Chained conditionals

Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained condi- tional:

if x < y: print x, "is less than", y elif x > y: print x, "is greater than", y else: print x, "and", y, "are equal"

elif is an abbreviation of “else if.” Again, exactly one branch will be executed. There is no limit of the number of elif statements, but the last branch has to be an else statement:

if choice == ’A’: functionA() elif choice == ’B’: functionB() elif choice == ’C’: functionC() else: print "Invalid choice."

4.7 Nested conditionals 39

Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.

As an exercise, wrap these examples in functions called compare(x, y) and dispatch(choice).

4.7 Nested conditionals

One conditional can also be nested within another. We could have written the trichotomy example as follows:

if x == y: print x, "and", y, "are equal" else: if x < y: print x, "is less than", y else: print x, "is greater than", y

The outer conditional contains two branches. The first branch contains a simple output statement. The second branch contains another if statement, which has two branches of its own. Those two branches are both output statements, although they could have been conditional statements as well.

Although the indentation of the statements makes the structure apparent, nested conditionals become difficult to read very quickly. In general, it is a good idea to avoid them when you can.

Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional:

if 0 < x: if x < 10: print "x is a positive single digit."

The print statement is executed only if we make it past both the conditionals, so we can use the and operator:

if 0 < x and x < 10: print "x is a positive single digit."

These kinds of conditions are common, so Python provides an alternative syntax that is similar to mathematical notation:

4.9 Recursion 41

countdown expects the parameter, n, to be a positive integer. If n is 0, it outputs the word, “Blastoff!” Otherwise, it outputs n and then calls a function named countdown—itself—passing n-1 as an argument.

What happens if we call this function like this:

countdown(3)

The execution of countdown begins with n=3, and since n is not 0, it outputs the value 3, and then calls itself...

The execution of countdown begins with n=2, and since n is not 0, it outputs the value 2, and then calls itself... The execution of countdown begins with n=1, and since n is not 0, it outputs the value 1, and then calls itself... The execution of countdown begins with n=0, and since n is 0, it outputs the word, “Blastoff!” and then returns. The countdown that got n=1 returns. The countdown that got n=2 returns.

The countdown that got n=3 returns.

And then you’re back in main (what a trip). So, the total output looks like this:

3 2 1 Blastoff!

As a second example, look again at the functions newLine and threeLines:

def newline(): print

def threeLines(): newLine() newLine() newLine()

Although these work, they would not be much help if we wanted to output 2 newlines, or 106. A better alternative would be this:

42 Conditionals and recursion

def nLines(n): if n > 0: print nLines(n-1)

This program is similar to countdown; as long as n is greater than 0, it outputs one newline and then calls itself to output n-1 additional newlines. Thus, the total number of newlines is 1 + (n - 1) which, if you do your algebra right, comes out to n.

The process of a function calling itself is recursion, and such functions are said to be recursive.

4.10 Stack diagrams for recursive functions

In Section 3.11, we used a stack diagram to represent the state of a program during a function call. The same kind of diagram can help interpret a recursive function.

Every time a function gets called, Python creates a new function frame, which contains the function’s local variables and parameters. For a recursive function, there might be more than one frame on the stack at the same time.

This figure shows a stack diagram for countdown called with n = 3:

main

countdown

countdown

countdown

countdown

n 3

n 2

n 1

n 0

As usual, the top of the stack is the frame for main. It is empty because we did not create any variables in main or pass any parameters to it.

The four countdown frames have different values for the parameter n. The bottom of the stack, where n=0, is called the base case. It does not make a recursive call, so there are no more frames.

As an exercise, draw a stack diagram for nLines called with n=4.

44 Conditionals and recursion

name = raw_input ("What...is your name? ") What...is your name? Arthur, King of the Britons! print name Arthur, King of the Britons!

If we expect the response to be an integer, we can use the input function:

prompt = "What...is the airspeed velocity of an unladen swallow?\n" speed = input(prompt)

If the user types a string of digits, it is converted to an integer and assigned to speed. Unfortunately, if the user types a character that is not a digit, the program crashes:

speed = input (prompt) What...is the airspeed velocity of an unladen swallow? What do you mean, an African or a European swallow? SyntaxError: invalid syntax

To avoid this kind of error, it is generally a good idea to use raw input to get a string and then use conversion functions to convert to other types.

4.13 Glossary

modulus operator: An operator, denoted with a percent sign (%), that works on integers and yields the remainder when one number is divided by an- other.

boolean expression: An expression that is either true or false.

comparison operator: One of the operators that compares two values: ==, !=, >, <, >=, and <=.

logical operator: One of the operators that combines boolean expressions: and, or, and not.

conditional statement: A statement that controls the flow of execution de- pending on some condition.

condition: The boolean expression in a conditional statement that determines which branch is executed.

compound statement: A statement that consists of a header and a body. The header ends with a colon (:). The body is indented relative to the header.

4.13 Glossary 45

block: A group of consecutive statements with the same indentation.

body: The block in a compound statement that follows the header.

nesting: One program structure within another, such as a conditional state- ment inside a branch of another conditional statement.

recursion: The process of calling the function that is currently executing.

base case: A branch of the conditional statement in a recursive function that does not result in a recursive call.

infinite recursion: A function that calls itself recursively without ever reach- ing the base case. Eventually, an infinite recursion causes a runtime error.

prompt: A visual cue that tells the user to input data.