
C # comparison operations
Comparing values when programming allows you to check the relationships between variables. The result of the check is always true or false.
Menu wpisu
The C # language provides the following comparison operators:
Operator | Znaczenie |
---|---|
> | Sprawdza czy pierwsza wartość jest większa od drugiej. |
< | Sprawdza czy pierwsza wartość jest mniejsza od drugiej. |
>= | Sprawdza czy pierwsza wartość jest większa lub równa od drugiej. |
<= | Sprawdza czy pierwsza wartość jest mniejsza lub równa od drugiej. |
== | Sprawdza czy pierwsza wartość jest równa drugiej. |
!= | Sprawdza czy pierwsza wartość jest różna od drugiej. |
Examples of the use of
a comparison of two values, and assign the result to a variable of type bool bool, check = 18 > = 7; use of the result of the comparison in the conditional statement. The resulting value It's true, so the code will perform If (check) Console. WriteLine ("Yes, 18 is greater than 7); Displays false because 45 and 12 are not equal Console. WriteLine (45! = 12); Console. WriteLine (45 = = 40 + 5); Displays a value of "true" since the operators comparisons are performed after arithmetic operations-performs Add, and later comparison. Displays false because 15 is not smaller than itself Console. WriteLine (15 < 15);
Possible error
Due to the diversity of languages a person beginning with programming in C/C++/c # sometimes have a problem with checking whether two values are equal. To check it is necessary to apply operator “==”. The application in this case, the operator “=” will be resulted in incorrect action or mistake. In C/C++ for different types, and for c # only for bool, the value instead of the comparison would be entered into the variable. The example below will bring more problem.
The following incorrect example bool, check = true,//a variable of type bool with a value of true If (check = false)//there is no comparison, the value of the variable is set set to false and the value dopiera is checked by the conditional statement if { This code does not execute } Under the second, also a mistaken example bool//zmienna = true; check type bool with a value of true If (check = true)//there is no comparison, the value of the variable is set set to true and the value dopiera is checked by the conditional statement if { This code will be } The following correct example bool, check = false; a variable of type bool with a value of false If (check == false)//the value of the variable "check" is compared to "false". The values are equal, so the if conditional statement is "true" and passes to code execution { This code will be }