## Introduction

The logical operators are the boolean operators && (AND), ||(OR), and !(NOT).

They can create compound conditions and perform conditional execution of a program depending on multiple conditions.

A logical expression results in a value false or true.

"Truth" table for logical operators &&, ||

A | B | A && B | A || B |
---|

true | true | true | true |

true | false | false | true |

false | true | false | true |

false | false | false | false |

"Truth" table for logical operators !

A | !A |
---|

true | false |

false | true |

## Examples

x | y | Logical Expression | Result |
---|

1 | -1 | x <= y || y >=0 | false |

0 | 0 | x > -2 && y == 0 | true |

-1 | 0 | x && !y | true |

0 | 1 | !(x+1) || y - 1 > 0 | false |

A numeric value, such as x or x+1, is interpreted as "false" if its value is 0.

Any value other than 0 is interpreted as "true."

The OR operator || will return true only if at least one operand is true, so the value of the expression

(length < 0.2) || (length > 9.8)

is true if length is less than 0.2or greater than 9.8.

The AND operator && will return true only if both operands are true, so the logical expression

(index < max) && (cin >> number)

is true, provided index is less than max and a number is successfully input.

If the condition index < max is not met, the program will not attempt to read a number!

For logical operators && and ||, the left operand is evaluated first and if a result has already been decided, the right operand will not be evaluated!

## Related Topic