Bu eğitimde, örnekler yardımıyla ilişkisel ve mantıksal operatörler hakkında bilgi edineceğiz.
C ++ 'da ilişkisel ve mantıksal operatörler iki veya daha fazla işlenenleri karşılaştırır ve true
veya false
değerlerinden birini döndürür .
Bu operatörleri karar vermede kullanıyoruz.
C ++ İlişkisel Operatörler
İki işlenen arasındaki ilişkiyi kontrol etmek için bir ilişkisel operatör kullanılır. Örneğin,
// checks if a is greater than b a> b;
İşte >
ilişkisel bir operatör. A'nın b'den büyük olup olmadığını kontrol eder.
İlişki doğruysa 1 döndürür , ilişki yanlışsa 0 döndürür .
Aşağıdaki tablo, C ++ 'da kullanılan ilişkisel operatörleri özetlemektedir.
Şebeke | Anlam | Misal |
---|---|---|
== | Eşittir | 3 == 5 bize yanlış verir |
!= | Eşit değil | 3 != 5 bize doğru verir |
> | Büyüktür | 3> 5 bize yanlış verir |
< | Daha az | 3 < 5 bize doğru verir |
>= | Büyüktür veya Eşittir | 3>= 5 bize yanlış ver |
<= | Küçüktür veya Eşittir | 3 <= 5 bize doğru verir |
== Operatör
==
Operatör döndürürlerine eşittir
true
- her iki işlenen de eşit veya aynıysafalse
- işlenenler eşit değilse
Örneğin,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Not: İlişkisel operatör ==
, atama operatörüyle aynı değildir =
. Atama operatörü =
bir değişkene, sabite, diziye veya vektöre bir değer atar. İki işleneni karşılaştırmaz.
! = Operatör
!=
Operatör döndürürlerine eşit değildir
true
- her iki işlenen de eşit değilsefalse
- her iki işlenen de eşitse.
Örneğin,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operatör
>
Operatör döndürürden büyüktür
true
- sol işlenen sağdan büyüksefalse
- sol işlenen sağdan küçükse
Örneğin,
int x = 10; int y = 15; x> y // false y> x // true
<Operatör
Operatörden daha az <
döndürür
true
- sol işlenen sağdan küçüksefalse
- sol işlenen sağdan büyükse
Örneğin,
int x = 10; int y = 15; x < y // true y < x // false
> = Operatör
Büyüktür veya eşittir >=
operatör döndürür
true
- sol işlenen sağdan büyük veya sağdan büyüksefalse
- sol işlenen sağdan küçükse
Örneğin,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operatör
Küçüktür veya eşittir operatör <=
döndürür
true
- sol işlenen sağdan küçük veya sağdan eşitsefalse
- sol işlenen sağdan büyükse
Örneğin,
int x = 10; int y = 15; x> y // false y> x // true
İlişkisel operatörlerin dizelerle nasıl kullanılabileceğini öğrenmek için buradaki eğiticimize bakın.
C ++ Mantıksal Operatörler
Biz bir ifade olup olmadığını kontrol için mantıksal operatörlerin kullanımı gerçek veya sahte . İfade ise doğru , bu döner 1 ifadesi ise ise sahte , bu döner 0 .
Şebeke | Misal | Anlam |
---|---|---|
&& | expression1 && expression 2 | Mantıksal VE. yalnızca tüm işlenenler doğruysa doğrudur. |
|| | ifade1 || ifade 2 | Logical OR. true if at least one of the operands is true. |
! | !expression | Logical NOT. true only if the operand is false. |
C++ Logical AND Operator
The logical AND operator &&
returns
true
- if and only if all the operands aretrue
.false
- if one or more operands arefalse
.
Truth Table of && Operator
Let a and b be two operands. 0 represents false while 1 represents true. Then,
a | b | a && b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
As we can see from the truth table above, the &&
operator returns true only if both a
and b
are true.
Note: The Logical AND operator && should not be confused with the Bitwise AND operator &.
Example 1: C++ OR Operator
// C++ program demonstrating && operator truth table #include using namespace std; int main() ( int a = 5; int b = 9; // false && false = false cout < b)) << endl; // false && true = false cout << ((a == 0) && (a < b)) << endl; // true && false = false cout < b)) << endl; // true && true = true cout << ((a == 5) && (a < b)) << endl; return 0; )
Output
0 0 0 1
In this program, we declare and initialize two int
variables a and b with the values 5
and 9
respectively. We then print a logical expression
((a == 0) && (a> b))
Here, a == 0
evaluates to false
as the value of a is 5
. a> b
is also false
since the value of a is less than that of b. We then use the AND operator &&
to combine these two expressions.
From the truth table of &&
operator, we know that false && false
(i.e. 0 && 0
) results in an evaluation of false
(0
). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table of the &&
operator.
C++ Logical OR Operator
The logical OR operator ||
returns
true
- if one or more of the operands aretrue
.false
- if and only if all the operands arefalse
.
Truth Table of || Operator
Let a and b be two operands. Then,
a | b | a || b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
As we can see from the truth table above, the ||
operator returns false only if both a
and b
are false.
Example 2: C++ OR Operator
// C++ program demonstrating || operator truth table #include using namespace std; int main() ( int a = 5; int b = 9; // false && false = false cout < b)) << endl; // false && true = true cout << ((a == 0) || (a < b)) << endl; // true && false = true cout < b)) << endl; // true && true = true cout << ((a == 5) || (a < b)) << endl; return 0; )
Output
0 1 1 1
In this program, we declare and initialize two int
variables a and b with the values 5
and 9
respectively. We then print a logical expression
((a == 0) || (a> b))
Here, a == 0
evaluates to false
as the value of a is 5
. a> b
is also false
since the value of a is less than that of b. We then use the OR operator ||
to combine these two expressions.
From the truth table of ||
operator, we know that false || false
(i.e. 0 || 0
) results in an evaluation of false
(0
). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table of ||
operator.
C++ Logical NOT Operator !
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.
Hakikat Tablosu! Şebeke
Let bir bir işlenen olacak. Sonra,
Örnek 3: C ++! Şebeke
// C++ program demonstrating ! operator truth table #include using namespace std; int main() ( int a = 5; // !false = true cout << !(a == 0) << endl; // !true = false cout << !(a == 5) << endl; return 0; )
Çıktı
1 0
Bu programda, int
değer ile bir a değişkenini bildirip başlatıyoruz 5
. Daha sonra mantıksal bir ifade yazdırıyoruz
!(a == 0)
Burada, a == 0
a'nın false
değeri olarak değerlendirilir 5
. Ancak, biz DEĞİL operatörünü kullanmak !
üzerine a == 0
. Olarak a == 0
değerlendirildiği için false
, !
operatör sonuçlarını ters çevirir a == 0
ve nihai sonuç olur true
.
Aym şekilde, !(a == 5)
nihai olarak döner false
, çünkü a == 5
bir true
.