The Basic Statements of Pascal

Index

Program
Writeln
Write
Readln
If-Then
If-Then-Else
While
Repeat-Until
For-Do

PROGRAM

Function:

The PROGRAM statement is a required first line of every program.

Syntax:

PROGRAM myname;
---
BEGIN
---
---
END.

Where myname is and identifier made up by the programmer.

Operation:

The PROGRAM statement names the program and marks its beginning.

When the program is run the computer starts performing the statements beginning with the first one after the BEGIN. When the computer reaches the END, the computer stops running the program and goes back to what was happening before the program started.

Notes:

Up to Index


:= (assignment)

The assignment statement is the only statement in Pascal that does not have a key word. It is recognized by the assignment operator ( := )

Function:

Places a value in a storage location. Allows calculations to be performed.

Syntax:

storage location := expression ;

Operation:

  1. Evaluate the expression on the right. That is, find its value, often by doing the calculation indicated.

  2. Erase the current value stored in the variable named on the left.

  3. Store the value of the expression ( from step 1) as the new contents of the variable on the left.

Notes:

Up to Index


WRITELN

Function:

The WRITELN statement tells the computer to write out information for the user to read. This document talks about writing to the screen but WRITELN can also write to a disk file or to a printer.

Syntax:

WRITELN(stuff to be written separated by commas);

Stuff to be written can be of two types:

  1. Things to be written exactly as they appear in the WRITELN statement. These are called literal strings and must begin and end with a single quote mark (the apostrophe) For example:

    WRITELN('this is the start of the program');

    will write:

    this is the start of the program

    note that the single quote marks are not written.

  2. The contents of storage locations. For example:

    value := 5;
    WRITELN(value);

    will write:

    5

  3. A single WRITELN statement can combine both kinds of stuff. For example:

    WRITELN('The number stored in value is' , value)

    will write:

    The number stored in value is 5

Operation:

The WRITELN statement prints the contents of each literal string or variable in turn.

Notes:

Up to Index


WRITE

Function:

The WRITE statement tells the computer to write out information for the user to read. It is almost exactly identical to the WRITELN except that it does not insert a carriage return when it is done writing.

Syntax:

WRITE(stuff to be written separated by commas);

Stuff to be written can be of two types:

  1. Things to be written exactly as they appear in the WRITE statement. These are called literal strings and must begin and end with a single quote mark (the apostrophe) For example:
    WRITE('this is the start of the program');
    will write:
    this is the start of the program
    note that the single quote marks are not written.

  2. The contents of storage locations. For example:
    value := 5;
    WRITE(value);

    will write:
    5

  3. A single WRITE statement can combine both kinds of stuff. For example:
    WRITE('The number stored in value is' , value)
    will write:
    The number stored in value is 5

    Operation:

    Strictly speaking a WRITE command does not write anything. Instead it puts the stuff to be written in a special part of the machine's memory called the write buffer. If there is another WRITE it attaches its stuff to be written to the end of the stuff in the write buffer. Then when a WRITELN occurs it:

    1. writes the contents of the write buffer and erases the contents of the buffer.

    2. writes its own stuff to be written.

    3. inserts a carriage return.

    BUT in most versions of Pascal WRITE actually writes to the screen or the printer without any WRITELN. The only difference is that the cursor is left on the same line as the stuff that was written rather than on the next line. It is only when writing to files that the WRITE may not write untill there is a WRITELN.

    Notes:

    Up to Index


    READLN

    Function:

    The READLN statement tells the computer get information and put it into storage locations. This sectuion talks about reading from the keyboard, but READLN can also from a disk file. The READLN statement and the assignment statement are the only two Pascal statements that can change the contents of a storage location.

    Syntax:

    READLN(names of variables to be filled);

    Operation:

    The READLN displays a flashing cursor and waits for the user to type the appropriate information AND press the carriage return.

    1. READLN erases the current contents of a variable before entering the new value from the user.

    2. If there is more than one variable to be filled, the user must type one or more spaces between each information item to be stored.

    3. If the variable to be filled is a number, the machine will beep if the user attempts to enter anything but a number. This includes commas so that 12,536 must be entered as 12536.

    4. The variables are filled strictly in the order in which they appear in the statement. It is the the programmer's responsibility to see to it that the user enters the information in the correct order.

    Notes:

    Up to Index


    IF - THEN

    Function:

    The IF - THEN statement allows you to have the machine ask a true/false question and if the answer is true, perform one or more statements.

    Syntax:

    IF condition THEN statement;

    Operation:

    1. Evaluate the condition to find out if it is true or false.

    2. If the condition is true, perform the statement following the THEN, and then go on to perform the next statement in the program.

    3. If the condition is false, skip the statement following the THEN, and perform the next statement in the program.

    Notes:

    Up to Index


    IF_THEN_ELSE

    Function:

    The IF_THEN_ELSE is a more powerful form of the IF_THEN statement. It allows you to have the machine ask a true/false question and if the answer is true, perform one or more statements while if the answer is false, it will perform a different statement or set of statements.

    Syntax:

    IF condition THEN statement1
    ELSE statement2;

    Operation:

    1. Evaluate the condition to find out if it is true or false.

    2. If the condition is true, perform the statement following the THEN, skip the statement following the ELSE, and then go on to perform the next statement in the program.

    3. If the condition is false, skip the statement following the THEN, perform the statement following the ELSE, and then go on to perform the next statement in the program.

    Notes:

    Up to Index


    WHILE

    Function:

    Lets us repeat a statement or a group of statements over and over testing a condition before each repitition to see if we should stop. This group of code repeated over and over is called a loop.

    Syntax:

    WHILE condition DO statement;

    Operation:

    1. The condition is tested.

    2. If the condition is true, the statement or statements following the DO are performed and then control returns to the WHILE and step 1) is repeated.

    3. If the condition is false the statement or statements following the DO are skipped and the program continues.

    4. By step 3) it is prefectly possible that the statement or statements following the DO are never done.

    5. It is important that if the condition is true, the statements following the DO do something that will eventually make the condition turn false, otherwise the WHILE repeats forever!

    Notes:

    Up to Index


    REPEAT

    Function:

    Lets us repeat a statement or a group of statements over and over testing a condition after each repitition to see if we should stop. Since the test is done after the statements, the statememts are always performed at least once.

    Syntax:

    REPEAT
    statement(s)
    UNTIL condition;

    Operation:

    1. The statements between the REPEAT and the UNTIL are performed.

    2. The condition followint the UNTIL is tested.

    3. If the condition is false, control passes to the first statement following the REPEAT and the operation continues as step 1).
      If the condition is true, control passes to the statement following the UNTIL.

    Notes:


    Up to Index



    FOR

    Function:

    Repeat a statement or a group of statements a specified number of times. (The FOR statement does a lot of things, so its syntax and operation have a lot of steps.)

    Syntax:

    FOR assignment TO expression DO statement;

    Operation:

    1. Perform the assignment statement.

    2. The TO implies the test:
      control variable > expression
      The true/false question is: "Is the value of the variable on the left of the assignment statement strictly greater than the value of the expression following the TO?"

    3. If the answer to 2) is true then control passes beyond the statement or BEGIN block following the DO. This ends the operation of the FOR statement.

    4. If the answer to 2) is false, then perform the statement or BEGIN block following the DO.

    5. Add one to the control variable. The one is called the incriment. You can think of this as:
      control_variable := control_variable + 1;

    6. Return to step 2) and perform the test.


    Notes:

    Up to Index

    [ Home ][ Vita ][ Copyright  ][ Copyright  ]