Difference between revisions of "Basic Pascal Tutorial/Chapter 3/Boolean Expressions"

From Lazarus wiki
Jump to navigationJump to search
(teur)
m (bypass language bar/categorization template redirect [cf. discussion])
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Boolean Expressions}}
+
{{Basic Pascal Tutorial/Chapter 3/Boolean Expressions}}
 +
{{TYNavigator|Chapter 3/Sequential control|Chapter 3/IF}}
  
3B - Expressions booléennes
+
3B - Boolean Expressions (author: Tao Yue, state: unchanged)
  
[[Boolean]] Expressions utilisée pour comparer deux valeurs et retourne <tt>true</tt> ou <tt>false</tt> :
+
[[Boolean]] expressions are used to compare two values and get a <tt>true-or-false</tt> answer:
  valeur1 '''relational_operator''' valeur2
+
  value1 '''relational_operator''' value2
  
Les opérateurs relationnels suivants sont utilisés :
+
The following relational operators are used:
  < inférieur à
+
  < less than
  > supérieur à
+
  > greater than
  = égal à
+
  = equal to
  <= inférieur ou égal à
+
  <= less than or equal to
  >= supérieur ou égal à
+
  >= greater than or equal to
  <> différent de
+
  <> not equal to
  
Les expressions booléennes sont affectables à des variables booléennes. Ici, nous affectons une expression valant <tt>true</tt> à some_bool :
+
You can assign Boolean expressions to Boolean variables. Here we assign a <tt>true</tt> expression to some_bool:
 +
 
 +
<syntaxhighlight lang="pascal">
 
  some_bool := 3 < 5;
 
  some_bool := 3 < 5;
 +
</syntaxhighlight>
  
Des expressions booléennes plus complexes sont formées avec les opérateurs booléens:
+
Complex Boolean expressions are formed by using the Boolean operators:
  not négation (~)
+
  not negation (~)
  and conjonction (^)
+
  and conjunction (^)
  or disjonction (v)
+
  or disjunction (v)
  xor ou exclusif ((+))
+
  xor exclusive-or
  
<tt>NOT</tt> est un opérateur unaire il est appliqué à une seule valeur et inverse sa valeur:
+
<tt>NOT</tt> is a unary operator it is applied to only one value and inverts it:
  
 
* <tt>not true = false
 
* <tt>not true = false
 
* not false = true</tt>
 
* not false = true</tt>
  
<tt>AND</tt> retourne <tt>TRUE</tt> uniquement si les deux valeurs sont <tt>TRUE</tt> simultanément:
+
<tt>AND</tt> yields <tt>TRUE</tt> only if both values are <tt>TRUE</tt>:
  
 
* <tt>TRUE and FALSE = FALSE
 
* <tt>TRUE and FALSE = FALSE
 
* TRUE and TRUE = TRUE</tt>
 
* TRUE and TRUE = TRUE</tt>
  
<tt>OR</tt> retourne <tt>TRUE</tt> si au moins l'une des valeurs est <tt>TRUE</tt>:
+
<tt>OR</tt> yields <tt>TRUE</tt> if at least one value is <tt>TRUE</tt>:
  
 
* <tt>TRUE or TRUE = TRUE
 
* <tt>TRUE or TRUE = TRUE
Line 40: Line 44:
 
* FALSE or FALSE = FALSE</tt>
 
* FALSE or FALSE = FALSE</tt>
  
<tt>XOR</tt> retourne <tt>TRUE</tt> si les deux valeurs sont différentes. Ainsi:
+
<tt>XOR</tt> yields <tt>TRUE</tt> if one expression is <tt>TRUE</tt> and the other is <tt>FALSE</tt>. Thus:
  
 
* <tt>TRUE xor TRUE = FALSE
 
* <tt>TRUE xor TRUE = FALSE
Line 47: Line 51:
 
* FALSE xor FALSE = FALSE</tt>
 
* FALSE xor FALSE = FALSE</tt>
  
C'est l'inverse de l'opérateur d'égalité <tt>=</tt>, on peut donc le remplacer par l'opérateur <tt><></tt> appliqué aux valeurs booléennes.
+
When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.
  
En combinant deux expressions booléennes en utilisant les opérateurs relationnels et booléens, l'emploi de parenthèses est prudent :
+
<syntaxhighlight lang="pascal">
<syntaxhighlight>
 
 
(3>5) or (650<1)
 
(3>5) or (650<1)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Cela parce que les opérateurs booléens ont une priorité d'évaluation plus élevée que les opérateurs relationnels :
+
This is because the Boolean operators are higher on the order of operations than the relational operators:
  
 
# <tt>not
 
# <tt>not
Line 61: Line 64:
 
# < > <= >= = <></tt>
 
# < > <= >= = <></tt>
  
Ainsi <tt>3 > 5 or 650 < 1</tt> est évalué en <tt>3 > (5 or 650) < 1</tt>, ce qui n'a pas de sens comme expression booléenne (<tt>OR</tt> est un opérateur agissant aussi sur les entiers mais avec un autre sens).
+
So <tt>3 > 5 or 650 < 1</tt> becomes evaluated as <tt>3 > (5 or 650) < 1</tt>, which makes no sense, because the Boolean operator or only works on Boolean values, not on integers.
 
 
Les opérateurs booléens (<tt>AND, OR, NOT, XOR</tt>) peuvent être employés aussi bien sur des expressions booléennes que sur des valeurs booléennes.
 
 
 
Quelques remarques:
 
* bien que ces opérateurs soient commutatifs (on peut changer la place les opérandes des opérateurs), une optimisation fréquente des compilateurs, dite évaluation paresseuse, consiste à n'évaluer que le premier opérande et le second au besoin du fait des propriétés des opérateurs booléens. Cela n'a habituellement aucune conséquence sauf pour des constructions particulières, prudence donc...
 
 
 
* Autant que possible, ne jamais comparer directement deux valeurs réelles, de légères différences d'arrondi peuvent fausser le résultat de l'expression. En fait, il vaut mieux comparer la différence à une valeur suffisamment petite.
 
  
* Dans la mesure du possible, éviter des expressions du genre
+
The Boolean operators (<tt>AND, OR, NOT, XOR</tt>) can be used on Boolean variables just as easily as they are used on Boolean expressions.
if some_bool = True then
 
alors que
 
if some_bool then
 
suffit largement et allège la lecture.
 
  
{|style=color-backgroud="white" cellspacing="20"
+
Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.
|[[Sequential_control|previous]] 
 
|[[Contents|contents]]
 
|[[IF|next]]
 
|}
 
  
[[Category: Object Pascal Introduction]]
+
{{TYNavigator|Chapter 3/Sequential control|Chapter 3/IF}}

Latest revision as of 16:18, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

3B - Boolean Expressions (author: Tao Yue, state: unchanged)

Boolean expressions are used to compare two values and get a true-or-false answer:

value1 relational_operator value2

The following relational operators are used:

<	less than
>	greater than
=	equal to
<=	less than or equal to
>=	greater than or equal to
<>	not equal to

You can assign Boolean expressions to Boolean variables. Here we assign a true expression to some_bool:

 some_bool := 3 < 5;

Complex Boolean expressions are formed by using the Boolean operators:

not	negation (~)
and	conjunction (^)
or	disjunction (v)
xor	exclusive-or

NOT is a unary operator — it is applied to only one value and inverts it:

  • not true = false
  • not false = true

AND yields TRUE only if both values are TRUE:

  • TRUE and FALSE = FALSE
  • TRUE and TRUE = TRUE

OR yields TRUE if at least one value is TRUE:

  • TRUE or TRUE = TRUE
  • TRUE or FALSE = TRUE
  • FALSE or TRUE = TRUE
  • FALSE or FALSE = FALSE

XOR yields TRUE if one expression is TRUE and the other is FALSE. Thus:

  • TRUE xor TRUE = FALSE
  • TRUE xor FALSE = TRUE
  • FALSE xor TRUE = TRUE
  • FALSE xor FALSE = FALSE

When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.

(3>5) or (650<1)

This is because the Boolean operators are higher on the order of operations than the relational operators:

  1. not
  2. * / div mod and
  3. + - or
  4. < > <= >= = <>

So 3 > 5 or 650 < 1 becomes evaluated as 3 > (5 or 650) < 1, which makes no sense, because the Boolean operator or only works on Boolean values, not on integers.

The Boolean operators (AND, OR, NOT, XOR) can be used on Boolean variables just as easily as they are used on Boolean expressions.

Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.

 ◄   ▲   ►