CPS 1 - Spring, 1997 - Ramm 1/27/97 #5
- Announce
- Check out sample quiz on Web for Wednesday
- Please use your permission number now!
Chapter 1. An Intro to Programming: Coding Decision Trees
- Turbo Pascal Summary (Syntax Description)
- need a precise notation
- more formal method
- <identifier> -> //a sequence of letters and/or digits that begin
with a letter//
- <string expression> -> <identifier>
- <string expression> -> '//any string of printable chars//'
- <statement> -> writeln(<string expression>)
- <statement> -> readln(<identifier>)
- <statement> -> readln
-
| <compound statement> | -> | begin
| | | //a sequence of <statements>s
| | | each followed by a semicolon//
| | | end
| |
|---|
|
|---|
|
|---|
- To Use: substitute, multiple times as necessary
- Example with compound statement: Want:
- begin
- readln(msg);
- writeln(msg);
- end
| #7 | <compound statement> | -> | begin
|
|---|
| | | | <statement>;
|
|---|
| | | | <statement>;
|
|---|
| | | | end
|
|---|
| | | |
|
|---|
| | <compound statement> | -> | begin
|
|---|
| #5 | | | readln(<identifier>);
|
|---|
| | | | <statement>;
|
|---|
| | | | end
|
|---|
| | | |
|
|---|
| | <compound statement> | -> | begin
|
|---|
| #1 | | | readln(msg);
|
|---|
| | | | <statement>;
|
|---|
| | | | end
|
|---|
| | | |
|
|---|
| | <compound statement> | -> | begin
|
|---|
| | | | readln(msg);
|
|---|
| #4 | | | writeln(<string expression>) ;
|
|---|
| | | | end
|
|---|
| | | |
|
|---|
| | <compound statement> | -> | begin
|
|---|
| | | | readln(msg);
|
|---|
| #2 | | | writeln(<identifier>) ;
|
|---|
| | | | end
|
|---|
| | | |
|
|---|
| | <compound statement> | -> | begin
|
|---|
| | | | readln(msg);
|
|---|
| #1 | | | writeln(msg);
|
|---|
| | | | end
|
|---|
- Example involving compound statement
- begin
- writeln('Your name?'));
- readln(name);
- end
- Do, In Class, as if we were taking a quiz.
- Allow Some Shortcuts
- Help from class required
- What about IF-THEN-ELSE:
| 8. | <statement>
| -> | if <boolean expression> then
| | | <compound statement>
| | | else
| | | <compound statement>
|
|---|
|
|---|
|
|---|
|
|---|
- IF-THEN:
| 9. | <statement>
| -> | if <boolean expression> then
| | | <compound statement>
|
|---|
|
|---|
- Boolean Expression:
| 10. | <boolean expression>
| -> | if <identifier> =
| | | | <string expression>
|
|---|
|
|---|
- What about a Complete Program?
| 11. | <variable declaration>
| -> | //nothing//
| | | |
|
|---|
| 12. | <variable declaration>
| -> | var //list of//
| | | | <identifier>s : <type>;
| | | |
|
|---|
| 13. | <type>
| -> | string
| | | |
|
|---|
| 14. | <program>
| -> | program <identifier>;
| | | | <variable declarations>
| | | | <compound statement>.
|
|---|
|
|---|
|
|---|
|
|---|
|
|---|
|
|---|
|
|---|
- Can use these rules to derive the following program.
- program train;
- var
- gate:string;
- begin
- writeln('Is crossing gate up or down?');
- readln(gate);
- if gate = 'down' then
- begin
- writeln('Watch the train go by.');
- end
- else
- begin
- writeln('We beat the train this time!');
- end;
- readln;
- end.
- Will start ...
| #14 | <program> | -> | program <identifier>;
|
|---|
| | | | <variable declarations>
|
|---|
| | | | <compound statement>.
|
|---|
| | | |
|
|---|
| #1 | <program> | -> | program train;
|
|---|
| | | | <variable declarations>
|
|---|
| | | | <compound statement>.
|
|---|
| | | |
|
|---|
| | <program> | -> | program train;
|
|---|
| #12 | | | var
|
|---|
| | | | <identifier>:<type>;
|
|---|
| | | | <compound statement>.
|
|---|
| | | |
|
|---|
| | <program> | -> | program train;
|
|---|
| | | | var
|
|---|
| #1 | | | gate: <type>;
|
|---|
| | | | <compound statement>.
|
|---|
| | | |
|
|---|
| | <program> | -> | program train;
|
|---|
| | | | var
|
|---|
| #13 | | | gate: string;
|
|---|
| | | | <compound statement>.
|
|---|
| | | |
|
|---|
| | <program> | -> | program train;
|
|---|
| | | | var
|
|---|
| | | | gate: string;
|
|---|
| #7 | | | begin
|
|---|
| | | | <statement>;
|
|---|
| | | | <statement>;
|
|---|
| | | | <statement>;
|
|---|
| | | | <statement>;
|
|---|
| | | | end.
|
|---|
- Look at book for complete examples
- Shows program is syntactically correct except for choice of
identifiers. Doesn't help logic.
- There are other system for describing syntax
Chapter 2. Text Manipulation and Algorithm Design
- Intro Text Processing
- Dead Sea Scrolls Problem
- Concordance was published:
- Each word and where it appears
- PC was used to reconstruct the texts
- Word Processing
- Everyone Does it.
- Probably most frequent form of computer use
- Already have defined variable of type STRING
- Need numbers for text processing
- Assignment Statement
- x := y;
- contents of x destroyed;
- becomes identical to contents of y
- COPY, not a MOVE
- First Algorithm: Exchange Problem
- start
- x contains 'milk'
- y contains 'juice'
- how do we interchange contents?