Difference between revisions of "In"

From Lazarus wiki
Jump to navigationJump to search
(Added range example)
m (Replace deprecated enclose attributes)
 
Line 1: Line 1:
 
{{In}}
 
{{In}}
  
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">in</syntaxhighlight>:
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>in</syntaxhighlight>:
  
 
* It tests whether a value is in a [[Set|set]]. It returns the [[Boolean|boolean]] value [[True|
 
* It tests whether a value is in a [[Set|set]]. It returns the [[Boolean|boolean]] value [[True|
<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>]] if the value belongs to the set and [[False|
+
<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]] if the value belongs to the set and [[False|
<syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]] if the value does not belong to the set.
+
<syntaxhighlight lang="pascal" inline>false</syntaxhighlight>]] if the value does not belong to the set.
* It is also used with the reserved word [[For|<syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>]] in [[for-in loop]].
+
* It is also used with the reserved word [[For|<syntaxhighlight lang="pascal" inline>for</syntaxhighlight>]] in [[for-in loop]].
* It is also usable in [[Uses|<syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>‑clauses]].
+
* It is also usable in [[Uses|<syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>‑clauses]].
  
 
== Example #1 ==
 
== Example #1 ==

Latest revision as of 09:38, 26 May 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 #1

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.

Example #2

const Test: char = 'q';

begin
  if (Test in ['0'..'9']) then
    Writeln('Digit')
  else if (Test in ['A'..'z']) then
    Writeln('Letter')
  else
    Writeln('Other');
end.