Relational Operators

English:

Relational Operators

You can also store boolean expression for bool variables, besides "true" or "false" states.
If you want to do that, you need to use relational operators.
Look at the code below:

bool checkNumber = 10 > 5;

Which value "checkNumber" stores at this moment?
In the code above, we are using one relational operator (>) to check if the value on the left is more than the value on the right.
If 10 is more than 5, do you agree that this expression is a truth?
If this boolean expression is a truth, the value of "checkNumber" is "true" too.

But if you write:

checkNumber = 2 > 514;

The value from "checkNumber" is false now, because 2 is not more than 514.
Returning "true" through from expression above, we can use other relational operator (<) which checks if value on the left is less than value on the right:

checkNumber = 2 < 514;

At this moment, "checkNumber" can return a true value, because 2 is less than 514.

There are other relational operators in C#:

> More than
< Less than
>= More or equals than
<= Less or equals than
== Equals 
!= Not equals

These operators: >, >=, <, <= can only be used within numeric and chars data type.
But == and != can be used within any data types.

The equals operator (==) checks if two values are equal, like:

bool checkValue = 5 + 5 == 12 - 2;
// Output from "checkValue" is true.

bool checkText = "Hello" == "Bye";
// Output from "checkText" is false.

And "not equals" operator (!=) checks if two values are not equal:

bool checkThis = 450 != 200;
// Output from "checkThis" is true.

bool checkOtherText = "Friend" != "Friend";
// Output from "checkOtherText" is false.

These operators only work within the variables of the same data type:

int number = 3;
char charNumber = '3';

bool check = number == charNumber;

The code above will throw an error, because the data types of those variables in expressions are different.
To solve that problem, you should convert one variable to same data type:

Convert.ToChar(number);

bool check = number == charNumber;
// Output from "check" is true.

Português-BR:

Operadores relacionais

Você também pode armazenar a expressão booleana para variáveis ​​bool, além dos estados "true" ou "false".
Se você quiser fazer isso, precisará usar operadores relacionais.
Veja o código abaixo:

bool checkNumber = 10 > 5;

Qual valor "checkNumber" armazena no momento?
No código acima, estamos usando um operador relacional (>) para verificar se o valor à esquerda é maior que o valor à direita.
Se 10 for maior que 5, você concorda que esta expressão é uma verdade?
Se essa expressão booleana for verdadeira, o valor de "checkNumber" também será "true".

Mas se você escrever:

checkNumber = 2 > 514;

O valor de "checkNumber" agora é falso, porque 2 não é maior que 514.
Retornando "true" através da expressão acima, podemos usar outro operador relacional (<) que verifica se o valor à esquerda é menor que o valor à direita:

checkNumber = 2 < 514;

Nesse momento, "checkNumber" pode retornar um valor verdadeiro, porque 2 é menor que 514.

Existem outros operadores relacionais em C#:

> Maior que
< Menor que
>= Maior ou igual a
<= Menor ou igual a
== Igual a
!= Diferente de

Estes operadores:>,>=, <, <= podem ser usados ​​apenas dentro de variáveis ​​numéricas e do tipo char.
Mas == e != Podem ser usados em qualquer tipo de dados.

O operador igual (==) verifica se dois valores são iguais, como:

bool checkValue = 5 + 5 == 12-2;
// A saída de "checkValue" é verdadeira.

bool checkText = "Olá" == "Tchau";
// A saída de "checkText" é falsa.

E o operador "diferente de" (!=) Verifica se dois valores não são iguais:

bool checkThis = 450  != 200;
// A saída de "checkThis" é verdadeira.

bool checkOtherText = "Amigo" != "Amigo";
// A saída de "checkOtherText" é falsa.

Esses operadores funcionam apenas dentro das variáveis ​​do mesmo tipo de dados:

int number = 3;
char charNumber = '3';

bool check = number == charNumber;

O código acima gerará um erro, porque os tipos de dados dessas variáveis ​​nas expressões são diferentes.
Para resolver esse problema, você deve converter uma variável no mesmo tipo de dados:

Convert.ToChar (number);

bool check = number == charNumber;
// A saída de "check" é verdadeira.

Comentários

Postagens mais visitadas deste blog

Boolean type

Assignments Operators

Increment and decrement operations