Boolean Operators and Truth Tables

The basic building blocks of Boolean algebra: variables, constants and operators.

Boolean Operators and Truth Tables

Boolean Operators का उपयोग logical conditions को combine करने और उनका result प्राप्त करने के लिए किया जाता है। Boolean expression का result सामान्यतः True या False होता है। C++ में Boolean values को represent करने के लिए bool data type का उपयोग किया जाता है।

Boolean Value: true या false
C++ में: true को 1 और false को 0 के रूप में represent किया जा सकता है।

What is Boolean Logic?

Boolean logic ऐसी logic है जिसमें expressions का result केवल दो possible values में से एक होता है: True या False। इसका उपयोग decision making, conditions और logical operations में किया जाता है।

उदाहरण के लिए, यदि x = 10 है:

cout << (x > 5);
Output:
1

क्योंकि 10, 5 से बड़ा है, इसलिए expression का Boolean result true है।

Boolean Data Type in C++

C++ में bool एक built-in data type है जिसका उपयोग Boolean values store करने के लिए किया जाता है। इसके दो मुख्य values हैं true और false

#include <iostream>
using namespace std;

int main()
{
    bool a = true;
    bool b = false;

    cout << a << endl;
    cout << b;

    return 0;
}
Output:
1
0

Types of Boolean Operators

C++ में तीन प्रमुख logical operators होते हैं:

Operator Name Meaning
&& Logical AND दोनों conditions true होनी चाहिए
|| Logical OR कम-से-कम एक condition true होनी चाहिए
! Logical NOT Boolean result को उलट देता है

Logical AND Operator ( && )

Logical AND operator तब true होता है जब दोनों conditions true हों। यदि कोई एक condition false है, तो पूरा expression false होगा।

उदाहरण:

#include <iostream>
using namespace std;

int main()
{
    int age = 20;

    cout << (age >= 18 && age <= 25);

    return 0;
}
Output:
1

यहाँ दोनों conditions true हैं, इसलिए result true (1) है।

Truth Table of AND

A B A && B
False False False
False True False
True False False
True True True

Rule: AND का result केवल तब true होता है जब दोनों inputs true हों।

Logical OR Operator ( || )

Logical OR operator तब true होता है जब कम-से-कम एक condition true हो। केवल तब false होता है जब दोनों conditions false हों।

#include <iostream>
using namespace std;

int main()
{
    int marks = 35;

    cout << (marks < 40 || marks == 100);

    return 0;
}
Output:
1

पहली condition true है, इसलिए OR expression का result true है।

Truth Table of OR

A B A || B
False False False
False True True
True False True
True True True

Rule: OR का result केवल तब false होता है जब दोनों inputs false हों।

Logical NOT Operator ( ! )

Logical NOT operator किसी Boolean value के result को उलट देता है। True को False और False को True बना देता है।

#include <iostream>
using namespace std;

int main()
{
    bool a = true;
    bool b = false;

    cout << !a << endl;
    cout << !b;

    return 0;
}
Output:
0
1

Truth Table of NOT

A !A
False True
True False

Rule: NOT operator Boolean value को reverse करता है।

Combined Boolean Expressions

एक expression में एक से अधिक logical operators का उपयोग किया जा सकता है। Parentheses का उपयोग करके conditions को स्पष्ट रूप से group किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int age = 20;
    bool student = true;

    bool eligible = (age >= 18 && student);

    cout << eligible;

    return 0;
}
Output:
1

यहाँ age >= 18 और student दोनों true हैं, इसलिए पूरा expression true है।

Boolean Operators with Relational Operators

Logical operators का उपयोग अक्सर relational operators के साथ किया जाता है। Relational operators दो values के बीच comparison करते हैं और Boolean result देते हैं।

Relational Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to

उदाहरण:

#include <iostream>
using namespace std;

int main()
{
    int marks = 75;

    cout << (marks >= 40 && marks <= 100);

    return 0;
}
Output:
1

Truth Table

Truth table एक table है जो किसी Boolean expression के सभी possible input combinations और उनके corresponding output को दर्शाती है।

