### CS 1110 - Introduction to Programming

#### Starting Out with Python Chapter 3, Decision Structures and Boolean Logic

##### Objectives:

This lesson covers methods to make choices in programs. Objectives important to this lesson:

1. The if statement
2. The if-else statement
3. Comparing strings
4. Nesting decisions, and the if-elif-else statement
5. Logical operators
6. Boolean variables
##### Chapter 3

There are three structures that are used in most programs. They can all be called control structures, because they control the order of execution of the lines of instructions. It helps to know about them, so let's look at them before we start this lesson.

• Sequence - The simplest programs follow a sequence of instructions, starting at the top of the file and moving through it one line at a time.
• Selection - Sometimes the next thing to do in a program is determined by a user's choice, or the value of a variable. When this is true, the program must select between two or more paths through the code, executing some lines, and not executing others. Selection structures are also called decision structures, because the program must decide what to do next.
• Iteration - Sometimes you want a program to repeat a series of instructions, as long a something is true, or until something is true. Repeating a code section is called iteration or looping.

This week's lesson is about selection. The text illustrates sequence first. In the first code example in the chapter, the text points out that a sequence is a series of instructions that are to be followed one after another. Many programs are a single sequence that is followed each time the program runs.

The text gives us an example of a payroll program that must follow a different sequence of calculations when there is overtime to be paid. In a simpler example, it shows us part of a flow chart for a decision structure that is followed if something is true. In a program, the entry point of a decision structure may be an if statement. The something that must be true (or false, the logic can work either way, but it is pre) is called the condition. The key word if, followed by a condition, is called the if clause. The line or lines to execute are just called a block. Each line in the block is indented in Python. In the example in figure 3-2, if the condition is true, a block of three lines will be executed. If the condition is false, they are not executed.

So, you need to know some things about writing conditions. In table 3-1, we see list of relational operators. They compare a value on their left to a value on their right in relational conditions. Note that the equality comparison operator is a double equal sign, not a single one.

 Operator Meaning > what is on the left is greater than what is on the right < what is on the left is less than what is on the right >= what is on the left is greater than or equal to what is on the right <= what is on the left is less than or equal to what is on the right. == what is on the left is equal to what is on the right (comparison, not assignment) != what is on the left is not equal to what is on the right

If you have not seen these operators in math classes, the text explains them in detail.

We finally get a code example after the operator discussions. In the payroll example, the if structure's code block is three lines, all of which are indented by four spaces. This structure should be placed where it is needed in the program, after the data collection, and it would only execute its block of code if its condition is met.

We will examine the test_average.py program in class. Read through it, and let me know if you have questions about it.

A more useful decision structure starts with the if-else statement. The if statement only said "if this is true, do that". The if-else statement says "if this is true, do that, otherwise do this other thing". Note that the syntax is a little different. The text gives us this example:

if temperature < 40:
print("A little cold, isn't it?")
else:
print("Nice weather we're having.")

Note the two colons, one after the condition, and the other after the key word else. Also notice that the if and the else are aligned vertically (they start in the same column) and the two code blocks are indented and aligned vertically as well, even though these two blocks are only one line each.

Examine program 3-2. Before you look at the code, look at the pseudocode that precedes it. This is a good example of a plan for the sample program. The programmer could have made notes about what a user wanted in the program, then created this pseudocode to plan the actual code work. Turning to the actual code, it illustrates several concepts we have covered so far. From last week, it uses a named constant and remarks. It uses input() functions nested inside float() functions, so the data entered will be stored as floating point values. It also uses a control string in the print() function to make the output look more like a pay statement.

The next section discusses comparing strings instead of numbers. The first condition discussed is a test of equality. In the example in the text, two short strings are loaded into two variables, then an if-else structure is used to test whether they are equal. We can see that they are not, and so can the computer. In this case, the strings are Mark and Mary, so they actually match for the first three characters. The program checks them, one character at at time, until there is a difference noticed or, in the case of a match, there is no difference, both strings are the same length, and it has reached the end of both strings at the same time.

The text also discussed whether one string is greater than (or less than) another string. It explains that when it makes the character by character comparisons, it is actually comparing the ASCII value of each character. (Bet you thought we were never going to talk about ASCII again.) The text provides a good summary of ASCII so that you can judge whether such a condition is being evaluated correctly.

