The key to successful programming is finding the "right" structure of data and program.
—Niklaus Wirth[1]
After you have learned to use anarray, this chapter introduces you to another data typestructure concept calledrecord.Like anarray, the use ofrecords primarily serves the purposes of allowing you to write clean,structured programs.It is otherwise optional.
You briefly saw arecord in thefirst chapter.While anarray is ahomogenous aggregation of data, that means all members have to have thesame base data type, arecord is potentially, but not necessarily an aggregation of data having variousdifferent data types.[2]
Arecord data type declaration looks pretty much like acollection of variable declarations:
programrecordDemo;type(* a standard line on a text console *)line=string(80);(* 1st grade through 12th grade *)grade=1..12;(* encapsulate all administrative data in one structure *)student=recordfirstname:line;lastname:line;level:grade;end;
The declaration begins with the wordrecord and ends withend.Inbetween you declare fields, ormembers, member elements of the entirerecord.
Allrecord members have to bear distinct nameswithin therecord declaration itself.For instance in the example above, declaringtwo “variables”, member elements of the namelevel will be rejected.
There is no requirement on how many fields you have to declare.An “empty”record is also possible:[fn 1]
typeemptyRecord=recordend;
Similar to thedeclaration of variables you can define multiple fields of thesame data type at once by separating identifiers with a comma.The previous declaration ofsphere could also be written as:
typesphere=recordradius,volume,surface:real;end;
Most Pascal veterans and style guides, however,discourage the use of this shorthand notation (both for variable as well asrecord declarations, but also in formal parameter lists).Itis only reasonable if all declared identifiersabsolutely always have same data type;it is virtually guaranteed you will never want to change the data type ofjust one field in the comma-separated list.If in doubt, use the longhand.In programming, convenience plays a tangential role.
By declaring arecord variable you immediately have theentire set of “sub”‑variables at your hand.Accessing them is done by specifying therecord variable’s name, plus a dot (.), followed by therecordfield’s name:
varposterStudent:student;beginposterStudent.firstname:='Holden';posterStudent.lastname:='Caulfield';posterStudent.level:=10;end.
You already saw the dot notation in the previous chapter onstrings, where appending.capacity on a name of astring(…) variable refers to the respective variable’s character capacity.This is not a coincidence.
programdotNoGo(output);{ This program does not compile. }typeline=string(80);quizItem=recordquestion:line;answer:line;end;varresponse:line;challenge:quizItem;beginwriteLn(line.capacity);{ ↯ `line` is not a variable }writeLn(response.capacity);{ ✔ correct }writeLn(quizItem.question);{ ↯ `quizItem` refers to a data type }{ Data type declarations (as per definition) do not reserve any memory }{ thus you cannot “read/write” from/to a data type. }writeLn(challenge.question);{ ✔ correct }end.
.) notation is only validif there is memory.[fn 2]But why and when do wewant to use arecord?At first glance and in the given examples so far it may seem like a troublesome way to declare and usemultiple variables.Yet the fact that arecord is handled asone unit entails one big advantage:
record values via a simple assignment (:=).record can be a parameter of routines, and inEP functions can return them as well.[fn 3]Evidently you want togroup data together that always appeartogether.It doesnot make sense to groupunrelated data, just because we can.Another quite useful advantage is presented below in the section onvariant records.
As you saw earlier, referring to members of arecord can get a little tedious, because we are repeating the variable name over and over again.Fortunately, Pascal allows us abbreviate things a bit.
With-clauseThewith-clause allows us to eliminate repeating a common prefix, specifically the name of arecord variable.[3]
beginwithposterStudentdobeginfirstname:='Holden';lastname:='Caulfield';level:=10;end;end.
All identifiers that identify values are first looked for in therecord scope ofposterStudent.If there is no match, all variable identifiers outside of the givenrecord are considered too.
Of course it is still possible to denote arecord member by itsfull name.E. g. in the source code above it would be perfectly legal to still writeposterStudent.levelwithin thewith-clause.Concededly, this would defeat the purpose of thewith-clause, but sometimes it may still be beneficial to emphasize the specificrecord variable just for documentation.It is nevertheless important to understand that the FQI, the fully-qualified identifier, the one with a dot in it, doesnot lose its “validity”.
In principle, all components of structured values “containing dots” can be abbreviated withwith.This is also true for thedata typestring you have learned in the previous chapter.
programwithDemo(input,output);type{ Deutsche Post „Maxi-Telegramm“ }telegram=string(480);varpost:telegram;beginwithpostdobeginwriteLn('Enter your telegram. ','Maximum length = ',capacity,' characters.');readLn(post);{ … }end;end.
Here, within thewith-clausecapacity, and for that matterpost.capacity, refer topost.capacity.
If multiplewith-clauses ought to be nested, there is the short notation:
withsnakeOil,sharpToolsdobegin…end;
which is equivalent to:
withsnakeOildobeginwithsharpToolsdobegin…end;end;
It is important to bear in mind, first identifiers insharpTools are searched, and if there is no match,secondly, identifiers insnakeOil are considered.
In Pascal arecord is the only data type structure concept that allows you to, so to speak, alter its structure during run-time,while aprogram is running.This super practical property ofrecord permits us to write versatile code covering many cases.
Let’s take a look at an example:
typecentimeter=10..199;// order of female, male has been chosen, so `ord(sex)`// returns the [minimum] number of non-defective Y chromosomessex=(female,male)// measurements according EN 13402 size designation of clothes [incomplete]clothingSize=recordshoulderWidth:centimeter;armLength:centimeter;bustGirth:centimeter;waistSize:centimeter;hipMeasurement:centimeter;casebody:sexoffemale:(underbustMeasure:centimeter;);male:();end;
Thevariant part of arecord starts with the keywordcase, which you already know fromselections.After that follows arecord member declaration, thevariant selector, but instead of a semicolon you put the keywordof thereafter.Below that follow all possible variants.Each variant is marked by a value out of the variant selector’s domain, herefemale andmale.Separated by a colon (:) follows avariant denoter surrounded by parentheses.Here you can list additionalrecord members that are only available if a certain variant is “active”.Note thatall identifiers acrossall alternatives must be unique.The individual variants are separated by a semicolons, and there can beat most one variant part which has to appearat the end.Because you will need to be able to list all possible variants, the variant selector has to be an ordinal data type.
Using variant records requires you to firstselect a variant.Variants are “activated” by assigning a value to the variant selector.Note, variants are not “created”; they all already exist atprogram startup.You merely need to make a choice.
boobarella.body:=female;boobarella.underbustMeasure:=69;
Onlyafter assigning a value to the variant selector andas long as this value remainsunchanged, you are allowed to access any fields of the respective variant.It is illegal to reverse the previous two lines of code and attempt accessing theunderbustMeasure field even thoughbody is not defined yet and, more importantly, does not bear the valuefemale.
It is certainly permissible to change the variant selector later in yourprogram and then use a different variant, but all previously stored values in the variant partrelinquish their validity and you cannot restore them.If you switch back the variant to a previous, original value, you will need to assign all values in that variant anew.
This concept opens up new horizons:You can design your programs more interactively in a neat fashion.You can now choose a variant based on run-time data (data that is readwhile theprogram is running).Because at any time (after the first assignment of a value to the variant selector) onlyone variant is “active”, yourprogram will crash if it attempts reading/writing values of an “inactive” variant.This is adesirable behavior, becausethat is the whole idea of having distinct variants.It guarantees your programs overall integrity.
Pascal also permits having anonymous variant selectors, that is selectors not bearing any name.The implications are
“But wasn’t this the object of the exercise?” you might ask.Yes, indeed, since there is no named selector yourprogram cannot keep track which variant is supposed to work and which one is “defective”.You are responsible to determine which variant you can sensibly read/write at present.
| See also the chapterType conversion inComputer Programming. |
programanonymousVariantsDemo(output);typebitIndex=0..(sizeOf(integer)*8-1);exposedInteger=recordcaseBooleanoffalse:(value:integer;);true:(bit:setofbitIndex;);end;vari:exposedInteger;begini.bit:=[4];writeLn(i.value);end.
16
16 is (and this should be considered “a coincidence”). Westress that all Pascal standards do not make any statement regarding internal memory structure. A high-level programming language is not concerned about how data is stored, it even does not know the notion of “bits”, “voltage high”/“voltage low”.GPC and theFPC in their default configurations. Do not deem it as “Pascal”, but a descendant of it. There is a good chance that using a different compiler will produce different results.This concept exists in many other programming languages too.In the programming language C, for instance, it is called aunion.
So far we have been exclusively using countingloops.This is great if you can predict in advance the number of iterations, how many times the loop’s body needs to be executed.Yet every so often it is not possible to formulate a proper expression determining the number of iterations in advance.
Conditional loops allow you to make the execution of thenext iteration dependent on aBoolean expression.They come in two flavors:
The difference is, the loop’s body of a tail-controlled loop is executedat least once in any case, whereas a head-controlled loop might never execute the loop body at all.In either case, a condition is evaluated over and over again and mustuphold for the loop to continue.
A head-controlled loop is frequently calledwhile-loop because of its syntax.
programcharacterCount(input,output);typeintegerNonNegative=0..maxInt;varc:char;n:integerNonNegative;beginn:=0;whilenotEOFdobeginread(c);n:=n+1;end;writeLn('There are ',n:1,' characters.');end.
$ cat ./characterCount.pas | ./characterCountThere are 240 characters.$ printf '' '' | ./characterCountThere are 0 characters.
Boolean expression framed by the wordswhile anddo. The condition must evaluate totrue for any (subsequent) iteration to occur.As you can see from the output, in the second case, it may even bezero times: Evidently forempty inputn:=n+1 wasnever executed.EOF is shorthand forEOF(input).This standardfunction returnstrue if there is no further data available to read, commonly calledend of file.It is illegal, and will horribly fail, toread from a file if the respectiveEOF function call returnstrue.
Unlike a counting loop, youare allowed tomodify data the conditional loop’s condition depends on.
const(* instead of a hard-coded length `64` *)(* you can write `sizeOf(integer) * 8` in Delphi, FPC, GPC *)wordWidth=64;typeintegerNonNegative=0..maxInt;wordStringIndex=1..wordWidth;wordString=array[wordStringIndex]ofchar;functionbinaryString(n:integerNonNegative):wordString;var(* temporary result *)binary:wordString;i:wordStringIndex;begin(* initialize `binary` with blanks *)fori:=1towordWidthdobeginbinary[i]:=' ';end;(* if n _is_ zero, the loop's body won't be executed *)binary[i]:='0';(* reverse Horner's scheme *)whilen>=1dobeginbinary[i]:=chr(ord('0')+nmod2);n:=ndiv2;i:=i-1;end;binaryString:=binary;end;
Then the loop’s condition depends on will be repeatedly divided by two.Because the division operator is an integer division (div), at some point the value1 will be divided by two and the arithmetically correct result0.5 is truncated (trunc) toward zero.Yet the value0 does not satisfy the loop’s condition anymore, thus there will not beany subsequent iterations.
In a tail-controlled loop the condition appearsbelow the loop’s body, at the foot.The loop’s body is always run once before even the condition is evaluated at all.
programrepeatDemo(input,output);vari:integer;beginrepeatbeginwrite('Enter a positive number: ');readLn(i);enduntili>0;writeLn('Wow! ',i:1,' is a quite positive number.');end.
The loop’s body is encapsulated by the keywordsrepeat anduntil.[fn 4]Afteruntil follows aBoolean expression.In contrast to awhile loop, the tail-controlled loop always continues, always keeps going,until the specified condition becomestrue.A true condition marksthe end.In the above example the user will be prompted again and again until he eventually complies and enters a positive number.
This section introduces you to features of Extended Pascal as defined in theISO standard 10206.You will need anEP‑compliant compiler to use those features.
InEP there is astandard data type calledtimeStamp.It is declared as follows:[fn 5]
typetimeStamp=recorddateValid:Boolean;timeValid:Boolean;year:integer;month:1..12;day:1..31;hour:0..23;minute:0..59;second:0..59;end;
As you can see from the declaration,timeStamp also contains data fields for a calendar date, not just the time as indicated by a standard clock.
A processor (i. e. usually a compiler) may provideadditional (thus non-standard) fields. TheGPC for instance supplies, among other fields, a field calledtimeZone indicating the offset in seconds versusUTC (“world time”). |
EP also defines a unaryprocedure that populates atimeStamp variable with values.GetTimeStamp assigns values to all members of atimeStamprecord passed in the first (and only) parameter.These values represent the “current date” and “current time” as at the invocation of this procedure.However, in the 1980’s not all (personal/home) computers did have a built-in “real time” clock.Therefore, theISO standard 10206 devised prior 21st century stated that the word “current” was “implementation-defined”.ThedateValid andtimeValid fields were specifically inserted to address the issue that some computers simply do not know the current date and/or time.When reading values from atimeStamp variable, it is still advisable to check their validity first after havinggetTimeStamp fill them out.
IfgetTimeStamp wasunable to obtain a “valid” value, it will set
day,month andyear to a value representingJanuary 1, 1CE, but alsodateValid tofalse.hour,minute andsecond become all0, a value representingmidnight. ThetimeValid field becomesfalse.Both are independent from each other, so it may certainly be the case that just the time could be determined, but the date is invalid.
Note that the Gregorian calendar was introduced during the year 1582 CE, so thetimeStamp data type is generally useless for any dates before 1583 CE.
Having obtained atimeStamp,EP furthermore supplies two unary functions:
date returns a human-readablestring representation ofday,month andyear, andtime returns a human-readablestring representation ofhour,minute andsecond.Both functions will fail and terminate theprogram ifdateValid ortimeValid indicate an invalid datum respectively.Note, the exact format ofstring representation is not defined by theISO standard 10206.
program:programdateAndTimeFun(output);varts:timeStamp;begingetTimeStamp(ts);ifts.dateValidthenbeginwriteLn('Today is ',date(ts),'.');end;ifts.timeValidthenbeginwriteLn('Now it is ',time(ts),'.');end;end.
Today is 11 Nov 2025.Now it is 08:06:42.
dateValid andtimeValid arefalse.This is a good time to take inventory and reiterate all kinds of loops.
Conditional loops are the tools of choice if you cannot predict the total number of iterations.
| head-controlled loop | tail-controlled loop |
|---|---|
whileconditiondobegin…end; | repeatbegin…enduntilcondition; |
condition must evaluate totrue for any (including subsequent) iterations to occur. | condition must befalse for anysubsequent iteration to occur. |
Itis possible to formulate either loop as the other one, but usually one of them is more suitable.A tail-controlled loop is particularly suitable if you do not have any data yet to make a judgment, to evaluate a propercondition prior the first iteration.
Counting loops are good if youcan predict the total number of iterations before entering the loop.
| counting up loop | counting down loop |
|---|---|
forcontrolVariable:=initialValuetofinalValuedobegin…end; | forcontrolVariable:=initialValuedowntofinalValuedobegin…end; |
After each non-final iterationcontrolVariable becomessucc(controlVariable).controlVariable must be less than or equal tofinalValue for another iteration to occur. | After each non-final iterationcontrolVariable becomespred(controlVariable).controlVariable must begreater than or equal tofinalValue for another iteration to occur. |
Both, theinitialValue andfinalValue expressions, are evaluated exactlyonce.[4] This is very different to conditional loops. |
Inside counting loops’ bodies you cannot modify the counting variable, only read it.This prevents you from any accidental manipulations and ensures the calculated predicted total number of iterations will indeed occur.
If you are using anEP-compliant compiler, you furthermore have the option to use afor…in loop onsets.
programforInDemo(output);typecharacters=setofchar;varc:char;parties:characters;beginparties:=['R','D'];forcinpartiesdobeginwrite(c:2);end;writeLn;end.
You have made it this far, and it is quite impressive how much you already know.Since this chapter’s concept of arecord should not be too difficult to grasp, the following exercises mainly focus on training.A professional computer programmer spends most of his time onthinking what kind of implementation, usingwhich tools (e. g.array “vs.”set), is the most useful/reasonable.You are encouraged to think first, before you even start typing anything.Nonetheless, sometimes (esp. due to your lack of experience) you need to just try things out, which is fine if it isintentional.Aimlessly finding a solution does not discern an actual programmer.
record contain anotherrecord?array to containanotherarray, this is quite possible for arecord too. Write a testprogram to see for yourself. The important thing is to note that the dot-notation can be expanded indefinitely (myRecordVariable.topRecordFieldName.nestedRecordFieldName.doubleNestedRecordFieldName). Evidently at some point it becomes too difficult to read so use this wisely.array to containanotherarray, this is quite possible for arecord too. Write a testprogram to see for yourself. The important thing is to note that the dot-notation can be expanded indefinitely (myRecordVariable.topRecordFieldName.nestedRecordFieldName.doubleNestedRecordFieldName). Evidently at some point it becomes too difficult to read so use this wisely.whiletruedobegin…end;
The condidition needs to be negated in arepeat…until loop:
repeatbegin…enduntilfalse;
true, or expressions that can never be fulfilled (in the case of arepeat…until loop), are not. For instance, given thati was aninteger the loopwhilei<=maxIntdo will run indefinitely, becausei can never exceedmaxInt[fn 6] and thus break the loop’s condition. Therefore be reminded to carefully formulate expressions for conditional loops and ensure it will eventually reach a terminating state. Otherwise it can be frustrating for the user of yourprogram.whiletruedobegin…end;
The condidition needs to be negated in arepeat…until loop:
repeatbegin…enduntilfalse;
true, or expressions that can never be fulfilled (in the case of arepeat…until loop), are not. For instance, given thati was aninteger the loopwhilei<=maxIntdo will run indefinitely, becausei can never exceedmaxInt[fn 6] and thus break the loop’s condition. Therefore be reminded to carefully formulate expressions for conditional loops and ensure it will eventually reach a terminating state. Otherwise it can be frustrating for the user of yourprogram.while-loop:repeatbeginimagineJumpingSheep;sheepCount:=sheepCount+1;waitTwoSeconds;enduntilasleep;
while-loop even begins:imagineJumpingSheep;sheepCount:=sheepCount+1;waitTwoSeconds;whilenotasleepdobeginimagineJumpingSheep;sheepCount:=sheepCount+1;waitTwoSeconds;end;
repeat…until-loop is more suitable in this case.while-loop even begins:imagineJumpingSheep;sheepCount:=sheepCount+1;waitTwoSeconds;whilenotasleepdobeginimagineJumpingSheep;sheepCount:=sheepCount+1;waitTwoSeconds;end;
repeat…until-loop is more suitable in this case.program that takes the output of the commandgetentpasswd as input and only prints thefirst field/column of every line. In apasswd(5) file, fields are separated by a colon (:). Yourprogram will list all known user names.getentpasswd|./cut1 (the file name of your executable program may differ).programcut1(input,output);constseparator=':';varline:string(80);beginwhilenotEOF(input)dobegin{ This reads the _complete_ line, but at most}{ line.capacity characters are actually saved. }readLn(line);writeLn(line[1..index(line,separator)-1]);end;end.
index will return the index of the colon character which you do not want to print, thus you will need to subtract 1 from its result. Thisprogram will evidently fail if a line does not contain a colon.getentpasswd|./cut1 (the file name of your executable program may differ).programcut1(input,output);constseparator=':';varline:string(80);beginwhilenotEOF(input)dobegin{ This reads the _complete_ line, but at most}{ line.capacity characters are actually saved. }readLn(line);writeLn(line[1..index(line,separator)-1]);end;end.
index will return the index of the colon character which you do not want to print, thus you will need to subtract 1 from its result. Thisprogram will evidently fail if a line does not contain a colon.program so only user names whoseUID is greater than or equal to1000. TheUID is stored in the third field.programcut2(input,output);constseparator=':';minimumID=1000;varline:string(80);nameFinalCharacter:integer;uid:integer;beginwhilenotEOFdobeginreadLn(line);nameFinalCharacter:=index(line,separator)-1;{ username:encryptedpassword:usernumber:… }{ ↑ `nameFinalCharacter + 1` }{ ↑ `… + 2` is the index of the 1st password character }uid:=index(subStr(line,nameFinalCharacter+2),separator);{ Note that the preceding `index` did not operate on `line` }{ but an altered/different/independent “copy” of it. }{ This means, we’ll need to offset the returned index once again. }readStr(subStr(line,nameFinalCharacter+2+uid),uid);{ Read/readLn/readStr automatically terminate reading an integer }{ number from the source if a non-digit character is encountered. }{ (Preceding blanks/space characters are ignored and }{ the _first_ character still may be a sign, that is `+` or `-`.)}ifuid>=minimumIDthenbeginwriteLn(line[1..nameFinalCharacter]);end;end;end.
subStr can be omitted effectively meaning “give methe rest of astring.” Note that this programming task mimics (some of) the behavior ofcut(1).Use programs/source code that has already been programmedfor you whenever possible. Reinventing the wheel is not necessary. Nonetheless, this basic task is a good exercise. On aRHEL system you may rather want to setminimumID to500.programcut2(input,output);constseparator=':';minimumID=1000;varline:string(80);nameFinalCharacter:integer;uid:integer;beginwhilenotEOFdobeginreadLn(line);nameFinalCharacter:=index(line,separator)-1;{ username:encryptedpassword:usernumber:… }{ ↑ `nameFinalCharacter + 1` }{ ↑ `… + 2` is the index of the 1st password character }uid:=index(subStr(line,nameFinalCharacter+2),separator);{ Note that the preceding `index` did not operate on `line` }{ but an altered/different/independent “copy” of it. }{ This means, we’ll need to offset the returned index once again. }readStr(subStr(line,nameFinalCharacter+2+uid),uid);{ Read/readLn/readStr automatically terminate reading an integer }{ number from the source if a non-digit character is encountered. }{ (Preceding blanks/space characters are ignored and }{ the _first_ character still may be a sign, that is `+` or `-`.)}ifuid>=minimumIDthenbeginwriteLn(line[1..nameFinalCharacter]);end;end;end.
subStr can be omitted effectively meaning “give methe rest of astring.” Note that this programming task mimics (some of) the behavior ofcut(1).Use programs/source code that has already been programmedfor you whenever possible. Reinventing the wheel is not necessary. Nonetheless, this basic task is a good exercise. On aRHEL system you may rather want to setminimumID to500.| See also the chapterSieve of Eratosthenes inDiscrete Mathematics. |
program meets all requirements. Note, an implementation using anarray[1..limit]ofBoolean would have been perfectly fine as well, although the shownsetofnatural implementation isin principle preferred.programeratosthenes(output);type{ in Delphi or FPC you will need to write 1..255 }natural=1..4095;{$setLimit 4096}{ only in GPC }naturals=setofnatural;const{ `high` is a Borland Pascal (BP) extension. }{ It is available in Delphi, FPC and GPC. }limit=high(natural);{ Note: It is important that `primes` is declared }{ in front of `sieve` and `list`, so both of these }{ routines can access the _same_ variable. }varprimes:naturals;{ This procedure sieves the `primes` set. }{ The `primes` set needs to be fully populated }{ _before_ calling this routine. }proceduresieve;varn:natural;i:integer;multiples:naturals;begin{ `1` is by definition not a prime number }primes:=primes-[1];{ find the next non-crossed number }forn:=2tolimitdobeginifninprimesthenbeginmultiples:=[];{ We do _not_ want to remove 1 * n. }i:=2*n;whileiin[n..limit]dobeginmultiples:=multiples+[i];i:=i+n;end;primes:=primes-multiples;end;end;end;{ This procedures lists all numbers in `primes` }{ and enumerates them. }procedurelist;varcount,n:natural;begincount:=1;forn:=2tolimitdobeginifninprimesthenbeginwriteLn(count:8,'.:',n:22);count:=count+1;end;end;end;{ === MAIN program === }beginprimes:=[1..limit];sieve;list;end.
sieve task from thelist task, both routine definitions and the main part of theprogram at the bottom remain quite short and are thus easier to understand.program meets all requirements. Note, an implementation using anarray[1..limit]ofBoolean would have been perfectly fine as well, although the shownsetofnatural implementation isin principle preferred.programeratosthenes(output);type{ in Delphi or FPC you will need to write 1..255 }natural=1..4095;{$setLimit 4096}{ only in GPC }naturals=setofnatural;const{ `high` is a Borland Pascal (BP) extension. }{ It is available in Delphi, FPC and GPC. }limit=high(natural);{ Note: It is important that `primes` is declared }{ in front of `sieve` and `list`, so both of these }{ routines can access the _same_ variable. }varprimes:naturals;{ This procedure sieves the `primes` set. }{ The `primes` set needs to be fully populated }{ _before_ calling this routine. }proceduresieve;varn:natural;i:integer;multiples:naturals;begin{ `1` is by definition not a prime number }primes:=primes-[1];{ find the next non-crossed number }forn:=2tolimitdobeginifninprimesthenbeginmultiples:=[];{ We do _not_ want to remove 1 * n. }i:=2*n;whileiin[n..limit]dobeginmultiples:=multiples+[i];i:=i+n;end;primes:=primes-multiples;end;end;end;{ This procedures lists all numbers in `primes` }{ and enumerates them. }procedurelist;varcount,n:natural;begincount:=1;forn:=2tolimitdobeginifninprimesthenbeginwriteLn(count:8,'.:',n:22);count:=count+1;end;end;end;{ === MAIN program === }beginprimes:=[1..limit];sieve;list;end.
sieve task from thelist task, both routine definitions and the main part of theprogram at the bottom remain quite short and are thus easier to understand.program that reads an infinite number of numerical values frominput and at the end prints onoutput the arithmetic mean.programarithmeticMean(input,output);typeintegerNonNegative=0..maxInt;vari,sum:real;count:integerNonNegative;beginsum:=0.0;count:=0;whilenoteof(input)dobeginreadLn(i);sum:=sum+i;count:=count+1;end;{ count > 0: do not do division by zero. }ifcount>0thenbeginwriteLn(sum/count);end;end.
Note that using a datatype excludingnegative numbers (here we named itintegerNonNegative) mitigates the issue thatcount may flip the sign, a condition known asoverflow. This would cause theprogram to fail ifcount:=count+1 became too large, and effectively falls out of the range0..maxInt.
maxReal, no programmatic way to tell thatsum became too large or too small rendering it severely inaccurate, becauseany value ofsum may be legit nevertheless.programarithmeticMean(input,output);typeintegerNonNegative=0..maxInt;vari,sum:real;count:integerNonNegative;beginsum:=0.0;count:=0;whilenoteof(input)dobeginreadLn(i);sum:=sum+i;count:=count+1;end;{ count > 0: do not do division by zero. }ifcount>0thenbeginwriteLn(sum/count);end;end.
Note that using a datatype excludingnegative numbers (here we named itintegerNonNegative) mitigates the issue thatcount may flip the sign, a condition known asoverflow. This would cause theprogram to fail ifcount:=count+1 became too large, and effectively falls out of the range0..maxInt.
maxReal, no programmatic way to tell thatsum became too large or too small rendering it severely inaccurate, becauseany value ofsum may be legit nevertheless.timefunction that returns astring in the “American” time format9:04 PM. This may look easy at first, but it can become quite a challenge. Have fun!time itself. However, the output oftime itself isnot standardized, so we will need to define everything by ourselves:typetimePrint=string(8);functiontimeAmerican(ts:timeStamp):timePrint;consthourMinuteSeparator=':';anteMeridiemAbbreviation='AM';postMeridiemAbbreviation='PM';typenoonRelation=(beforeNoon,afterNoon);letterPair=string(2);var{ contains 'AM' and 'PM' accessible via an index }m:array[noonRelation]ofletterPair;{ contains a leading zero accessible via a Boolean expression }z:array[Boolean]ofletterPair;{ holds temporary result }t:timePrint;begin{ fill `t` with spaces }writeStr(t,'':t.capacity);
This fallback value (in the casets.timeValid isfalse) allows the programmer/“user” of thisfunction to “blindly” print its return value. There will be anoticeable gap in the output. Another sensible “fallback” value would be an emptystring.
withtsdobeginiftimeValidthenbeginm[beforeNoon]:=anteMeridiemAbbreviation;m[afterNoon]:=postMeridiemAbbreviation;z[false]:='';z[true]:='0';writeStr(t,((hour+12*ord(hour=0)-12*ord(hour>12))mod13):1,hourMinuteSeparator,z[minute<10],minute:1,' ',m[succ(beforeNoon,hourdiv12)]);
This is the most complicated part of this problem. First of all, all number parameters towriteStr areexplicitly suffixed with:1 as the minimum-width specification, because there aresome compilers that would otherwise assume, for example,:20 as a default value. Since we know thattimeStamp.hour is in the range0..23 we can use thediv andmod operations as demonstrated. However, we will need account of anhour value of0, which is usually denoted as 12:00 AM (andnot zero). A conditional “shift” by 12 using the shownBoolean expression andord “fixes” this. Furthermore, here is a brief reminder that inEP thesucc function accepts a second parameter.
end;end;timeAmerican:=t;end;
time itself. However, the output oftime itself isnot standardized, so we will need to define everything by ourselves:typetimePrint=string(8);functiontimeAmerican(ts:timeStamp):timePrint;consthourMinuteSeparator=':';anteMeridiemAbbreviation='AM';postMeridiemAbbreviation='PM';typenoonRelation=(beforeNoon,afterNoon);letterPair=string(2);var{ contains 'AM' and 'PM' accessible via an index }m:array[noonRelation]ofletterPair;{ contains a leading zero accessible via a Boolean expression }z:array[Boolean]ofletterPair;{ holds temporary result }t:timePrint;begin{ fill `t` with spaces }writeStr(t,'':t.capacity);
This fallback value (in the casets.timeValid isfalse) allows the programmer/“user” of thisfunction to “blindly” print its return value. There will be anoticeable gap in the output. Another sensible “fallback” value would be an emptystring.
withtsdobeginiftimeValidthenbeginm[beforeNoon]:=anteMeridiemAbbreviation;m[afterNoon]:=postMeridiemAbbreviation;z[false]:='';z[true]:='0';writeStr(t,((hour+12*ord(hour=0)-12*ord(hour>12))mod13):1,hourMinuteSeparator,z[minute<10],minute:1,' ',m[succ(beforeNoon,hourdiv12)]);
This is the most complicated part of this problem. First of all, all number parameters towriteStr areexplicitly suffixed with:1 as the minimum-width specification, because there aresome compilers that would otherwise assume, for example,:20 as a default value. Since we know thattimeStamp.hour is in the range0..23 we can use thediv andmod operations as demonstrated. However, we will need account of anhour value of0, which is usually denoted as 12:00 AM (andnot zero). A conditional “shift” by 12 using the shownBoolean expression andord “fixes” this. Furthermore, here is a brief reminder that inEP thesucc function accepts a second parameter.
end;end;timeAmerican:=t;end;
Sources:
record Type".Oh! Pascal! (third edition ed.). p. 374.ISBN 0-393-96077-3.[…] records have two unique aspects:
First, the stored values can have different types. This makes records potentiallyheterogeneous—composed of values of different kinds. Arrays, in contrast, hold values of just one type, so they're said to behomogeneous.
[…]
{{cite book}}:|edition= has extra text (help);line feed character in|quote= at position 269 (help);syntaxhighlight stripmarker in|chapter= at position 17 (help)Within the component statement of the with statement, the components (fields) of the record variable specified by the with clause can be denoted by their field identifier only, i.e. without preceding them with the denotation of the entire record variable.
{{cite book}}:Check date values in:|date= (help)The initial and final values are evaluated only once.
Notes:
record will not be able to store anything. In thenext chapter you will learn a (and the only) instance it could be useful.function can only return “simple data type” and “pointer data type” values.begin…end is redundant sincerepeat…until constitute a frame in their own right. For pedagogical reasons we teach you to always usebegin…end nevertheless wherever asequence of statements usually appears. Otherwise you might change yourrepeat…until loop to awhile…do loopforgetting to surround the loop’s body statements with a properbegin…end frame.packed designation has been omitted for simplicity.maxInt. TheISO standards merely require, that all arithmetic operations in the interval-maxInt..maxInt work absolutely correct, but it is thinkable (although unlikely) that more values are supported.