DEV Community

Wahid Abduhakimov
Wahid Abduhakimov

Posted on

Relational Operators

Boolean data type
  • C++ dasturlash tilida faqatgina true yoki false qiymatlar qabul qiluvchi bool data type mavjud.
  • xotiradan 1 bit joy egallaydi.
  • 0dan boshqa har qanday qiymat true hisoblanadi 0 esa false.
    bool b = true;

    cout << b << endl;   // 1

    b = 5;
    cout << b << endl;   // 1
Enter fullscreen mode Exit fullscreen mode

Relational Operators
  • > - chap taraf kattaroq bo'lsa true qaytaradi
  • < - chap taraf kichikroq bo'lsa true qaytaradi
  • >= - chap taraf kattaroq yoki teng bo'lsa true qaytaradi
  • <= - chap taraf kichikroq yoki teng bo'lsa true qaytaradi
  • == - ikkala taraf qiymati teng bo'lsa true qaytaradi
  • != - ikkala taraf qiymati teng bo'lmasa true qaytaradi
    int x = 5, y = 6;

    cout << (x > y) << endl;        // 0
    cout << (x < y) << endl;        // 1
    cout << (x >= y) << endl;       // 0
    cout << (x <= y) << endl;       // 1
    cout << (x == y) << endl;       // 0
    cout << (x != y) << endl;       // 1
Enter fullscreen mode Exit fullscreen mode

Logical Operators

NOT

Image description

  • ! operatori boolean qiymatni teskarisiga o'zgartirish uchun ishlatiladi
  • true qiymatni false va false qiymatni truega o'zgartiradi
    bool qiymat = true;
    int son = !qiymat;

    cout << qiymat << endl;         // 1
    cout << son << endl;            // 0
Enter fullscreen mode Exit fullscreen mode
AND

Image description

  • && operatori agar ikkala o'zgaruvchining ham qiymati true bo'lsagina true qaytaradi
    int a = 0;
    int b = 1;

    cout << (a && b) << endl;       // 0
Enter fullscreen mode Exit fullscreen mode
OR

Image description

  • || operatori ikkala o'zgaruvchidan kamida birining qiymati true bo'lsa, true qaytaradi
    int a = 0;
    int b = 1;

    cout << (a || b) << endl;       // 1
Enter fullscreen mode Exit fullscreen mode

Mashq

Quyidagi ifodalar nimani chop etishini taxmin qiling!

    int a = 3;
    int b = 5;

    cout << ((a >= 3) && (b < 6)) << endl;
    cout << ((a != 3) && (a > 2)) << endl;
    cout << ((b != 5) || (a == 1)) << endl;
    cout << ((a != !b) || (b == 2)) << endl;
Enter fullscreen mode Exit fullscreen mode

Ternary operator

Image description

  • ternary operatori shart natijasi true bo'lsa birinchi o'rindagi qiymatni, false bo'lsa ikkinchi o'rindagi qiymatni qaytaradi
    int a  = 5;
    int b = 0;

    cout << (a >= b) << endl;
    cout << ((a >= b) ? "a katta" : "b katta") << endl;

    cin >> a >> b;

    cout << (a > b ? a : b) << endl;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)