У овом упутству ћемо научити о релационим и логичким операторима уз помоћ примера.
У Ц ++, релациони и логички оператори упоређују два или више операнда и враћају true
или false
вредности.
Ове операторе користимо у доношењу одлука.
Релативни оператори Ц ++
Релациони оператор се користи за проверу односа између два операнда. На пример,
// checks if a is greater than b a> b;
Овде >
је релациони оператор. Проверава да ли је а веће од б или није.
Ако је релација тачна , враћа 1, док ако је релација нетачна , враћа 0 .
Следећа табела резимира релационе операторе који се користе у Ц ++.
Оператор | Значење | Пример |
---|---|---|
== | Је једнако | 3 == 5 даје нам лажне |
!= | Неједнако са | 3 != 5 даје нам истину |
> | Веће од | 3> 5 даје нам лажне |
< | Мање од | 3 < 5 даје нам истину |
>= | Већи или једнак | 3>= 5 дајте нам лажне |
<= | Мање од или једнако | 3 <= 5 даје нам истину |
== Оператор
==
Враћа се једнако оператеру
true
- ако су оба операнда једнака или истаfalse
- ако су операнди неједнаки
На пример,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Напомена: Релацијски оператор ==
није исто што и оператор додјеле =
. Оператор =
доделе додељује вредност променљивој, константи, низу или вектору. Не упоређује два операнда.
! = Оператор
!=
Враћа се оператер који није једнак
true
- ако су оба операнда неједнакаfalse
- ако су оба операнда једнака.
На пример,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Оператер
>
Враћа се оператер већи од
true
- ако је леви операнд већи од десногfalse
- ако је леви операнд мањи од десног
На пример,
int x = 10; int y = 15; x> y // false y> x // true
<Оператор
<
Враћа се оператор мање од
true
- ако је леви операнд мањи од десногfalse
- ако је леви операнд већи од десног
На пример,
int x = 10; int y = 15; x < y // true y < x // false
> = Оператор
Поврат >=
оператора већи или једнак
true
- ако је леви операнд већи или једнак десномfalse
- ако је леви операнд мањи од десног
На пример,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Оператор
The less than or equal to operator <=
returns
true
- if the left operand is either less than or equal to the rightfalse
- if the left operand is greater than right
For example,
int x = 10; int y = 15; x> y // false y> x // true
In order to learn how relational operators can be used with strings, refer to our tutorial here.
C++ Logical Operators
We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Operator | Example | Meaning |
---|---|---|
&& | expression1 && expression 2 | Logical AND. true only if all the operands are true. |
|| | expression1 || expression 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.
Табела истине! Оператор
Нека а буде операнд. Онда,
Пример 3: Ц ++! Оператор
// 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; )
Оутпут
1 0
У овом програму декларишемо и иницијализујемо int
променљиву а са вредношћу 5
. Затим исписујемо логички израз
!(a == 0)
Овде a == 0
вреднује false
као вредност а 5
. Међутим, користимо оператер НОТ !
на a == 0
. Од a == 0
процене на false
, !
оператор инвертира резултате a == 0
и коначни резултат је true
.
Слично томе, израз се !(a == 5)
на крају враћа false
јер a == 5
је true
.