दो Boolean variables A और B के लिए कुल 2² = 4 possible combinations होते हैं।

A B A && B A || B
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

Truth Table for NOT

A !A
0 1
1 0

Operator Precedence in Boolean Expressions

जब एक expression में multiple logical operators होते हैं, तो operators एक निश्चित precedence के अनुसार evaluate होते हैं। सामान्यतः logical operators में NOT (!) की precedence सबसे अधिक, उसके बाद AND (&&) और फिर OR (||) होती है।

Priority Operator
1 ! (NOT)
2 && (AND)
3 || (OR)

फिर भी complex expressions में ambiguity से बचने के लिए parentheses का उपयोग करना अच्छा practice है।

Example of Operator Precedence

#include <iostream>
using namespace std;

int main()
{
    bool a = true;
    bool b = false;
    bool c = true;

    cout << (a || b && c);

    return 0;
}
Output:
1

यहाँ && की precedence || से अधिक है, इसलिए पहले b && c evaluate होगा।

Boolean Operators in Conditional Statements

Logical operators का सबसे common उपयोग if और else statements में multiple conditions को combine करने के लिए किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 75;

    if (marks >= 40 && marks <= 100)
        cout << "Pass";

    return 0;
}
Output:
Pass

Real-Life Example

मान लीजिए किसी exam में student को pass होने के लिए marks 40 या उससे अधिक होने चाहिए और attendance 75% या उससे अधिक होनी चाहिए। दोनों conditions को AND operator से combine किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 65;
    int attendance = 80;

    if (marks >= 40 && attendance >= 75)
        cout << "Eligible";

    return 0;
}
Output:
Eligible

Important Points

  • Boolean expression का result true या false होता है।
  • C++ में Boolean data type bool है।
  • && Logical AND operator है।
  • || Logical OR operator है।
  • ! Logical NOT operator है।
  • AND तभी true होता है जब दोनों conditions true हों।
  • OR तब true होता है जब कम-से-कम एक condition true हो।
  • NOT Boolean result को reverse करता है।
  • Truth table Boolean expression के सभी possible input-output combinations को दिखाती है।
  • दो Boolean inputs के लिए कुल 4 possible combinations होते हैं।
  • NOT operator में केवल एक input होता है।
  • Logical operators का उपयोग conditional statements में frequently किया जाता है।

Board Focus

Exam के लिए याद रखें:
&& → Logical AND
|| → Logical OR
! → Logical NOT
AND → दोनों true होने पर true
OR → कोई एक true होने पर true
NOT → Result को उलटता है
Truth Table → सभी possible input-output combinations

Board Important Questions

Very Short Answer Questions

Q1. C++ में Boolean data type कौन-सा है?

Answer: bool.

Q2. Logical AND operator कौन-सा है?

Answer: &&.

Q3. Logical OR operator कौन-सा है?

Answer: ||.

Q4. Logical NOT operator कौन-सा है?

Answer: !.

Q5. AND operator कब true होता है?

Answer: जब दोनों conditions true हों।

Q6. OR operator कब false होता है?

Answer: जब दोनों conditions false हों।

Q7. NOT operator क्या करता है?

Answer: Boolean result को reverse करता है।

Q8. दो Boolean variables के लिए कितने possible combinations होते हैं?

Answer: 4.

Q9. Truth table क्या है?

Answer: यह Boolean expression के सभी possible inputs और उनके outputs को दर्शाने वाली table है।

Q10. C++ में true को सामान्यतः किस integer value से represent किया जाता है?

Answer: 1.

Short Answer Questions

Q11. Logical AND operator को truth table सहित समझाइए।

Answer: Logical AND operator && तभी true होता है जब दोनों inputs true हों।

A B A && B
0 0 0
0 1 0
1 0 0
1 1 1

Q12. Logical OR operator को truth table सहित समझाइए।

Answer: Logical OR operator || तब true होता है जब कम-से-कम एक input true हो।

