
==== Step 1:
File Name      : MP_1.txt
Author Note    : First Ill give you a few weapons to work with..
Layout         :
                 - basic program outline
                 - variables part 1
                 - constants / typed constants
                 - variables part 2   (contains diagram 1)
                 - arrays part 1
                 - variables part 3
                 - retrospect

Example Source : MP_1.pas
Rating         : ***** (5)
====
Author         : Michael Preslar / E_Maus
This file is (c) copyright Michael Preslar, Elysium Software, 1998.
Please refer back to "intro.txt" for complete documentation of copyrights,
and author information.

Step 1 foreword
---------------
At one time, I taught "basic pascal" classes in the channel #coders on the
irc.sysopnet.org IRC server. This is a log of one of my first (and best)
classes. I have edited it quite a bit.. The original log was 38 pages! This
one isnt but 20. At first, its more of me talking, but eventually, it turns
to discussion. We covered quite a few basics, and most will be of use to you.
I have added some here and there, cleaned up some things, and overall made
this log rather useful.

Sections are labeled for quick reference, but pages are not numbered. sorry.
Things in this step are in the dictionary, which can also be used as a quick
reference. Looking back over this text, I recommend reading over the
dictionary first, even if you dont understand it, and then reading this text.

In the retrospect, I give a quick editor's "redo" of everything I covered
online.


==== In the beginning.. ====

<E_Maus> youll learn.. youll learn.. ;>
<Auzze> Well...what are we learning ????
<E_Maus> auzze as of now, nothing.. shall i teach something?
<Auzze> yes
<E_Maus> what would you recommend?
<Auzze> The Basics
<E_Maus> i know! polymorphism, tangents, and pointers!
<E_Maus> grin
<Auzze> The Basics
<E_Maus> basics..
<E_Maus> hm.. okay..
<E_Maus> Auzze:  what lang? Pascal?
<Auzze> tp
<E_Maus> do you have a compiler?
<Auzze> yes
<E_Maus> alright.. whats your experience with TP? As in, how much do you know?
<Auzze> zip...
<E_Maus> okay, and what kind of programs will you be wanting to do? online,
         highly graphical, major apps, ?
<Auzze> bbs door games and apps
<Sleeper> hehe
<E_Maus> alright. we'll head for online... theres still a lot of basics to
         cover before we get there tho.. soo..
<Auzze> OK..lets do some stuff...

<E_Maus> Hawke:  i take it you have a compiler?
<Hawke> [E_Maus]: Yep...
<E_Maus> Auzze? Do you have a compiler?
<Auzze> Yup..
<E_Maus> Alright.. I ask that because theres no sense in teaching something
         that wont be used. now on to teaching..

<E_Maus> before you can actually start to program, you have to know what the
         program will do. Because of that, and a few other things youll pick
         up along the way, i suggest writing everything down on paper first.
         makes ideas easier to organize, and bugs cant creep up on notebook
         paper.. heres the first program taught by every pascal book,
         professor, help file, etc..



  ========================= Basic Program Outline ==========================


program hithere;

begin
 writeln('Hi there!');
end.


<E_Maus> thats it. if you put it in your compiler, and compile then run it,
         itll print "hi there" to the screen.. yes, i know.. very simple.
         but it shows some syntax that must be followed.

<E_Maus> first, you have to tell the compiler youre doing a program, so you
         put "program <something>" at the top.. at the end of that line,
         youll see a semi-colon.. ; .. almost every pascal line of code has
         to have this line. it tells the compiler that that line is done..
         if'n you dont, the compiler will tell you, and stop compiling.

<E_Maus> easy mistake to make since its a typo, but if youve just finished
         10,000 lines of code, and havent compiled yet, itll piss you off..
         next is the "begin" word. thats rather simple to understand and im
         sure you all can guess it.

<E_Maus> "begin" does not have to have a semi-colon, however most programmers
          still choose to do so, just to be consistant.

