Beginning Pascal Tutorial Part 3 
Author Message
 Beginning Pascal Tutorial Part 3

                   Turbo Pascal for DOS Beginning Tutorial
                             by Glenn Grotzinger
             Part 3 -- While and Repeat Loops; Case Statements
            all parts copyright 1995-96 (c) by Glenn Grotzinger.

        Hello again.  I haven't gotten much input about continued
interest in this tutorial.  I may discontinue it if the interest
doesn't pick up.  Otherwise, I'll assume everybody's dead and I'll quit. :> I am
planning on taking this tutorial through all of the Pascal data structures, and maybe
special topics (advanced ones, too, listen up self-professed Pascal experts -- you
may even learn something. :>).  I haven't gotten any suggestions on the special

An example of a solution of last week's programming problem:

program part2;

  { This program accompanies part 2.  It is a program designed to take a
    number representing the dimensions of a multiplication table from the
    user, and then write a multiplication table out to the screen. }

  var
    dimension: integer;
    i, j: integer;

  begin { part 2 }

    {take input for the dimension}
    write('What dimension number do you want for a table? ');
    readln(dimension);
    writeln;

    { if dimension not greater than 15, process table }
    if dimension <= 15 then
      begin

        { write top line of table }
        write('   ');
        for i := 1 to dimension do
          write(i:4);
        writeln;

        { write top rule line }
        write(#201:3);
        for i := 1 to (dimension * 4) do
          write(#205);
        writeln;

        { write rest of table }
        for i := 1 to dimension do
          begin
            if i < 10 then
              write(' ');
            write(i,#186);
            for j := 1 to dimension do
              write(i*j :4);
            writeln;
          end;
      end

    { else it's greater than 15, write an error message }
    else
      writeln('You must give a dimension that''s 15 or lower.');
  end.

If there are any questions as to understanding, or difficulties
in solving the practice problems I pose (only way to improve your

On to the new stuff....

WHILE loops
===========
        It is possible to have a loop to perform a set of
commands a non-set number of times, until a condition is met.  It
can be a contrived one (we can code the basic idea of a FOR loop
using a while loop or the repeat loop. We see this in the recode
of tutorial6 from last time I will make using a while loop instead
of a for loop.

program tutorial7;
  var
    i: integer;
  begin
    writeln('I''m going to write something 10 times.');
    i := 1;
    while i <= 10 do
      begin
        writeln('something', '(Time #':15, i, ')');
        i := i + 1;
      end;
  end.

As we see when we run this program, it produces the same output
as program tutorial6 did.  The statements in the while loop
function while the condition is true.  When i becomes 11, the loop
breaks off and the program ends. Like the IF statements, we can
place multiple conditions by connecting them like before with the
AND or OR identifiers...

REPEAT loops
============
        This is another loop we can use.  The WHILE loop will
function while a condition is true. The REPEAT loop stops fun-
ctioning when a condition is true. We see the idea of this again,
when we reconstruct program tutorial6 using the repeat loop...

program tutorial8;
  var
    i: integer;
  begin
    writeln('I''m going to write something 10 times.');
    i := 1;
    repeat
      writeln('something', '(Time #':15, i, ')');
      i := i + 1;
    until i > 10;
  end.

The programs tutorial6, tutorial7, and tutorial8 perform the same
things, with loops, using different ideas.  The difference of the
while and repeat loops over the for loop is that UNDER NO CIRCUM-
STANCES IS THE INDEX VARIABLE FOR A FOR LOOP TO BE CHANGED WHILE
INSIDE THE LOOP. The conditional variable for a while or repeat
loop can be easily changed as we saw in tutorial7 and tutorial8.
There are many choices and options we can implement with the while
and repeat loops. Menu systems are often implemented like this
Continue until user wants to quit. is the basic logic.).

CASE statement
==============
        As we saw in tutorial5, we may want to make a choice based
on many, multiple options.  This statement is analogous to a series
of IF statements on the same variable.  The CASE statement reduces
the wordiness of such a construct, and makes things easier.  I was
eluding to this statement before when I mentioned then that there
is a better way of doing it.  Well, here it is.  We use the example
of tutorial9 below, which is a rewrite of tutorial5, to illustrate
the use and syntax of a case statement.  Keep in mind that the operator
we use in the case statement (like option below) must be a character
or an integer...

program tutorial9;
  var
    one, two: integer;
    option: char;
  begin
    writeln('Enter an integer.');
    readln(one);
    writeln('Enter another integer.');
    readln(two);
    writeln('Use a mathematical symbol to indicate what you want to do');
    writeln('with these two numbers.');
    readln(option);
    case option of
      '+': begin   { sub procedures can be coded as well }
             writeln(one, ' + ', two, ' = ', one + two, '.');
             writeln('See, I can add.');
           end
      '-': writeln(one, ' - ', two, ' = ', one - two, '.');
      '*': writeln(one, ' * ', two, ' = ', one * two, '.');
      '/': writeln(one, ' / ', two, ' = ', one / two :0:3, '.');
    else {catch rest}
      writeln('Use +, -, *, or / as your operator.  Try again.');
    end; {case includes an implied begin.  We MUST end. }
  end.

As I said in the note, case statements include an implied BEGIN.  We must
say end; to complete the case statement.  The case statement is formatted
for each of our choices above, and the else can be used as a catch-all in
case the user places something in there that we don't account for in the
program, so we can write an error message to the user.  The syntax of the
case statement is basically as above.  You see everything that can be done
with the case statement under Pascal.

Random and Randomize
====================
        We can generate random numbers by doing the following.
At the beginning of the program, call randomize.  Then do
random(<a number>).  What will happen is it will produce an
integer from 0 and less than the number you put in.  Random(3)
will produce a random number from 0-2.  Example.

program tutorial10;
  { write 10 random numbers between 1 and 20 }
  var
    number: integer;
    i: integer;
  begin
    randomize;
    { start the random number generator.  Only call once, but must call! }
    for i := 1 to 10 do
      writeln('Random number (#', i,') = ', random(20) + 1);
      { produce random number from 1-20 instead of 0-19 like random(20) only
        does }
  end.

The Upcase and Length Functions, addressing strings
====================================================

        upcase(char) command will place a letter into uppercase.  This
command is useful for input prompts where the user is asked to give input
that involves a character or a string.  To illustrate the use of this
command, which may be placed in a write statement, or an assign (it is
what is defined as a function.  We will see what that is next time.).
write(upcase('c'));  will produce a C on the screen.  Another example,
which uppercases a string.  We use the function length(string) which is
useful for that purpose.  Given a string into that function, it will
return an integer length of the string.

     for i := 1 to length(inputstring) do
       upcasestring := upcasestring + upcase(inputstring[i]);

This for loop will accomplish it.  The thing we see also, with the illus-
trations of the upcase and length functions, is that we can address any
part of the string by stringname[position in string].  For example, the
string "Charleston" can be there as inputstring.  If we wanted the 5th
character of the string, we say inputstring[5].  inputstring[5] would
be equal to 'l', since it's the 5th character of the string.

Programming Practice Problem Notes
==================================
        I am beginning to see the problems get more difficult as there are
more things we know, and we can do more useful and fun things with our
coding.  You may even want to start setting out and solving simple
mathematical problems for homework, or coding up a simple program that can
figure up your checkbook ... it can be done with what you know now, believe
it or not.  Think about what you can do with your new found knowledge.  Do
not try and overextend yourself trying to do things you have no knowledge of
as of yet.  Attempt this practice programming problem as you hopefully have
the others.  It's a rather fun one, as it's an actual game.  We see we are
coming far and there is a lot farther road to go.  There's lots more
concepts we have to learn, which will enable us to do a whole lot more.
Look forward to several more parts, hopefully...

Practice Programming Problem #3
===============================
        Create a program in Pascal and entirely Pascal that will enable the
user to play a guessing game using the keyboard and the monitor as input and
output.  The points to be addressed in programming this game:

1) The number range of the guessing game must be from 1 to 100.
2) The user must be given 6 opportunities to guess the number.
3) After the user guesses at the number, the program is to answer the
   user by saying whether their guess was high or low and tell them the
   number of guesses they have remaining.
4) If they guess the correct number, give them a congrats message.  If
   they exhaust their attempts, give them a try again message, revealing
   the correct number.
5) Give them the opportunity to play again by asking
...

