In the program "conversation.p",
what would happen if you entered your class
instead of your name?
* Try
it!
d) Pascal so far:
* basic instructions (or "statements", or "commands"):
writeln()
readln()
* sequencing (or "compound statement"):
begin
<instruction>;
.
.
.
<instruction
end
* indentation for human readability
(not enforced by compiler,
as it was for Karel)
* semicolons: signal sequencing
therefore, go between 2 statements
(just as in Karel)
* how to keep track of "begin"s & "end"s:
label
them with comments
(as I did in "How
to Read")
e) Programming a Decision Tree
* Here's a very simple DT:
---------------- /| Take CSE 115 | yes / ---------------- / --------------- / | Do you plan |/ | to major in |\ | CSE? | \ --------------- \ \ no \ \------------------- | Take 101 or 111 | | or 113 | -------------------As with all DTs, this involves a choice;
TDD-1:
var answer : varying [20] of char;
writeln('Do you plan to major in CSE?');
readln(answer);
if answer = 'yes'
then {response for "yes"
branch}
else {response for "no"
branch}
SWR-2:
if answer = 'yes'
then writeln('Take CSE 115')
else begin
writeln('Take 101 or 111');
writeln('or 113')
end
SWR-3:
program DecisionTree1;
var answer : varying [20] of char;
begin {DecisionTree1}
writeln('Do you plan to
major in CSE?');
readln(answer);
if answer = 'yes'
then writeln('Take CSE 115')
else begin {else if answer
<> 'yes'}
writeln('Take 101 or 111');
writeln('or 113')
end {else if answer <> 'yes'}
end. {DecisionTree1}
Comments:
1. In the test, "answer = 'yes'
",
what happens
if the user types in "YES" or "y"?
Try it to see!
2. Note the way that I commented
the
"begin/end" pairs
3. Note the way you have to show
an
"unequals" sign in Pascal: <>
f) Coding the CSE DT:
* Here's the general form of a DT:
---------- | C | | / | = A | yes/ | | / | | Q2 | | \ | | no\ | | \ | | D | ---------- / yes / / / Q1 Etc. \ \ no\ \ ---------- | E | | / | = B | yes/ | | / | | Q3 | | \ | | no\ | | \ | | F | ----------* In "if-then-else" format, this becomes:
if Q1 = yes
then A
else B
where A is:
if Q2 = yes
then C
else D
& where B is:
if Q3 = yes
then E
else F
* Putting it all together:
if Q1 = yes
then if Q2 = yes
then C
else D
else if Q3 = yes
then E
else F
* In our case:
if Q1 = yes
then {write something}
else if Q3 = yes
then {write something}
else F
Etc.
(to be continued in Lecture Notes #15...)