• a space has ASCII value 32
• the digits 0 through 9 have ASCII values 48 through 57
• the letters A through Z have ASCII values 65 through 90
• the letters a through z have ASCII values in a higher range: 97 through 122

So strings are compared character by character, and evaluated by their ASCII values. When entering strings, be careful not to leave trailing spaces. Such an addition would invalidate the comparisons.

This takes us to nested decision structures and  the if-elif-else statement. The chapter takes a bit to warm up to nested decision structures. It reviews the second example of an if statement we were shown. It remarks that the decision structure had a code block of three statements, and that we could look at that and truthfully say that the decision structure's TRUE condition has a three line sequence structure in it. What if we wanted to know more about the weather at that point? Couldn't we have put another decision structure inside the first one in the location of the sequence structure, that might have led to different actions for rain, snow, or sun?

To pursue this concept the text starts another example, this time of a program that decides whether a bank customer meets qualifications for a loan. Examining the code for program 3-5, we see that the programmer wrote an if-else statement inside another if-else statement. The applicant must answer two questions correctly to qualify for a loan.

• If the first condition is not met, there is no need to enter the decision structure. The first condition is FALSE, and its else prints the first disqualification notice.
• If the first condition is met, and the second condition is met, both if conditions are TRUE, so a qualification notice is printed.
• If the second condition was not met, the second if-else is FALSE, leading to its else clause printing the second kind of disqualification notice.

It is important in Python to maintain proper indents and vertical alignments of the matching parts of the inner and outer structures. Each if in this case has a matching else, and the two parts of a given pair must be aligned properly to make the program work.

The text presents an example of a grading program that illustrates how complicated indenting can be. In this example, the program may have to test a value against four different qualifying scores before it reaches a conclusion. This is not really such a bad example, but you should imagine what a program would look like that used twenty or more nested if-else statements. It would be quite hard to type and to read.

To overcome that problem, another decision structure was create, the if-elif-else statement. The name does not describe the flexibility of this statement. Consider the simplest case that needs it: we have three alternatives that the program must choose from. The logic for this case might look like this:
if condition_one:
outcome_one
elif condition_two:
outcome_two
else:
outcome_three

If the first condition is met, the first outcome is executed, and the program exits the structure. If the first condition is not met, the elif condition is evaluated just like it was a new else-if structure, except that it is not so much nested as it is just another alternative condition. If it is met, its outcome if executed and the program leaves the structure. The last petal of this flower is an else, because we decided (well, I did) that there were only three alternatives. The first two were not met, so there can only be one more outcome.

Now, the magic part. An if-elif-else statement can have any number of elif sections, as long as they are all mutually exclusive. Look at the rewrite the author did for the grading program. It is much neater and prettier once you understand it and understand the alternative.

The next to last section in the chapter is about logical operators. The text introduces us to three of them: and, or, and not. These are logical operators, also called Boolean operators. They are named for George Boole, an English mathematician who did early work on logic that established a basis for computing..

We can combine the tests we run with Boolean and, or, and not operators. Python uses these words in lower case. Most languages I have used prefer upper case.

 Boolean Operator Python example and x > 10 and x < 30 This would be true for all values between 10 and 30, not including 10 or 30 themselves. or x > 10 or x < 30 This would be true for all numbers, because any number is either more than 10 or less than 30. not not x This would be true for any value not equal to x.

Consider the possibilities:

• if we test for the truth of condition1 and condition2, the test will be true only if both of the expressions are true
• if we test for the truth of condition1 or condition2, the test will be true if either of the expressions are true, and also true if both are true
• if we test for the truth of not (condition1), the test will be true only if condition1 itself is false

The text revisits the loan qualifier program to illustrate the utility of checking for the only success condition for the applicant: salary is high enough, and work history is long enough. The logical and allows us to test for the condition of both qualifiers being true in one step.

Examples are given of program instructions that check for numbers inside and outside ranges whose boundaries are known. Look over this material. It will be useful.

The text concludes this section of the chapter with a discussion of Boolean variables. Python has a variable type for them called bool. What's special about them is that they can only hold the values True or False. The text refers to this class of variables an example of a flag variable, one whose value can be set in the program so that it stands for a condition being met or not.

 Assignments Assignments for these chapters will be found in Blackboard. We will explore that in class.