A B A || B
0 0 0
0 1 1
1 0 1
1 1 1

Q13. Logical NOT operator को truth table सहित समझाइए।

Answer: NOT operator ! input के Boolean result को reverse करता है।

A !A
0 1
1 0

Q14. Boolean operators का उपयोग कहाँ किया जाता है?

Answer: Boolean operators का उपयोग multiple conditions को combine करने और conditional statements जैसे if और else में decision लेने के लिए किया जाता है।

Q15. AND और OR operators में अंतर बताइए।

Answer: AND operator तभी true होता है जब दोनों conditions true हों, जबकि OR operator तब true होता है जब कम-से-कम एक condition true हो।

Long Answer Questions

Q16. C++ के logical operators को उदाहरण सहित समझाइए।

Answer: C++ में तीन प्रमुख logical operators हैं AND (&&), OR (||) और NOT (!)। AND दोनों conditions के true होने पर true देता है। OR कम-से-कम एक condition के true होने पर true देता है। NOT Boolean result को reverse करता है।

#include <iostream>
using namespace std;

int main()
{
    bool a = true;
    bool b = false;

    cout << (a && b) << endl;
    cout << (a || b) << endl;
    cout << (!a);

    return 0;
}
Output:
0
1
0

Q17. Truth table क्या है? AND, OR और NOT की truth tables बनाइए।

Answer: Truth table किसी Boolean expression के सभी possible input combinations और उनके corresponding outputs को दर्शाती है।

A B A && B A || B
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
A !A
0 1
1 0

Q18. निम्न C++ program का output बताइए:

#include <iostream>
using namespace std;

int main()
{
    bool a = true;
    bool b = false;

    cout << (a && b) << endl;
    cout << (a || b) << endl;
    cout << (!b);

    return 0;
}
Output:
0
1
1

Q19. Logical operators का उपयोग conditional statement में उदाहरण सहित समझाइए।

#include <iostream>
using namespace std;

int main()
{
    int age = 22;

    if (age >= 18 && age <= 60)
        cout << "Eligible";

    return 0;
}
Output:
Eligible

यहाँ AND operator दोनों conditions को combine करता है। दोनों conditions true होने पर ही statement execute होता है।

Practice Questions

  1. Boolean logic क्या है?
  2. C++ में Boolean data type कौन-सा है?
  3. Boolean operators क्या हैं?
  4. C++ के तीन logical operators लिखिए।
  5. Logical AND operator का symbol लिखिए।
  6. Logical OR operator का symbol लिखिए।
  7. Logical NOT operator का symbol लिखिए।
  8. AND operator की truth table बनाइए।
  9. OR operator की truth table बनाइए।
  10. NOT operator की truth table बनाइए।
  11. Truth table क्या है?
  12. दो Boolean variables के कितने possible combinations होते हैं?
  13. AND और OR में अंतर लिखिए।
  14. Boolean operators का उपयोग conditional statements में कैसे किया जाता है?
  15. Relational और logical operators में अंतर लिखिए।
  16. Logical AND operator को उदाहरण सहित समझाइए।
  17. Logical OR operator को उदाहरण सहित समझाइए।
  18. Logical NOT operator को उदाहरण सहित समझाइए।
  19. Truth table की सहायता से AND और OR operators समझाइए।
  20. C++ program लिखिए जिसमें AND, OR और NOT operators का उपयोग हो।

Quick Revision

  • bool: Boolean data type.
  • true: सामान्यतः 1 के रूप में represent होता है।
  • false: 0 के रूप में represent होता है।
  • &&: Logical AND.
  • ||: Logical OR.
  • !: Logical NOT.
  • AND: दोनों true → true.
  • OR: कोई एक true → true.
  • NOT: True → False और False → True.
  • Truth Table: सभी possible Boolean input-output combinations.
One-Line Revision: C++ में Boolean operators &&, || और ! का उपयोग logical conditions को combine और evaluate करने के लिए किया जाता है, जबकि truth table उनके सभी possible input और output combinations को दर्शाती है।
Lesson 27 of 37
On This Page