CS100, Spring 1996: Answers to Quiz 5

This page contains questions and answers for the fifth quiz in CS100: Principles of Computer Science, a course based on the book The Most Comples Machine.

The answers given here are sample answers only. There is always some variation in the answers that would receive full credit.


Question 1: Consider the following xTurtle commands, which you should assume are the first few lines in a program:

        DECLARE NumberWanted
        AskUser("How many squares should I draw?", NumberWanted)
        LOOP
           EXIT IF (NumberWanted > 0)
           AskUser("Please enter a number greater than zero.", NumberWanted)
        END LOOP

What is the postcondition of this loop? Why do you suppose the programmer would include such a loop in her program?

Answer: The postcondition of the loop is that NumberWanted is greater than zero. This is the one fact that you absolutely know is true after the computer executes the loop. The point of this would be that the rest of the program depends on the fact that the value of NumberWanted is positive. For example, the program might crash or run forever if NumberWanted is not a positive number. By including the loop in the program, the programmer ensures that this can't happen.

Note: A postcondition is not a part of a program. It is a fact that is known to be true after a part of the program has been executed. Here, the fact that is true after the loop is done is that the value of the variable NumberWanted is greater than zero, since that is the only way that the loop can ever end. (A lot of people said that the postcondidtion was one of the statements in the loop. That's not correct. Of course, the reason that those statements are there is to ensure that the desired postcondition will be true when the loop ends.)


Question 2: Show the picture that would be drawn by the following xTurtle program:

              DECLARE length
              length := 5
              LOOP
                 forward(length) turn(90) forward(length)
                 back(length) turn(-90) back(length)
                 length := length - 1
                 EXIT IF length = 0
                 PenUp
                 turn(90) forward(1) turn(-90)
                 PenDown
              END LOOP

Answer:

Note: The turtle starts at the lower left of this picture Each pass through the loop draws one "L"-shaped figure. The part of the loop before "EXIT IF" draws the figure, returns the turtle to its starting point, and reduces length by 1. The part after the EXIT IF moves the turtle up one unit, without drawing a connecting line. A surprising number of people seemed to ignore the commands following the EXIT IF. These are executed each time through the loop except for the last time through.


Question 3: Explain what is meant by the syntax of a programming language, and give an example.

Answer: The syntax of a language refers to its grammar, that is, to the set of rules that determine what is "legal" in the language. For example, one of the syntax rules of xTurtle is that an assignment statement consists of a variable name, followed by :=, followed by a constant, a variable, or a mathematical formula. Thus, "x := 5" is a legal assignment statement, but "x = 5" and "5 := x" are not, because they do not follow the proper syntactic rules.


Question 4: Explain what is meant by a comment in a program. What does the computer do with comments? What is the purpose of including comments in programs?

Answer: A comment in a program is text put there for the benefit of a human reader, rather than the computer. The computer simply entirely ignores any comments in a program. Comments are there to explain what the program does, how it works, who wrote it, or anything else the programmer might want to say. (In xTurtle, comments are enclosed in curly brackets, {&npsb;and }.)


(by David Eck)