Boolean data type
- C++ dasturlash tilida faqatgina
true
yokifalse
qiymatlar qabul qiluvchibool
data type mavjud. - xotiradan 1 bit joy egallaydi.
-
0
dan boshqa har qanday qiymattrue
hisoblanadi0
esafalse
.
bool b = true;
cout << b << endl; // 1
b = 5;
cout << b << endl; // 1
Relational Operators
-
>
- chap taraf kattaroq bo'lsatrue
qaytaradi -
<
- chap taraf kichikroq bo'lsatrue
qaytaradi -
>=
- chap taraf kattaroq yoki teng bo'lsatrue
qaytaradi -
<=
- chap taraf kichikroq yoki teng bo'lsatrue
qaytaradi -
==
- ikkala taraf qiymati teng bo'lsatrue
qaytaradi -
!=
- ikkala taraf qiymati teng bo'lmasatrue
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
Logical Operators
NOT
-
!
operatori boolean qiymatni teskarisiga o'zgartirish uchun ishlatiladi -
true
qiymatnifalse
vafalse
qiymatnitrue
ga o'zgartiradi
bool qiymat = true;
int son = !qiymat;
cout << qiymat << endl; // 1
cout << son << endl; // 0
AND
-
&&
operatori agar ikkala o'zgaruvchining ham qiymatitrue
bo'lsagina true qaytaradi
int a = 0;
int b = 1;
cout << (a && b) << endl; // 0
OR
-
||
operatori ikkala o'zgaruvchidan kamida birining qiymatitrue
bo'lsa,true
qaytaradi
int a = 0;
int b = 1;
cout << (a || b) << endl; // 1
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;
Ternary operator
-
ternary
operatori shart natijasitrue
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;
Top comments (0)