Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

9.3. Logical operators and expressions

Logical operators are used to create compound conditions:

  • unary logical complement operator denoted by "!"

  • binary conditional-and operator denoted by "&&"

  • binary conditional-or operator denoted by "||"

Assume we want to check whether the following condition holds: a value n lies in the interval [1, 10], i.e. n is greater than or equal 1 and, at the same time, less than or equal 10. As we already know the notation if (1 <= n <= 10) is incorrect (because relational operators are applied to numeric types only and the expression 1 <= n is of boolean type). We must apply the conditional-and operator here to check whether the two conditions hold. n >= 1 and n <= 10. In Java it is noted as: n >= 1 && n <= 10 and used as the control expression of the if statement: if (n >= 1 && n <= 10) ...; Similarly we might want to check whether a value n lies outside the interval [1, 10]. To fulfill such a condition n has to be less than 1 or greater than 10. To note this in Java the conditional-or operator is applied: if (n < 1 || n > 10) .. The third logical operator - logical complement - is used to negate expressions. For example, if we want to take some action in case a variable txt does not represent the string "Abc" we write: if (! txt.equals("Abc")) ... ; Arguments to logical operators must be expressions of boolean type. The result of such an expression is of boolean type too. Its value is true or false according to the following table:

Conditional logical operators

a

b

a && b

a || b

!a

true

true

true

true

false

false

true

false

true

true

true

false

false

true

false

false

false

false

The operator && has the value true only if both its arguments have the value true, and false otherwise. The operator || has the value true if at least one of its arguments is true, and is false if both its arguments are false.

Logical operators have lower precedence than relational operators and equality operators. Thus we can write (without parentheses): if ( a == 1 && b > 3) c = a + b; to calculate c when a is equal 1 and b > 3.

Note: the unary complement operator has higher precedence than other logical operators. Therefore parentheses should be applied to ensure the proper order of evaluation of subexpressions.

For example, if we want to take some action in case the following condition does not hold: the variable txt does not hold the string "Abc" and - at the same time - the variable years has the value greater than 1, we cannot write: if (! txt.equals("Abc") && years > 1) ... as this has the meaning of simultaneous fulfilment of the two conditions: - the string txt is not "Abc" - years is greater than 1 (of course it is not what we wanted). We must write instead: if (!( txt.equals("Abc") && years > 1)) ... Additional parentheses modify the order of evaluation of subexpressions and thus we achieve the desired effect. The discussed operators are called conditional logical operators because they are evaluated from-left-to-right and the evaluation stops when the value of the whole expression is known (lazy evaluation). This means that not all subexpressions are evaluated if some part of the whole expression determines the result. For example, in the following expression: a && (b > 0 || c == 1) if a is false then the expression in the parentheses will not be evaluated, as the value of the whole expression does not depend on its value (if a == false then the whole expression must be false). In Java there are unconditional logical operators (called boolean logical operators):

  • & AND

  • | OR

  • ^ XOR (exclusive OR)

  • ~ bitwise complement

The boolean logical operators: & and | have the same properties as the conditional operators:&& and || with the following exception: both operands are evaluated unconditionally.

The exclusive OR operator (^) acts as follows:

  • if both operands have the same value then the value of the XOR expression is false,

  • if operand values are different, the result is true.

The boolean logical operators are applied to arguments of boolean type only. Unfortunately, the same symbols ( &, |, ^, ~) denote bitwise operators which are applied to integer numbers (they enable operations on bits).

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]