What does a logical operator return for a true condition?

Logical operators are used to perform logical operations of given expressions [relational expressions] or variables.

There are three logical operators available in C. Let’s discuss one by one.



|| [Logical OR] operator

If one of the operands or expressions is true, it will return 1.

If all of them are false, it will return 0.

A

B

A || B

Example

0

0

0

[5 > 10] || [5 < 4]         Both expressions are false. so, logical OR output will be 0

0

1

1

[10 > 20] || [10 < 20]    First expression is false and second one is true. so, logical OR output will be 1

1

0

1

[10 < 20] || [10 > 100]   First expression is true and second one is false. so, logical OR output will be 1

1

1

1

[10 < 20] || [10 < 100]   Both expressions are true. so, logical OR output will be 1



&& [Logical AND] operator

If both left and right operands or expressions are true, it will return true. Otherwise, it will return false.

Note, non-zero value operands are considered as true.

A

B

A && B

Example

0

0

0

[5 > 10] && [5 < 4]         Both expressions are false. so, logical AND output will be 0

0

1

0

[10 > 20] && [10 < 20]    First expression is false and second one is true. so, logical AND output will be 0

1

0

0

[10 < 20] && [10 > 100]   First expression is true and second one is false. so, logical AND output will be 0

1

1

1

[10 < 20] && [10 < 100]   Both expressions are true. so, logical AND output will be 1



! [Logical NOT] operator

Logical NOT operator is used to inverse the current decision. Say, if current state is true, Logical NOT [!] operator will make it false.

A

!A

Example

0

1

![100 < 10]   100 is greater than 10. So, it will return false.
![false] ==> true

1

0

![10 < 100]   10 is less than 100. So, it will return true.
![true] ==> false



Sample Program


Example

#include int main[] { int a = 5, b = 10 ,ret; ret = [ [a true. So, 1 || 1 will return 1. printf["Return value of above expression is %d\n",ret]; ret = [ [ a true. 5 == 10 ==> false. So, 1 && 0 will return 0; printf["Return value of above expression is %d\n",ret]; ret = ! [ [ a a && a > 0]{ //do something... }

Note that:

  • a && b will return true only when BOTH a AND b are true.
  • a || b will return true when either a AND/OR b are true.

Challenge
As briefly mentioned in About Programming, computers ultimately calculate just by doing simple logical operation like AND and OR on Logic Gates. While these gates are physical parts of a computer, you can simulate them in software. The NAND gate is a logical gate that returns false only when both its inputs are true. In other cases it returns true. You will simulate a NAND gate in this challenge. Try to do this challenge without using any if statements.

What does a logical operator return for a true condition in C?

The && [logical AND] operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int .

Which logical operator returns true if either condition is true?

Answer and Explanation: The "or" operator returns true if either condition is true.

Which operator returns true if?

Equal to operator: Represented as '==', the equal to operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise, it returns false. For example, 5==5 will return true.

What is the operator for a logical AND condition?

The logical AND operator is represented as the '&&' double ampersand symbol. It checks the condition of two or more operands by combining in an expression, and if all the conditions are true, the logical AND operator returns the Boolean value true or 1. Else it returns false or 0.

Chủ Đề