Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






NOT a boolean operator

The boolean logical operators AND and OR were covered earlier. Strictly speaking, NOT is a boolean operator, and not a function or a procedure. The NOT operator can very useful. Its operand is a condition and its result is BOOLEAN, but the result is the opposite of the operand. If the operand is evaluated as TRUE, then the result of using NOT is FALSE, and vice versa.

  • TRUE goes in, and out comes the value FALSE.
  • FALSE goes in, and out comes the value TRUE.


Code fragment; NOT
In this scenario a data-base is being searched through, for an account number. Initially the boolean variable found is set at false. If the account number exists in the data-base the boolean variable found is set to true, and the rest of the program continues. Here is a piece of code which might follow after the search algorithm.

IF NOT found THEN WRITELN('Your account number was not found') ELSE BEGIN WRITE('Please enter the amount you wish to withdraw : '); READLN(amount); END;


If by the time the program reaches this piece of code, the BOOLEAN variable found is still false, then NOT(found) would be true, and the line:

Your account number was not found


Would be written to the screen, otherwise, the line:

Please enter the amount you wish to withdraw :


Would be written to the screen, and the program would continue.
Here is another small program which might help you to understand the boolean better.

Program 6

PROGRAM boolean_demo(INPUT,OUTPUT); { a small demonstration program showing how to use the boolean facility } BEGIN WRITELN('-------------------------------------------------'); WRITE('The expression 1 + 1 = 3 is : '); WRITELN(1 + 1 = 3); WRITE('The expression a < b is : '); WRITELN('a' < 'b'); WRITE('The expression mouse > elephant is : '); WRITELN('mouse' > 'elephant'); WRITE('The expression 1002340001 < 1002340002 is : '); WRITELN('1002340001' < '1002340002'); WRITE('The expression (1 + 1 = 2) and (3 - 1 = 7) is : '); WRITELN((1 + 1 = 2) AND (3 - 1 = 7)); WRITE('The expression (1 + 1 = 2) or (3 - 1 = 7) is : '); WRITELN((1 + 1 = 2) OR (3 - 1 = 7)); WRITE('The expression not(true) is : '); WRITELN(NOT TRUE); WRITE('The expression not(false) is : '); WRITELN(NOT FALSE); WRITELN; END. { program boolean_demo }


A peripheral point is that the standard function ORD(x), PRED(x) and SUCC(x) will all work on the boolean values TRUE and FALSE. The TRUE and FALSE are values of ordinal types, and there are only two of them. Try adding the lines,

ORD, SUCC and PRED

WRITELN('True is ',ORD(TRUE):0); WRITELN('False is ',ORD(FALSE):0); WRITELN('Successor of false is ',SUCC(FALSE)); WRITELN('Predecessor of true is ',PRED(TRUE));


to the previous program. I don't know to what use you will be able to put this knowledge to, though I think it is something you should be aware of. There are another four standard procedures in Pascal, but these are used in file handling which is the subject of the next chapter. So we will take a closer look at them then. This next program moves into a higher level of programming sophistication. It makes elegant use of both the WHILE loop, and the type boolean. If you look back to the program times_table you will see that idea is roughly the same. However, this program, instead of just writing out the table, asks the user for the answer, and uses this answer to evaluate the boolean result of the identifier correct.
The two lines,



I := I + 1; correct := FALSE;


are essential, the first line increments the value of the variable I ready for the next pass through the WHILE loop. The second line then resets the boolean variable correct back to FALSE, again this is something that must be done for the program to work correctly. Initialising and resetting of booleans within a program causes a lot of problems for beginner programmers, so beware you have been warned.

Program 7