<E_Maus> next is writeln('hi there!');
* Sleeper raises his hand, can you put a space in between writeln and (?
<Sleeper> as in writeln ('
<E_Maus> there are quite a few "things" built into TP that you can use like
         this. Once you get deeper into it, youll learn these "things" name.
<E_Maus> Sleeper:  actually, you can.
<E_Maus> writeln itself only tells the compiler to print whatever to the
         screen with a CRLF.. rather simple..
<Hawke> like BASIC's PRINT command...<g>
<E_Maus> but, look at the syntax.. thats a single quote.. ' .. its a
         delimeter for TP.. tells it when to start and stop..
<E_Maus> and of course, the semi-colon.. ; ..
<E_Maus> last is the "end."
<E_Maus> of course, it tells the compiler its done, however - note the
         period.. that tells the compiler that that is the last end in the
         program.

<E_Maus> alright. program #1 done.. any questions?
<Hawke> Nope...<g>
<Sleeper> nopers
<E_Maus> Auzze:  any questions from down under?
<Auzze> Nope....Have a break E-Maus

<smoke smoke, puff puff>

  ============================ Variables Part 1 =============================

<E_Maus> no program is actually useful unless it can somehow "work" with its
         user. to do so, you use "variables".. a symbol that stands for
         something else, and can be changed. before you can use them, you
         have to "declare" them.. IOW, tell the compiler "hey, this is a
         variable!"..

<Hawke> IOW?
<E_Maus> iow = in other words.. heres program #3..

program number3;

var ch:char;

begin
  writeln('Hit a key ..');
  read(ch);
end.


<E_Maus> of course, you do all the regular stuff.. program blah blah..
         then theres "var ch:char;".. this tells the compiler that variable
         "ch" is of type "char", or better known as character.. your actual
         variable name can be anything.. almost. the variable name CANNOT
         start with a number.. but it can be "c1", "ch5", "superguy88"..
         and the name cant have spaces..

<E_Maus> note the syntax.. "var ch:char;".. var tells the compiler "this is
         a variable".. then the name.. "ch".. then a colon.. : .. which says
         "end of name".. then the type.. "char".. then the semi-colon.. ; ..

<E_Maus> next is your program's "begin".. then a simple writeln.. then theres
         "read(ch);".. what this does is reads one character from the
         keyboard. whatever it reads, it puts into ch..

<E_Maus> then the program's last end with a period..

<Sleeper> E_Maus: so a user input kinda thing?
<E_Maus> Sleeper:  right.
<Sleeper> E_Maus: ok, got ya

<E_Maus> but what if you want to write whats in ch to the screen? Easy. watch

program haha;

var ch:char;

begin
  writeln('Hit a key');
  read(ch);
  writeln(ch);
end.

<E_Maus> youve got youre regular stuff.. program.. declare the variable..
         begin.. writeln, and read(ch).. then it gets to something new.
         anyone know what "hardcoded" means?

<Hawke> [E_Maus]: errmmm...kinda.
<E_Maus> Auzze:  how about you? you know what "hard coded" means?
<Auzze> It that where the coding gets hard to do  <g>
<Sleeper> Auzze: haha
<Hawke> hehe....
<E_Maus> hah.. nope. and you dont win the double jeapordy question.. how much
         did you wager? "why i bet all of it Chuck".. well then youre flat
         broke.. <g>
<Hawke> hehe....
<E_Maus> actually, "hardcoded" means you tell the program exactly what to do.
         writeln('hi there'); is an example. youve told the compiler to
         ALWAYS say "hi there"..
<E_Maus> writeln(ch); is not hardcoded. Sooo, you dont need the single quotes
         Actually, theres a better explanation, but ill get to that later.
<Hawke> ahhhh....I get it...kinda.
<Sleeper> whats the diff between both? i dont get it
<E_Maus> what it does is writes whatevers in the variable "ch" to the screen..
<Sleeper> E_Maus: but whats the diff betwee, (ch) and ('hi there')
<Sleeper> E_Maus: im missing something here
<Hawke> [Sleeper]: with (ch) it displays whatever variable ch is set to...
        with ('hi there') it always says hi there, no matter what.
<Sleeper> Hawke: i understand now
<E_Maus> Hawke:  thanks.. proves im actually teaching something <anything?>..
         <g> if'n you put that last proggie in your compiler, compile and run
         it, itll write to the screen whatever key you pressed..
<Sleeper> E_Maus: what about enter ?
<E_Maus> that is also the same thing used in door games when they have a
         <more> prompt or something..
<E_Maus> Sleeper:  well, itll print that to the screen, but you cant read it.
         itll just write a blank line..

  ============================== Constants =================================

<E_Maus> alright.. next is constants.. im sure all of you can tell kinda
         what it is by the name.. constant.. do you all know what i mean when
         i say "declare a variable" ?

<E_Maus> Auzze:  follow me?
<Auzze> yes E_Maus
<E_Maus> Hawke:  how about you?
<Hawke> [E_Maus]: Sure do.
<E_Maus> Sleeper:  and you?
<Sleeper> [E_Maus]: ya
<E_Maus> alright.. to "declare" a constant, youd do a:

         const message = 'Hi there!';

<E_Maus> note the syntax.. anyone notice a difference between this syntax and
         a variable's syntax?
<Hawke> Yeah...the =
<E_Maus> yes, the = is a major difference.

<E_Maus> now heres an example that uses a const, a var, writes "hi there" to
         the screen no matter what key is hit..

program auzzesstuff;

var ch:char;
const message = 'Hi there!';

begin
 writeln('hit a key');
 read(ch);
 writeln(message);
end.

<E_Maus> and there it is. itll wait till a key is hit, and then write
         whatever is in "message".. a few notes about constants.

<Hawke> Does Pascal add anything to it, as BASIC does with input?
<Sleeper> [E_Maus]: another question
<E_Maus> good question.. no it doesnt. Pascal stores it exactly like it was
         entered.
<E_Maus> Sleeper:  yes?
<Sleeper> [E_Maus]: how does the app know what char to look for when pressed?
<E_Maus> Sleeper:  since you havent told it any particular key, itll go for
         any key, but we can change that easily.
<E_Maus> on with notes on constants..
<E_Maus> 1) theyre not called "constants" for nothing. They will not change.
<E_Maus> 2) they can be anything.. of any type. or mixed types.. examples:
         "const message = 'Hi there!';" "const number = '99999999'; " etc..
         whatever you want..
