Difference between revisions of "Operators"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
m
Line 19: Line 19:
 
| Modulus || Performed using the "%" operator || Finds the remainder of a division between values. || <tt>10 % 3 = 1</tt>
 
| Modulus || Performed using the "%" operator || Finds the remainder of a division between values. || <tt>10 % 3 = 1</tt>
 
|}
 
|}
 +
Note: there are variations on the increment and decrement operators. These can be performed in both prefix and postfix forms. In prefix form, (syntax "++x;") the variable will be incremented or decremented, and then the value will be used by the function calling it. In postfix form, however, (syntax "x++;") the variable will be used by the function calling it, and be incremented or decremented afterwards.
 
<h2>Bitwise operators</h2>
 
<h2>Bitwise operators</h2>
 
{| class="wikitable"
 
{| class="wikitable"
Line 32: Line 33:
 
| NOT || performed using "!" || Returns false if true, or true if false. || <tt><nowiki>!</nowiki>true = false</tt>
 
| NOT || performed using "!" || Returns false if true, or true if false. || <tt><nowiki>!</nowiki>true = false</tt>
 
|}
 
|}
 +
The "and" and "or" operators also have variants when combined with "not", known as "nand" and "nor", but these cannot be performed directly in C#. and instead must be performed by combining the "and"/"or" with the "not" function.

Revision as of 22:49, 15 December 2016

There are seven major types of mathematical operator, and four major types of bitwise operator.

Mathematical operators

Operation Character used Description Example
Addition Performed using the "+" operator Adds values together. A + B = 12
Subtraction Performed using the "-" operator Subtracts values. A - B = 8
Multiplication Performed using the "*" operator Multiplies values together. A * B = 20
Division Performed using the "/" operator Divides values. S / B = 5
Increment Performed using the "++" operator Adds 1 to a variable and saves the variable. A++ = 11
Decrement Performed using the "--" operator Subtracts 1 from a variable and saves the variable. B-- = 9
Modulus Performed using the "%" operator Finds the remainder of a division between values. 10 % 3 = 1

Note: there are variations on the increment and decrement operators. These can be performed in both prefix and postfix forms. In prefix form, (syntax "++x;") the variable will be incremented or decremented, and then the value will be used by the function calling it. In postfix form, however, (syntax "x++;") the variable will be used by the function calling it, and be incremented or decremented afterwards.

Bitwise operators

Operation Character used Description Example
AND Performed using "&" or "&&" Returns true if both inputs are true. true && false = false
OR performed using "|" or "||" returns true if one or both inputs are true. true || false = true
XOR Performed using "^" Returns true if both inputs are different. true ^ false = true
NOT performed using "!" Returns false if true, or true if false. !true = false

The "and" and "or" operators also have variants when combined with "not", known as "nand" and "nor", but these cannot be performed directly in C#. and instead must be performed by combining the "and"/"or" with the "not" function.