i := 1; {initializes the test}
while
i <= 10 do {an arithmetic test}
begin {while loop}
writeln('hello');
i := i+1
end {while loop}
end.
Click here for a downloadable/runnable version.
b) You can change the behavior of this program
by changing various parts
of it:
* to change
the number of times it outputs
'hello', change the test from "i <= 10" to
"i <= 3" or "i <= 20"
* to change
what it outputs, change the
"writeln" statement; e.g., you could have
it print out the contents of the memory
location "i" by changing the "writeln"
statement to: writeln(i)
* to change
other behaviors, you could
change the increment: Change "i := i+1"
to "i := i+2"
c) The next loop program we'll look at it is called
a "stub" (actually, being
a stub has nothing to
do with being a loop program).
* A program stub is a program that:
- runs
- does
something to tell you that it ran
successfully
- doesn't
(yet) do the details of what you
really want it to do
- has the structure of how you want it done
i.e., it's a top-down design of a program that
needs to be refined, but it runs!
* here it is:
program loop2;
{stub that repeatedly executes a user-provided
command until a "quit" command is issued}
var command : varying [20] of char;
begin {main}
writeln('COMMAND:');
readln(command);
{the previous 2 instructions
"prime the pump"
for the while-loop
that follows, getting the
test ready, so that
the loop will execute at
least once}
while command <> 'quit' do
begin {while}
writeln('Command ', command,
' has been executed');
{the previous line is the line that "does
something to tell you that the program
runs successfully" but "doesn't yet do
the details of what you really want it to do}
{the next two lines "prime" the while-loop
again}
writeln('COMMAND:');
readln(command)
end; {while}
{the next instruction is
the first one after the
loop exits,
so when this gets printed out, it
tells you that the loop has finished.}
writeln('The ', command,
' command was executed; good-bye.')
end. {main}
Click
here for a downloadable/runnable version.
program CSE111Editor;
{outline; needs refinement}
<type declarations go here>
<var declarations go here>
<function declarations go here>
begin {main}
{initialize text and pointer: text := ''; ptr := 0}
writeln('COMMAND:');
readln(command);
while command <> 'quit' do
begin {while}
if
command = 'i' then {insert new text};
if command = 'd' then {delete text};
if command = 's' then {insert space};
{etc.}
writeln('COMMAND:');
readln(command)
end; {while}
writeln(text)
end. {main}