docs: add more info about sets and for-in loop
This commit is contained in:
parent
573b28bd21
commit
796d8f8e5f
2 changed files with 57 additions and 3 deletions
|
|
@ -20,7 +20,38 @@ Note that the **char** type is a 32-bit type, and strings contain bytes. Non-ASC
|
|||
When indexing a string, the result is a **char** type with bits 31 to 8 set to zero. When assigning a **char** to an indexed string element, the destination byte is set to bits 7 to 0 from the **char** variable. Bits 31 to 8 are ignored.
|
||||
|
||||
## For-in Loop
|
||||
The for-in-loop which allows iterating over a string or a linear array of scalar variables is supported. It is more efficient than using a for loop for indexing a string or an array, because no bounds checks are needed.
|
||||
The for-in-loop is supported, which allows iterating over a string or a linear array of scalar variables. It is more efficient than using a for loop for indexing a string or an array, because no bounds checks are needed on each iteration.
|
||||
|
||||
String Example:
|
||||
```
|
||||
var s:string;
|
||||
c:char;
|
||||
|
||||
...
|
||||
|
||||
s := 'Test';
|
||||
|
||||
for c in s do
|
||||
writeln(ord(c));
|
||||
|
||||
...
|
||||
```
|
||||
|
||||
Array Example:
|
||||
```
|
||||
var a:array [1..3] of integer;
|
||||
i:integer;
|
||||
|
||||
...
|
||||
|
||||
a[1] := 3; a[3] := 2; a[5] := 1;
|
||||
|
||||
for i in a do
|
||||
writeln(i);
|
||||
|
||||
...
|
||||
|
||||
```
|
||||
|
||||
## Sets
|
||||
The maximum number of elements in a set is 32. This makes a SET OF CHAR impossible.
|
||||
|
|
@ -39,9 +70,32 @@ The _in_ operator also works for linear arrays, so the _if_ statement will have
|
|||
|
||||
Note that the array _in_ operator will be more inefficient for larger ranges (i.e. `'A'..'z'`), but more efficient for sparse sets (i.e. `'A','z'`).
|
||||
|
||||
A set literal can only appear on the right side of an assignment to a set variable, or when passing an argument of a set type to a procedure/function.
|
||||
|
||||
Example:
|
||||
```
|
||||
program settest;
|
||||
type weekday = (Mon,Tue,Wed,Thu,Fri,Sat,Sun);
|
||||
days = set of weekday;
|
||||
|
||||
var s:days;
|
||||
d:weekday;
|
||||
|
||||
begin
|
||||
s := [Sat,Sun]; (* set literal *)
|
||||
|
||||
d := Sun;
|
||||
if d in [Sat,Sun] then (* array literal, IN operator for arrays *)
|
||||
writeln('weekend');
|
||||
|
||||
if d in s then (* IN operator for sets *)
|
||||
writeln('also weekend');
|
||||
end.
|
||||
```
|
||||
|
||||
## Break and Continue Statements
|
||||
|
||||
Wirth Pascal has no statement to exit a loop. Other Pascal dialects have the _break_ and _continue_ statements to exit the loop or skip to the next iteration.
|
||||
Wirth Pascal has no statement to exit a loop other than the loop condition. Other Pascal dialects have the _break_ and _continue_ statements to exit the loop or skip to the next iteration.
|
||||
|
||||
Tridora-Pascal only supports the _break_ statement at the moment.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue