Logical Operators in Programming
Logical operators are used to combine conditional statements.
What is a Logical Operator?
A logical operator is one or two symbols or a keyword that tells the computer how to combine conditional statements.
The result of using a logical operator is a boolean value (true or false).
See this page for an overview of other types of operators.
The most common logical operators are:
&&(Logical AND)||(Logical OR)!(Logical NOT)
In the example below, we use the && operator to combine two conditions:
print(10 == 5 and 10 != 5)
console.log(10 == 5 && 10 != 5);
System.out.println(10 == 5 && 10 != 5);
cout << (10 == 5 && 10 != 5);
Run Example »
In the example above, the left side 10 == 5 is false, and the right side 10 != 5 is true. The logical operator && requires both sides to be true for the result to be true, so the result is false.
Try the simulation below to see the result of different logical operators:
{{ operatorText }}
{{ op.codeSnippet }}
a
{{ aValue }}
operator
{{ selectedOperator }}
b
{{ bValue }}
{{ operators[currentIndex].codeSnippet }}
{{ resultValue }}