Basic Pascal Tutorial/Chapter 3/Boolean Expressions/ja

From Free Pascal wiki
Revision as of 19:13, 8 August 2015 by Derakun (talk | contribs)
Jump to navigationJump to search

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

3B - ブール演算式 (著者: Tao Yue, 状態: 原文のまま修正なし)

ブール 演算式は2つの値を比較するために利用され、 true-あるいは-false の答えをもたらす。

value1 関係演算子 value2

以下の関係演算子が利用される。

<	未満
>	超過
=	等しい
<=	以下
>=	以上
<>	等しくない

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.

previous contents next