<E_Maus> heres a brief explanation.. when you declare a variable, you say
         "ch" is of "this" type. when you declare a const, you say "message"
         IS "hi there"..
<E_Maus> "const message = 'hi there'; " refers to a regular constant.

  ========================== Typed Constants ===============================

<E_Maus> "const ch : char = 'A'; " is a "typed constant"..
<E_Maus> note the syntax. it looks like a variable, BUT it already has a
         value. everyone follow?
<Hawke> a typed constant?
<Hawke> That whole second part threw me completely...
<E_Maus> yes, a typed constant. it has a "type" (char).. but since its a
         "const" it has a value as well.. " = 'A'; "
<Hawke> OK...I get it...so this is "ch is a char type...defined as A"?
<E_Maus> right..
<Hawke> Cool.
<E_Maus> its basically a variable that already has something in it.

  =========================== Variables Part 2 =============================

<E_Maus> just as much as there are different constants, there are different
         variables. word, integer, longint, shortint, string, char, byte
         and boolean.

<E_Maus> most are variations of number limits.. with the exceptions string,
         char, and boolean. heres an explanation..

<editor's note: This following diagram and information with it was originally
part of the online lesson, however, it wasnt complete, and no where near
as clear as this one is. A good suggestion for this diagram would be to cut
and paste it to an empty text file and print it out for your own reference.
Keep in mind that Ive went ahead and described some things in the diagram
and then saved the parts where i described them online.>

(Diagram 1)

Number Types
------------
name      minimum       maximum    (un)signed  MemSize
-----------------------------------------------------
shortint  -128        / 127         signed    1 byte
integer   -32768      / 32767       signed    2 byte
longint   -2147483648 / 2147483647  signed    4 byte
byte      0           / 255         unsigned  1 byte
word      0           / 65535       unsigned  2 byte
-----------------------------------------------------

name       = name of the variable type
minimum    = the minimum value a variable of this type can have
maximum    = the maximum value a variable of this type can have
(un)signed = is the type signed or unsigned? Signed means it can be
             a negative number.. unsigned must be 0 or higher.
memsize    = the amount of memory used by a variable of this type..

Other Types
-----------
The char type can hold any value of the ASCII table, which contains 256
different characters. Thus, the char type uses 1 byte of memory, just like
the "byte" type.

