Logical operations: Difference between revisions

→‎Delphi: fulfill task requirements and write a function [not a program], make reference to →‎Pascal: to deduplicate code, →‎Pascal: show/mention all operators, make use of formatted output capabilities
(→‎Delphi: fulfill task requirements and write a function [not a program], make reference to →‎Pascal: to deduplicate code, →‎Pascal: show/mention all operators, make use of formatted output capabilities)
Line 1,066:
 
=={{header|Delphi}}==
Delphi supports all logical operators shown in [[#Pascal|§ Pascal]].
<lang Delphi>program LogicalOperations;
Furthermore, the exclusive or operator <tt>xor</tt> is supported:
 
<lang delphi> { exclusive or }
{$APPTYPE CONSOLE}
writeLn(A:5, ' xor', B:6, ' yields', A xor B:7);</lang>
 
Beware: In Delphi the operators <tt>and</tt>, <tt>or</tt> and <tt>xor</tt> can also refer to [[Bitwise operations#Delphi|bitwise operations]].
const
a = True;
b = False;
begin
Write('a = ');
Writeln(a);
Write('b = ');
Writeln(b);
Writeln;
 
Write('a AND b: ');
Writeln(a AND b);
 
Write('a OR b: ');
Writeln(a OR b);
 
Write('NOT a: ');
Writeln(NOT a);
 
Write('a XOR b: ');
Writeln(a XOR b);
end.</lang>
 
{{out}}
<pre>a = TRUE
b = FALSE
 
a AND b: FALSE
a OR b: TRUE
NOT a: FALSE
a XOR b: TRUE</pre>
 
=={{header|DWScript}}==
Line 1,420 ⟶ 1,390:
PRINT *, 'a not equivalent to b is ', A .NEQV. B
END</lang>
 
=={{header|Free Pascal}}==
''See [[#Delphi|Delphi]]''
 
=={{header|Frink}}==
Line 2,942 ⟶ 2,915:
 
=={{header|Pascal}}==
Nine logical operators are standard.
<lang pascal>procedure printlogic(a, b: boolean);
Since [[Boolean values#Pascal|<tt>Boolean</tt> is a built-in enumeration data type]], all relational operators except the membership operator (<tt>in</tt>) are applicable.
begin
Moreover, [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]] support the exclusive operator <tt>xor</tt>.
writeln('a and b is ', a and b);
<lang pascal>function logicalOperations(A, B: Boolean): Boolean;
writeln('a or b is ', a or b);
begin
writeln('not a is', not a);
{ logical conjunction }
end;</lang>
writeLn(A:5, ' and', B:6, ' yields', A and B:7);
{ logical disjunction }
writeLn(A:5, ' or', B:6, ' yields', A or B:7);
{ logical negation }
writeLn(' not', A:6, ' yields', not A:7);
{ logical equivalence }
writeLn(A:5, ' =', B:6, ' yields', A = B:7);
{ negation of logical equivalence }
writeLn(A:5, ' <>', B:6, ' yields', A <> B:7);
{ relational operators }
writeLn(A:5, ' <', B:6, ' yields', A < B:7);
writeLn(A:5, ' >', B:6, ' yields', A > B:7);
writeLn(A:5, ' <=', B:6, ' yields', A <= B:7);
writeLn(A:5, ' >=', B:6, ' yields', A >= B:7);
{ fulfill task requirement of writing a function: }
logicalOperations := true
end.;</lang>
Furthermore, [[Extended Pascal]] (ISO standard 10206) defines two additional logical operators.
The operators <tt>and_then</tt> and <tt>or_else</tt> are intended for [[Short-circuit evaluation#Pascal|short-circuit evaluation]].
However, since all actual parameters need to be evaluated ''prior'' activation of the function, it makes little sense to use/show them in the above sample code.
 
=={{header|Perl}}==
149

edits