Day 8 : C++Language| Logical Operators

Logical Operators The three most commonly used logical operators are AND, OR, and NOT AND (&&) This operator requires that all connected conditions be true for the overall statement to be true. For instance, in programming, if A and B represent conditions, A AND B returns true only if both A and B are true. #include using namespace std; int main() { int x = 6; int y = 4; cout 3 && x < 10); // returns true (1) because 6 is greater than 3 AND 4 is less than 10 . //since both statement are true , result will be true satisfying and operator return 0; } OR ( || ) This operator allows for flexibility—only one of the connected conditions must be true. In the statement A OR B, the result will be true if either A or B (or both) is true. #include using namespace std; int main() { int x = 5; int y = 3; cout 1 || x < 3); // returns true (1) because one of the conditions are true (5 is greater than 1, but 5 is not less than 3) return 0; } NOT (!) NOT inverts the truth value of a single condition. If A is true, then NOT A will be false, and vice versa. #include using namespace std; int main() { int x = 6; int y = 4; cout 3 && x < 10); // returns false (0) because ! (not) is used to reverse the result return 0; } Uses of logical operators They are implemented in conditional statements, loops, and algorithms to enable computers to make decisions. For example, imagine an e-commerce platform where discounts are applied only if a customer is both a loyalty member AND has spent over a certain amount. Logical operators will be explained in detail while learning loops Previous Blogs Day1:Introduction to C++ Day2:C++ language | output Day3: C++ language | variables | Datatypes |part1 Day 4: C++ language | Variables | Datatypes | Part-2 Day 5: C++ Language | Arithmetic operators Day 6: C++ Language | Assignment operators Day 7: C++ Language | Comparison Operators

Mar 10, 2025 - 22:54
 0
Day 8 : C++Language| Logical Operators

Logical Operators

The three most commonly used logical operators are AND, OR, and NOT

AND (&&)

This operator requires that all connected conditions be true for the overall statement to be true.

For instance, in programming, if A and B represent conditions, A AND B returns true only if both A and B are true.

#include 
using namespace std;

int main() {
  int x = 6;
  int y = 4;

  cout << (x > 3 && x < 10); 

// returns true (1) because 6 is greater than 3 AND 4 is less than 10 .

 //since both statement are true , result will be true satisfying and operator

  return 0;
}

OR ( || )

This operator allows for flexibility—only one of the connected conditions must be true.

In the statement A OR B, the result will be true if either A or B (or both) is true.

#include 
using namespace std;

int main() {
  int x = 5;
  int y = 3;
  cout << (x > 1 || x < 3);

 // returns true (1) because one of the conditions are true
 (5 is greater than 1, but 5 is not less than 3)

  return 0;

}

NOT (!)

NOT inverts the truth value of a single condition.
If A is true, then NOT A will be false, and vice versa.

#include 
using namespace std;

int main() {
  int x = 6;
  int y = 4;

  cout << !(x > 3 && x < 10); 
 // returns false (0) because ! (not) is used to reverse the result

  return 0;
}

Uses of logical operators

They are implemented in conditional statements, loops, and algorithms to enable computers to make decisions.
For example, imagine an e-commerce platform where discounts are applied only if a customer is both a loyalty member AND has spent over a certain amount.

Logical operators will be explained in detail while learning loops

Previous Blogs