read more »



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3
Hello Valtar!



 V>                    Turbo Pascal for DOS Beginning Tutorial
 V>                              by Glenn Grotzinger
 V>              Part 3 -- While and Repeat Loops; Case Statements
 V>             all parts copyright 1995-96 (c) by Glenn Grotzinger.

 V>         Hello again.  I haven't gotten much input about continued
 V> interest in this tutorial.  I may discontinue it if the interest
 V> doesn't pick up.

MAybe when you get to more exciting subjects I will catch on.
- graphics
  - pageflipping
  - scrolling
  - fading
- files
  - binary files
  - interfacing with common formats like DBF
- interface
  - mouse
  - dialogs

Not with BGI's, or unit from shareware authors, but from the bottom up. The
whole process - you see, it is not the end product, the unit I want it is the
long way to the unit. The algorithm, the implementation, the do's & the DON'Ts.

 V> Otherwise, I'll assume everybody's dead and I'll quit. :>

Then your compiler would need some patching in the boolean evaluation :)

 V> I am planning on taking this tutorial through all of the Pascal data
 V> structures, and maybe special topics (advanced ones, too, listen up
 V> self-professed Pascal experts -- you may even learn something. :>).

I hope you will touch the subjects I am interested in.

 V>   I
 V> haven't gotten any suggestions on the special topics!!!  Please give

