JavaRush /Java Blog /Random EN /Comparison operators and logical operators. Branching in ...
articles
Level 15

Comparison operators and logical operators. Branching in the program. Conditional operator

Published in the Random EN group

Logical operators

There are several binary logical operators and one unary one. The arguments for all these operators are logical literals (constants), logical variables and expressions that have a logical value.
Comparison operators and logical operators.  Branching in the program.  Conditional operator - 1
Operators:
  • !- “negation”, a unary operator, changes the meaning to the opposite (inverts: turns a lie into truth, and turns truth into a lie).
  • &&- logical “and” (“conjunction”, “intersection”), a binary operation, returns true if and only if both operands are true.
  • ||- logical “or” (“disjunction”, “union”), binary operation, returns true value when at least one of the operands is true.
Logical operators have the following priority: negation, conjunction, disjunction. Just as in the case of arithmetic operators, parentheses are used to correct precedence. If one pair of parentheses is nested within another pair of parentheses, the value in the inner parentheses is evaluated first. Examples:
boolean a = true;
boolean b;
b = a || true; // b истинно
b = !b; // b ложно
System.out.println(b); // выведет false
a = a || b; // a истинно
boolean c;
c = a && (a||b); //с истинно
System.out.println(c); // выведет true
In Java, boolean and numeric types cannot be converted to each other.

Comparison Operators

Most comparison operators apply to numeric values. These are all binary operators that have two numeric arguments, but return a Boolean value.
  • >— the “more than” operator.
  • >=— “greater than or equal” operator.
  • <— “less than” operator.
  • <=— “less than or equal” operator.
  • !=— “not equal” operator.
  • ==— equivalence (equality) operator.
The last two operators can be used not only for numeric values, but also, for example, for logical ones. Examples:
boolean m;
m = 5 >= 4; // истина
m = 5 != 5 || false; // ложь
boolean w;
w = m == false; // истина
System.out.println(w); // выведет true
It is very important not to confuse the equivalence operator with the assignment operator. In expressions that contain operators of different types, arithmetic operations are performed first, then comparison operations, then logical operations, and lastly assignment.

Conditional if statement

The operator ifensures that an instruction is executed or skipped depending on a specified logical condition. If the condition is true, then the instruction is executed.
if (condition) инструкция;
In place of an instruction there can be either a regular instruction (one command) or a compound instruction (a block containing several commands, including other conditional statements). Examples (if zero is specified as the variable values, division will not be performed and its result will not be displayed on the screen):
// Пример 1
int a = 25;
if (a != 0) System.out.println( 100/a );
// Пример 2
int b = 25;
if (b != 0) {
  System.out.println( 100/b );
}
Despite the fact that the code in the first example looks more compact, only in the second example would it be possible to execute several instructions if the condition is true. The operator ifhas a format with an additional part else:
if (condition)
инструкция1;
else
инструкция2;
If the condition is true , a simple or compound instruction1 is executed , and if the condition is false, a simple or compound instruction2 is executed . Example:
int a = 0;
if (a != 0) System.out.println( 100/a );
else System.out.println("На нуль делить нельзя");
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION