Difference between revisions of "In"

From Free Pascal wiki
Jump to navigationJump to search
(→‎example: prettify code)
Line 9: Line 9:
 
* It is also usable in [[Uses|<syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>‑clauses]].
 
* It is also usable in [[Uses|<syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>‑clauses]].
  
== example ==
+
== Example ==
 
 
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
program projectin;
 
program projectin;
uses SysUtils,TypInfo;
+
uses SysUtils, TypInfo;
 
type
 
type
   Berry = (Blueberry,FlyHoneysuckle, Lingonberry, Raspberry,Snowberry,Strawberry);
+
   Berry = (Blueberry, FlyHoneysuckle, Lingonberry, Raspberry, Snowberry, Strawberry);
 
   Berries = set of berry;
 
   Berries = set of berry;
 
var
 
var
 
   basket: Berries;
 
   basket: Berries;
   someberry:berry;
+
   someberry: berry;
   str:string;
+
   str: string;
   i:integer;
+
   i: integer;
 
begin
 
begin
 
   basket := [];
 
   basket := [];
 
   writeLn('Choose a berry from the following berries into your basket');
 
   writeLn('Choose a berry from the following berries into your basket');
 
   repeat
 
   repeat
     i:=1;
+
     i := 1;
 
     for someberry in berry do
 
     for someberry in berry do
 
       begin
 
       begin
Line 33: Line 32:
 
         inc(i);
 
         inc(i);
 
       end;
 
       end;
     writeln('0 : exit ');
+
     writeln('0 : exit');
 
     writeln;
 
     writeln;
     readln (i);
+
     readln(i);
 
     if i>0 then
 
     if i>0 then
 
       begin
 
       begin
         someberry :=Berry(i-1);
+
         someberry :=Berry(i-1);
         Include(basket,someberry);
+
         Include(basket, someberry);
 
       end;
 
       end;
 
   until  i=0;
 
   until  i=0;
 
   if (FlyHoneysuckle in basket) or (Snowberry in basket) then
 
   if (FlyHoneysuckle in basket) or (Snowberry in basket) then
 
   begin
 
   begin
     writeln ('You have poisonous berries in your basket');
+
     writeln('You have poisonous berries in your basket');
 
     if FlyHoneysuckle in basket then
 
     if FlyHoneysuckle in basket then
       writeln ('Your basket has a poisonous Fly honeysuckle!') ;
+
       writeln('Your basket has a poisonous Fly honeysuckle!') ;
 
     if Snowberry in basket then
 
     if Snowberry in basket then
       writeln ('Your basket has a poisonous Snowberry!');
+
       writeln('Your basket has a poisonous Snowberry!');
 
   end;
 
   end;
 
   writeln('So you had these berries in your basket:');
 
   writeln('So you had these berries in your basket:');
 
   for someberry in basket do
 
   for someberry in basket do
 
   begin
 
   begin
     Str := GetEnumName(TypeInfo(Berry),ord(someberry));
+
     Str := GetEnumName(TypeInfo(Berry), Ord(someberry));
 
     writeln(Str);
 
     writeln(Str);
 
   end;
 
   end;
   readln ;
+
   readln;
 
end.
 
end.
     
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:57, 28 January 2022

Deutsch (de) English (en) suomi (fi)

The reserved word in:

  • It tests whether a value is in a set. It returns the boolean value true if the value belongs to the set and false if the value does not belong to the set.
  • It is also used with the reserved word for in for-in loop.
  • It is also usable in uses‑clauses.

Example

program projectin;
uses SysUtils, TypInfo;
type
  Berry = (Blueberry, FlyHoneysuckle, Lingonberry, Raspberry, Snowberry, Strawberry);
  Berries = set of berry;
var
   basket: Berries;
   someberry: berry;
   str: string;
   i: integer;
begin
  basket := [];
  writeLn('Choose a berry from the following berries into your basket');
  repeat
    i := 1;
    for someberry in berry do
      begin
        Str := GetEnumName(TypeInfo(Berry),ord(someberry));
        writeln(i,' : ',Str);
        inc(i);
      end;
    writeln('0 : exit');
    writeln;
    readln(i);
    if i>0 then
      begin
        someberry :=Berry(i-1);
        Include(basket, someberry);
      end;
  until  i=0;
  if (FlyHoneysuckle in basket) or (Snowberry in basket) then
   begin
     writeln('You have poisonous berries in your basket');
     if FlyHoneysuckle in basket then
       writeln('Your basket has a poisonous Fly honeysuckle!') ;
     if Snowberry in basket then
       writeln('Your basket has a poisonous Snowberry!');
   end;
  writeln('So you had these berries in your basket:');
  for someberry in basket do
  begin
    Str := GetEnumName(TypeInfo(Berry), Ord(someberry));
    writeln(Str);
  end;
  readln;
end.