I prefer posting my favourite subjects here in plenum, maybe others will ad to
my list.

Chained lists? Binary trees?

Maybe I should see what actually is in parts 1 and 2, huh?

How about a contest of making the shortest implementation of the Tic-Tac-Toe
game. Two users, using keyboards or mouse. No units from other people, just
yourself and the libraries we've built ourselves or code for this ocasion only.
Huh?

No Assembly Allowed.
Deadline Feb 1?
Posting the binaries to an FTP site?
Checking out the game and the source of others can be good to see how other
went about problems. There has to be a thousand ways to implement such a simple
game.

Regards
Mikkel Breiler



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3

Quote:
>MAybe when you get to more exciting subjects I will catch on.
>- graphics
>  - pageflipping
>  - scrolling
>  - fading
>- files
>  - binary files
>  - interfacing with common formats like DBF
>- interface
>  - mouse
>  - dialogs

basically, my interest in writing this tutorial is to get some of the basics out
there for knowledge for those who are learning.  I find simple "how-to" documentation
for a lot of basic things is either lacking or non-existent.  It makes it real hard
for a private individual to get their foot in the door to programming, with only
reference books (which do not cover structures and their usage all that adequately),
and the TP manuals (those things are nightmares to learn from)... I wanted to start
from NO KNOWLEDGE WHATSOEVER of pascal programming on up to a pretty advanced
level....

from your list:  I will not cover anything that remotely involves anything but use of
pascal structures.  Your graphics requests (graphics w/o use of BGI) involve use of
assembler, which is beyond the scope of this tutorial.  I will cover use of binary
files, and interfacing with a common format (a.k.a. reading and translating a specs
sheet on the format into valid and correct Pascal record structures, which can then
be processed.  I will probably use a mouse interface as an illustration of pascal
access to DOS registers (a.k.a. the registers record type)...

Quote:
>Not with BGI's, or unit from shareware authors, but from the bottom up. The
>whole process - you see, it is not the end product, the unit I want it is the
>long way to the unit. The algorithm, the implementation, the do's & the DON'Ts.

The non-use of BGIs, which I favor, require a reliance on assembler, which is beyond
the scope of this tutorial.  If you wish, though, I have a tutorial here that was
written by Denthor of Asphyxia that covers a lot of use of VGA graphics at assembler
level....

Quote:
>Chained lists? Binary trees?

Chained lists: yes, I planned on covering those from day 1... Binary trees -- sorta
the same concept as chained lists...will cover the concept of what a binary tree is,
but will not devote an awful lot of text to it, since all you need to do is change
the record format from the chained lists and change a few commands, and you have the
operation of a binary tree....

Quote:
>Maybe I should see what actually is in parts 1 and 2, huh?

How bout this...All parts written to this point...even ones not released...I still
will follow the one part a week rule....

Part 1: Basic structure of a Pascal program (basic commands) -- The Hello World type
stuff....
Part 2: If statements, FOR loops, format codes for reading/writing
Part 3: Case statements, while loops, and repeat loops, random and randomize, string
           addressing (length, upcase, addressing parts of a string...)
Part 4: Functions and Procedures
Part 5: Reading and Writing to Text Files...
Part 6 (in progress): Arrays and their usage...

Quote:
>How about a contest of making the shortest implementation of the Tic-Tac-Toe
>game. Two users, using keyboards or mouse. No units from other people, just
>yourself and the libraries we've built ourselves or code for this ocasion only.
>Huh?
>No Assembly Allowed.
>Deadline Feb 1?
>Posting the binaries to an FTP site?
>Checking out the game and the source of others can be good to see how other
>went about problems. There has to be a thousand ways to implement such a simple
>game.

Good idea...Will probably think about it towards the end...Just to see how well
people pick up...Will have to cover a broader focus than that....I'd lean toward a
card game, personally....


Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3

Quote:
>The non-use of BGIs, which I favor, require a reliance on assembler,
which is beyond
>the scope of this tutorial.  If you wish, though, I have a tutorial here
that was
>written by Denthor of Asphyxia that covers a lot of use of VGA graphics
at assembler
>level....

If you wouldn't mind I'd like to get a copy of this - thanks.


Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3

Quote:

> >The non-use of BGIs, which I favor, require a reliance on assembler,
> which is beyond
> >the scope of this tutorial.  If you wish, though, I have a tutorial here
> that was
> >written by Denthor of Asphyxia that covers a lot of use of VGA graphics
> at assembler
> >level....

> If you wouldn't mind I'd like to get a copy of this - thanks.

If you are interested in this sort of area, then try the PCGPE, which includes
the tutorials, plus Assembly tutorials and lots of other information:

ftp://x2ftp.oulu.fi/pub/msdos/programming/gpe/pcgpe.zip
http://x2ftp.oulu.fi/pub/msdos/programming/gpe/00index.html

--
* TQ 1.0 * Stolen Sigs!
Friends come & go, enemies accumulate



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3

Quote:

>If you are interested in this sort of area, then try the PCGPE, which includes
>the tutorials, plus Assembly tutorials and lots of other information:
>ftp://x2ftp.oulu.fi/pub/msdos/programming/gpe/pcgpe.zip
>http://x2ftp.oulu.fi/pub/msdos/programming/gpe/00index.html

I already have provided this person with the VGA tutorial.  It is a very good one
which should answer his questions about the VGA stuff....


Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3

Quote:

>                   Turbo Pascal for DOS Beginning Tutorial
>                             by Glenn Grotzinger
>             Part 3 -- While and Repeat Loops; Case Statements
>            all parts copyright 1995-96 (c) by Glenn Grotzinger.

>        Hello again.  I haven't gotten much input about continued
>interest in this tutorial.  I may discontinue it if the interest
>doesn't pick up.  Otherwise,   <snip>

PLease don't give up.  What you are doing is just what I need.
Many thanks for going to the trouble.

I program in my spare time. Wrote a couple of CP/M programs for the
Amstrad PCW, using CP/M's GSX extension, and HI SOFT's FORTH. They
both sold quite well. I thought BP7 would be a better bet for the
PC. HI SOFT's FORTH plus GSX took up 12k - I find the Mb's of BP7 a
bit overwhelming at the moment and need some plain and simple info.

I would specially value info on graphics, i.e making little diagrams
and moving them around the screen, but any info is well received.

Please carry on the good work.  



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginning Pascal Tutorial Part 3

Quote:

>PLease don't give up.  What you are doing is just what I need.
>Many thanks for going to the trouble.

Thanks.  Are you picking up on the practice programming at the end well?

Quote:
>I would specially value info on graphics, i.e making little diagrams
>and moving them around the screen, but any info is well received.

Text Graphics: Yes  Video Graphics: No.  (involves use of assembler to really
do it right, and that's beyond the scope of the tutorial).

Quote:
>Please carry on the good work.  

I will...


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Beginning Pascal Tutorial Part 7

2. Index of Beginning Pascal Tutorial Parts

3. Beginning Pascal Tutorial Part 21

4. Beginning Pascal Tutorial Part 19 (again)

5. Beginning Pascal Tutorial Part 17

6. Beginning Pascal Tutorial Part 16

7. Beginning Pascal Tutorial Part 15

8. Beginning Pascal Tutorial Part 14

9. Beginning Pascal Tutorial Part 13

10. Beginning Pascal Tutorial Part 12

11. Beginning Pascal Tutorial Part 11

12. Index of Beginning Pascal Tutorial Parts.

 

 
Powered by phpBB® Forum Software