CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #15

PROGRAMMING IN PASCAL:
DECISION TREES

(3.  Programming in Pascal, continued)

f)    Coding the CSE DT (concluded)

*    Here is the structure of our DT in Pascal-like
      notation;  you can consider this to be the top
      level of the TDD of the Pascal version of our
      DT:

        write Question
        read Answer
        if Answer = yes
            then write "115"
            else do the no-branch

    The no-branch looks like this:

        write Question
        read Answer
        if Answer = yes
            then write "113"
            else do the no-branch

    This no-branch also looks the same:

        write Question
        read Answer
        if Answer = yes
            then write "111"
            else do the no-branch

    Etc.

    The final step looks like this:

        write Question
        read Answer
        if Answer = yes
            then write "111"
            else write "take a different course"

*    Here's the next step of the SWR:

    program CourseAdvisor;
    var answer : varying [20] of char;
    begin
        writeln('Do you plan to major in CSE?');
        readln(answer);
        if answer = 'yes'
            then writeln('Take CSE 115')
            else {code for no-branch}
    end.

    This program will compile and run,
    but it leaves something to be desired,
    since the "else" branch does nothing.

    Here's a more useful version:

    program CourseAdvisor;
    var answer : varying [20] of char;
    begin
        writeln('Do you plan to major in CSE?');
        readln(answer);
        if answer = 'yes'
            then writeln('Take CSE 115')
            else writeln('No-branch not yet coded')
    end.

*    Now, let's code that else branch:

    We need to replace the "writeln" command
   following that "else" by the same sort of trio
    of "writeln question", "readln answer",
    "if-then-else" that we had previously,
    surrounded by "begin/end".

    We'll also need a new variable for the new
    answer, so it will also be useful to rename
    the first one:

    program CourseAdvisor;
    var answer1, answer2 : varying [20] of char;
    begin
        writeln('Do you plan to major in CSE?');
        readln(answer1);
        if answer1 = 'yes'
            then writeln('Take CSE 115')
            else begin {if answer1 <> yes}
                     writeln('Do you need to learn');
                     writeln('how to program a computer?');
                     readln(answer2);
                     if answer2 = 'yes'
                        then writeln('Take CSE 113')
                        else writeln('no-branch not coded')
                    end {if answer1 <> yes}
    end.

*    Etc., until done.  To see the final version,
      click here.

*    You can save that file to your Unix directory as text,
     compile it, and execute it.

THIS ENDS THE MATERIAL THAT WILL BE ON THE
MID-TERM EXAM.

THE MATERIAL ON PASCAL GRAMMAR THAT I BEGAN
IN LECTURE ON OCT. 10 WILL BE COVERED IN
LECTURE NOTES #16.


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes15.10oc00.html