A string type can hold 255 characters. It uses one byte per character of
memory. Add another byte to let the program save the "length" of the string,
and you have 256. That would be 256 bytes of memory for one string variable.

Quoting Charles Calvert from _Turbo_Pascal_Programming_101_ (a very good
book for beginners).. he describes boolean variables like so:

     "Despite the obscure-sounding name, Boolean variables are one of
     the simplest Pascal types. In fact, you can think of them as Byte
     variables limited in range to either 0 or 1. In these cases, the
     number 0 is associated with false, and the number 1 is associated
     with true."

Therefore, the boolean variable uses 1 byte of memory, and is either
TRUE or FALSE.

(end diagram 1)

<E_Maus> something to watch out for is your types. if you declare a
         "var num:shortint;" and then try to give it a value thats not within
         its range, the programm will cycle the number around as many times
         as it has to to fit the number in its range..

<E_Maus> now on to strings, chars, and booleans.
<E_Maus> a "char" is a character. since youre on SysopNet, you know something
         about bbs's. So i assume youve heard the word "ascii" before..
<E_Maus> does anyone know what "ascii" is, and can anyone define it?
<Kateness> american standard character... ummmmm... nevermind
<E_Maus> anyone else?
<Kateness> it is the trans of characters into numbers that represent them
<E_Maus> Kateness:  kinda. im looking for a more generalized definition..
<E_Maus> ascii is any letter, number or symbol a computer can generate. there
         are 256 in all.. 127 in lower <IOW readable> ascii..
<E_Maus> everyone follow?
<Hawke> Yep... <g>

  ============================= Arrays part 1 =============================

<E_Maus> so everyone knows what a "char" is.. but can anyone tell me what an
         "array" is?
* Kateness raises her hand <g>
<E_Maus> Kateness:  go for it..
<Kateness> an array is to a character what a cube is to a point....
<E_Maus> Kateness:  good analogy.. if anyone else can follow.. <g>
<Sleeper> Kateness: that went past me like a jet
<editor's note: explanation of Kateness's explanation: a cube is actually
a bunch of different points, just as an array is a bunch of different
whatevers. And no. an array isnt limited to just characters.>

<E_Maus> pascal definition of array.. an array <in simple terms> is just a
         collection of like things. a "string" is an array of type "char"..
         i tell you this not to confuse you, but so you will know how to
         think about "strings"..
<Hawke> hmm...I didn't think about that...interesting point.
<E_Maus> a string isnt just one "thing".. its a bunch of little things..
         and thus can be limited. In other words, a variable of string
         type does not have to be a full 255 characters long. if you did

         var s10: string[10];

         the s10 variable would only be able to hold 10 characters, and only
          use 11 bytes of memory.


<Hawke> [E_Maus]: If a limit is not defined, it defaults to 255?
<E_Maus> Hawke:  yup..
<Kateness> are all arrays limited to 255? can you have an array of an array?  255 x255?
<E_Maus> Kateness:  nope, they arent limited. they can be as big as you want.
         as long as you dont try to break the 64k barrier..
<Kateness> so only strings are limited?
<E_Maus> Kateness:  so only predefined types are limited. if you want to
         redefine "string", and can do it, go for it..
<Kateness> EM: in pascal, can you have an array of strings?
<E_Maus> an array of strings? certainly. an array can be of any type..
<Hawke> Well, if a string -is- an array...
<E_Maus> yes, you can have arrays of arrays.
<Kateness> EM: arrays of arrays can get dangerous with the mem thing...

<editor's note: the "mem thing" kateness is refering to here, and the 64k
barrier E_Maus mentioned earlier will be discussed in time. But for now,
dont concern yourself with it.>

<E_Maus> the reason ive not gotten too deep into arrays is because its a
         different type of variable. There are simple vars and complex.. and
         im doing basics for now..

 ============================= Variables part 3 ============================

<E_Maus> ive done all the numbers <word, shortint, integer, longint, and
         byte> , chars, and strings. last of the "simple variables" is
         boolean. theyre really simple.. they use one byte of memory, and
         hold only one value. theyre actually "stored" as 1 and 0, but they
         can be used as true or false. such as: "var tf:boolean; ..
         if tf=true then <blah> "... or.. "if tf=1 then <blah>"..

