Wednesday, August 20, 2014

Section 2-3: If-then Statements in Computer Programs (Day 10)

Section 2-3 of the U of Chicago text discusses computer programs written in BASIC. We must remind ourselves that this book was written over 20 years ago, back when BASIC was a popular language. But now, BASIC has been derided as "spaghetti code" and isn't as widely used anymore.

Of course, I asked myself whether I should even include this lesson on this blog. After all, I could have skipped it just as I omitted parts of Chapter 1 -- I can't expect the students in the classroom to have access to a computer at all, much less one that can be programmed in BASIC. But many students do carry around something that can be programmed -- a graphing calculator.

Now I'm aware that many students, especially below Algebra II, don't have graphing calculators. And even those who do have such a calculator usually don't know how to program it. I have an old TI-83 that I've owned ever since I was an AP Calculus student. Nowadays the standard is a TI-84. But still, I want to discuss how to program the TI graphing calculator. If the students have graphing calculators, then they might find this to be an interesting lesson -- especially since one of the reasons given for studying higher math is its use in computer science and video games. But if the students don't have access to calculators, or if the teacher doesn't wish to teach programming, then there can be a second day of Section 2-2.

The first BASIC program in the U of Chicago text gives the number of diagonals in a polygon:

10 PRINT "COMPUTE NUMBER OF DIAGONALS IN POLYGON"
20 PRINT "ENTER THE NUMBER OF SIDES"
30 INPUT N
40 IF N>=3 THEN PRINT "THE NUMBER OF DIAGONALS IS "; N*(N-3)/2
50 END

Now let's convert this program to the language of the TI. This language is often called TI-BASIC, even though it's not quite the same as usual BASIC. The first thing that we notice is that unlike BASIC programs, TI-BASIC programs have names. Also, BASIC lines have numbers, while TI-BASIC lines simply begin with a colon. Let's give this program the name DIAGONAL:

PROGRAM:DIAGONAL
:Disp "COMPUTE N
UMBER"
:Disp "OF DIAGON
ALS IN"
:Disp "POLYGON"
:Disp "ENTER THE
 NUMBER"
:Disp "OF SIDES"

:Input N
:If N>3
:Then
:Disp "THE NUMBE
R OF"
:Disp "DIAGONALS
 IS",N(N-3)/2
:End

We notice that the command PRINT in BASIC corresponds to Disp in TI-BASIC. The problem is that unlike a computer screen, the TI screen is only 16 characters across. So I had to divide up the lines of text into several Disp lines on the TI. The command Input is the same for both languages.

But, considering the title of this lesson, the emphasis is on the If line. We observe several differences between BASIC and TI-BASIC. First, in BASIC we write "greater than or equal to" as >=, but on the TI it has its own symbol, found on the TEST menu (2nd-MATH). Next, we see that Then has its own line on the TI, where in BASIC it is in the middle of the line. When displaying the number of diagonals, we notice that TI-BASIC uses a comma where BASIC uses a semicolon. Finally, on the TI we may use juxtaposition for multiplication, where in BASIC we must use an explicit multiplication system. Notice how the TI displays multiplication and division with an asterisk and a slash, respectively. These symbols actually date back to computer BASIC and other languages.

When I wrote this program in the exercises, I decided to include less text to display, since all the text printed by BASIC in the U of Chicago book was intended for widescreen computer monitors. Notice that my new if-then statement looks like this:

:If N>3
:Then
:Disp "DIAGONALS
",N(N-3)/2
:End

A TI-BASIC programmer wouldn't usually write this. As it turns out, if the body of the Then section contains only a single statement, then the line Then can be left out. But I decided to include it anyway in order for this program to look more like the BASIC program -- and to emphasis the word Then because of its importance in mathematical logic and geometry. Actually, if the word Then is omitted, then so should the word End. In TI-BASIC, End denotes the end of the if-then statement, whereas in BASIC, the word END denotes the end of the entire program. Actually, when I used to program in BASIC, I didn't need to include Line 50 in my program, but some computers require that line.

Here are some of the other programs from the questions, converted into TI-BASIC. First, here's the program from Question 12:

PROGRAM:ROBOTS
:Disp "HOW MANY 
ROBOTS"
:Input N
:Disp "PROFIT",5
N-1500
:If N<300
:Then
:Disp "ORDER MOR
E ROBOT"
:End

Sorry about the bad grammar there. The sentence "Order more robots" contains 17 characters when the maximum is 16.

Notice that the program in Question 13 uses 3.14159 for pi and multiplies by r twice. But the TI has both a pi key and a squared key. The proper way to enter the line corresponding to 20 is: start by entering pi (which is 2nd-^), then the letter R, then the squared key (left side of the calculator), then the STO-> key (just above ON -- this corresponds to the BASIC LET), and finally the letter A.

Question 25, in the Exploration section, contains a FOR loop in Line 20. As it turns out, TI-BASIC also has For loops. Here is the entire program:

PROGRAM:DIAGLOOP
:For(N,3,20)
:Disp "SIDES",N
:Disp "DIAGONALS
",N(N-3)/2
:Pause
:End

I decided to add a Pause at the end of the loop. Once again, this is because not all the lines of output can be displayed at the same time on the tiny calculator screen. In order to get the calculator to resume after pausing, simply press the ENTER key. Notice that the End line at the end of this program actually ends the For loop, so it corresponds to the line 50 NEXT N in BASIC, not the line 60 END. So this End line cannot be omitted.

Finally, question 26 asks the student to print an appropriate message if the input is less than 3. I assume that the book intends the student to add a second if-then statement:

:If N>3
:Then
:Disp "DIAGONALS
",N(N-3)/2
:End
:If N<3
:Then
:Disp "NOT ENOUG
H SIDES"
:End

But no professional programmer would do this. Instead, a programmer in both BASIC and TI-BASIC would use the Else command:

:If N>3
:Then
:Disp "DIAGONALS
",N(N-3)/2
:Else
:Disp "NOT ENOUG
H SIDES"
:End

But Else, unfortunately, is not mentioned in the U of Chicago text. In the end, since there isn't enough room (nor likely enough time, unless this becomes a two-day lesson) I decided to include the example program and the program in Question 12, but not the one in Question 13 (in order to avoid explaining the pi key or the STO-> key). Question 25 is included (since it's only a bonus question anyway), but not 26 (since it would be better written with Else).

If the condition in an If statement is false, then the Then statement is not executed. The book uses this fact to segue into what happens in mathematical logic if the hypothesis is false. Then as it turns out, the entire conditional is automatically true. This is called vacuous truth. The concept of vacuous truth can be confusing to many students. For example, the statement:

All unicorns are white.

is actually true -- after all, we have never seen a unicorn that isn't white (precisely because there exists no unicorns at all, much less ones that aren't white). Another way of thinking about this is that there are zero unicorns in this world, and all zero of them are white! In if-then form this statement becomes:

If an animal is a unicorn, then it is white.

The hypothesis is false (since there are no unicorns), so the entire conditional is true. This statement has no counterexamples (unicorns that aren't white), and conditionals without counterexamples are normally called true.

The book then derives, from the statement 1=2, the statement 131=177. There is a famous example of a derivation of a false conclusion from a false hypothesis, often attributed to the British mathematician Bertrand Russell, about a hundred years ago. From the statement 1=2, Russell proved that he was the Pope:

The Pope and I are two, therefore the Pope and I are one.

that is, he used the the Substitution Property of Equality from the hypothesis 1=2.


No comments:

Post a Comment