PROGRAM while_multiply(INPUT,OUTPUT); { a more sophisticated version of program four in chapter 2, using both the type boolean and a while loop } VAR I, { loop counter } table, answer : INTEGER; correct : BOOLEAN; BEGIN WRITE('Which multiplication table do you want to do ? '); READLN(table); WRITELN('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); I := 1; WHILE (I <= 12) DO BEGIN WRITE('What does: ',I:2,' * ',table:0,' = '); READLN(answer); correct := (I * table) = answer; { boolean evaluation of answer } IF correct THEN BEGIN WRITELN('Your answer is correct! '); I := I + 1; correct := FALSE; { reset to original state } END ELSE BEGIN WRITE('Your answer is incorrect,'); WRITELN(' Please try again!'); END; END; END. { program while_multiply }


Now this program is a fairly significant jump from the programs you have seen so far. Run it a few times. Enter incorrect answers as well as correct ones. Follow through the program and you should be able to figure it out yourself.
It is probably worth me going over the line

correct := (I * table) = answer;


Let's break that down. Shall we?

  • First correct is a boolean variable.
  • I is a counter variable.
  • Both table and answer are integers which have been input to the program by the user.


This line is fairly straight forward though I admit it doesn't seem like it the first time you see something like it. Ok, I has a value and table has a value. They are multiplied together, and the result is compared over the equality operator = to answer, this comparison has a boolean result, this boolean result is then assigned to correct.

TYPE

The section of a program starting with the reserved word TYPE is the section of the program where a programmer can declare their own types. What are they? Well if you remember way back to the first chapter I gave you a brief description of the elementary types: INTEGER, REAL, CHAR and STRING. Well these are standard types and have already been declared, but because they are built in you can't see their declarations. The type section allows the programmer to declare an identifier as a name for a special data type. The TYPE section must go into the top of the program between the CONST section and the VAR list, note that there is an = (equals sign) between identifier and its type.

Program 8

PROGRAM types_demo(INPUT,OUTPUT); { a program which demonstrates the use of the reserved word type } CONST SIZE = 50; TYPE A_NAME = STRING[SIZE DIV 2]; AN_ADDRESS = STRING[SIZE]; AN_ACCOUNT_NUMBER = INTEGER; A_BALANCE = REAL; VAR customer_name : A_NAME; customer_address : AN_ADDRESS; customer_account_number : AN_ACCOUNT_NUMBER; customer_balance : A_BALANCE;


So what this actually does is to make new types, for this program only, which are called A_NAME, AN_ADDRESS, AN_ACCOUNT_NUMBER and A_BALANCE. These can then be used in the the program in the same way as any of the standard types.

BEGIN WRITE('Enter the customer''s name : '); READLN(customer_name); WRITE('Enter the customer''s address : '); READLN(customer_address); WRITE('Enter the customer''s account number : '); READLN(customer_account_number); WRITE('Enter customer''s account balance : '); READLN(customer_balance); WRITELN; WRITE('The Cotton Bank of Scotland,'); WRITELN(' Customer Statement'); WRITELN('-----------------------'); WRITELN('------------------------'); WRITE('Name : ',customer_name,' | Account Number : '); WRITELN(customer_account_number); WRITELN('Address : ',customer_address); WRITE('Has an account balance of : œ '); WRITELN(customer_balance:6:2); WRITELN('-----------------------'); WRITELN('------------------------'); END. { program types_demo }


There is little in this particular program which really gets any benefit from the use of the TYPE declarations, but at least you have a little understanding of what it is for. The next step is to use this knowledge in using the structured data-type called RECORD.

RECORD

The Pascal type record is a programmer-defined data structure. The type string is also a data structure though it is a standard type, and only its size is defined by the programmer. Different data items are grouped together in this record in an organised, structured way so that they can be kept together as a unit. Concepts in programming, such as data structures are best dealt with by analogy. Just as I said that the REAL, and INTEGER numbers could be thought of as boxes, and the type STRING as a compartmentalised box. The type RECORD too, is open to a very good analogy, that is of the card index. Imagine that you have a card index with the personal details of your friends on each card. It would contain, details for each card such as:

name, address, phone number, age, birthday


You would have the layout of each card structured so that each card in the card index had the same information at the same place.


Figure 1 - Card Index


The type RECORD, has a similar fixed structure, and I find it easiest to visualise it as another compartmentalised box.

<name>
 
<address>
 
<phone number>
 
<age>
 
<birthday>
 

 

A RECORD should be declared in the TYPE section of a program.


Date: 2016-03-03; view: 663


<== previous page | next page ==>
The Standard Procedures | Syntax of the RECORD declaration
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)