<Hawke> Why do they take up more than 1 bit, if they're only 1/0?
<E_Maus> Hawke:  Think of the boolean type as the byte type but with a
         different range. The boolean type stores its value the same way the
         byte type does, but you can refer to it differently.
<Hawke> Oh. <g>
<Hawke> so a boolean variable is either true or false?
<Kateness> yes/no
<E_Maus> either (true/1) or (false/0)
<Hawke> so using that previous example, you could also say ..
        if tf=1 then <blah>  ?
<E_Maus> Hawke:  yup.
<Hawke> [E_Maus]: Could you give me an example of the use of it?
<E_Maus> Hawke:  of a boolean?
<Hawke> [E_Maus]: Yes.
<E_Maus> alright. an example for a boolean.. does anyone know anything about
         the if/then loops? If not, dont worry about it. Theyll be covered
         at a later time.
<Hawke> I do...
<E_Maus> alright.. here comes an example..

program blah;

var tf:boolean;
    ch:char;

begin
  writeln('hit a key');
  read(ch);
  if ch=' ' then tf:=true else tf:=false;
  if tf=true then writeln('you hit the space bar') else
   writeln('wrong key bud');
end.

<E_Maus> and there it is
<Hawke> OK...why not use .. if ch=' ' then writeln('you hit the space bar')
        else writeln('wrong key bud');
<Kateness> Hawke: messy
<E_Maus> Hawke:  that would be a better way, but it wouldnt be an example of
         booleans.. <g>
<E_Maus> Kateness:  messy depends on the programmer. not using a bool would
         save a bit of memory <even if it is tiny>, and would simplify the
         code..
<Hawke> [E_Maus]: I was wondering under what circumstances it would be better
        to use the boolean var rather than the second method...
<Kateness> depends on what you are using it for...
<E_Maus> Kateness:  exactly. depends..
<Kateness> boolean is more portable.. easily changed, than is hard coding
<E_Maus> depends is a good word. no matter what programming language, what
         program, or where its used, code always depends on the programmer.
         Its however he wants to do it. His "style".
<Kateness> if you hard code the ' ' & later want to change the true to a
           character... then you would have to go back through & change every
           instance
<E_Maus> if he's more comfortable using an extra variable to keep up with his
         stuff, then so be it. if hed rather not, thats fine too..
<Hawke> OK...I kinda get it...
<E_Maus> Kateness:  change the true to a character?
<Kateness> I meant change the ' ' to a diff character, ie  ',' in your exaple
<E_Maus> Kateness:  id do that by using another variable.. "if ch=checkkey
         then tf:=true".. or something.. but its a small program and can only
         be nitpicked so long.. ;>
<Hawke> hehe...
<Kateness> if it is hard coded rather than a variable used, then you'd have
           to change all instances... can be diff in a big proggy
<Hawke> OMG...programmming theory...eek. <g>
<Kateness> small proggy, hard coding is quicker, big proggy variables are
           more flexible

<Editor's addition: In the above example, you see the line

  if ch=' ' then tf:=true else tf:=false;

What does this do? Since pascal is an easy to read language, you can pretty
much tell what it means. if the variable ch equals a space then set the
variable tf to true else set the variable tf to false. Take note of the
syntax. "=" compares a value, while ":=" sets a value.>

<editor's note: Here the discussion went to programming theory, which is
basically "why programmers do what they do".. Not long after that, the
class was over since everyone wanted to go to bed. It was 3am, ya know.>

  ================================ Retrospect =============================

Heres what we covered in this class. Variables! Many different variables.
And no, I didnt cover every variable known to pascal. We covered:

 - char    - byte   - word    - shortint
 - longint - string - boolean

 and started on arrays. My next lesson will be devoted to arrays. Ill have
 to explain quite a few more advanced, yet easy to learn, topics when
 I cover arrays. Thats why I left them out of this lesson.

 We also covered basic input and output procedures. write, writeln, read,
 and readln. Ill also devote a lesson to IO, as I will with arrays.

 Of course, the very first thing we covered was the basic program outline,
 and its necessities: "begin", "end.", and "program"..

<Author note: Hope this text file helps!>