C++ Programming Code Examples
Learn C++ Language
Logical Operators in C++ Programming Language
Logical Operators in C++
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable.
We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Assume variable A holds 1 and variable B holds 0:
&&
Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
The logical AND operator && returns
true - if and only if all the operands are true.
false - if one or more operands are false.
||
Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
The logical OR operator || returns
true - if one or more of the operands are true.
false - if and only if all the operands are false.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. !(A && B) is true.
The logical NOT operator ! is a unary operator i.e. it takes only one operand.
It returns true when the operand is false, and false when the operand is true.
/* The operator ! is the C++ operator for the Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.
The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds to the Boolean logical operation AND, which yields true if both its operands are true, and false otherwise. */
#include <iostream>
using namespace std;
main() {
int a = 5;
int b = 20;
int c ;
if(a && b) {
cout << "Line 1 - Condition is true"<< endl ;
}
if(a || b) {
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */
a = 0;
b = 10;
if(a && b) {
cout << "Line 3 - Condition is true"<< endl ;
} else {
cout << "Line 4 - Condition is not true"<< endl ;
}
if(!(a && b)) {
cout << "Line 5 - Condition is true"<< endl ;
}
return 0;
}
'Binary Search tree' for a given unsorted data array & maintain an additional count variable. If any 'Element is Repeated' then increase the count of that 'Node'. Proceed with the search
Enter width of rectangle and enter height of rectangle. Print the "area of rectangle". Print the perimeter of rectangle. Formula: Area of rectangle: height*width. Formula: Perimeter
Program to implement queue using stacks. n this method, in en-queue operation, the new element is entered at the top of stack1. In de-queue operation, if "stack2" is empty then all