Difference between revisions of "Operators"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "There are five types of mathematical operator, and four types of bitwise operator.<br /> <h2>Mathematical operators</h2> *Addition - performed using the "+" operator; adds val...")
 
(Comparison Operators)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
There are five types of mathematical operator, and four types of bitwise operator.<br />
+
There are seven major types of mathematical operator, and four major types of bitwise operator.<br />
<h2>Mathematical operators</h2>
+
==Mathematical operators==
*Addition - performed using the "+" operator; adds values together.
+
{| class="wikitable"
*Subtraction - performed using the "-" operator; subtracts values.
+
|-
*Multiplication - performed using the "*" operator; multiplies values together.
+
! Operation !! Character used !! Description !! Example
*Division - performed using the "/" operator; divides values.
+
|-
*Modulus - performed using the "%" operator; finds the remainder of a division between values.
+
| Addition || Performed using the "+" operator || Adds values together. || <tt>A + B = 12</tt>
<h2>Bitwise operators</h2>
+
|-
*AND - performed using "&" or "&&"; returns true if both inputs are true.
+
| Subtraction || Performed using the "-" operator || Subtracts values. || <tt>A - B = 8</tt>
*OR - performed using "|" or "||"; returns true if one or both inputs are true.
+
|-
*XOR - performed using "^"; returns true if both inputs are different.
+
| Multiplication || Performed using the "*" operator || Multiplies values together. || <tt>A * B = 20</tt>
*NOT - performed using "!"; returns false if true, or true if false.
+
|-
 +
| Division || Performed using the "/" operator ||  Divides values. || <tt>S / B = 5</tt>
 +
|-
 +
| Increment || Performed using the "++" operator || Adds 1 to a variable and saves the variable. || <tt>A++ = 11</tt>
 +
|-
 +
| Decrement || Performed using the "--" operator || Subtracts 1 from a variable and saves the variable. || <tt>B-- = 9</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.
 +
 
 +
==Comparison Operators==
 +
{| class="wikitable"
 +
|-
 +
! Operation !! Character used !! Description !! Example
 +
|-
 +
| Equal || Performed using == || Returns true if both inputs are the same. || <tt>value == "test"</tt>
 +
|-
 +
| Not Equal|| performed using != || returns true if both inputs are not the same || <tt>value != "test"</tt>
 +
|-
 +
|Less Than || Performed using < || Returns true if value 1 is less than or equal to value 2. || <tt>value < 0</tt>
 +
|-
 +
|Greater Than || performed using > || Returns true if value 1 is greater than value 2. || <tt>value > 1</tt>
 +
|-
 +
|Less Than or Equal || Performed using <= || Returns true if value 1 is less than or equal to value 2. || <tt>value <= 0</tt>
 +
|-
 +
|Greater Than  or Equal|| performed using >= || Returns true if value 1 is greater than or equal to value 2. || <tt>value >= 1</tt>
 +
|}
 +
 
 +
==Bitwise operators==
 +
{| class="wikitable"
 +
|-
 +
! Operation !! Character used !! Description !! Example
 +
|-
 +
| AND || Performed using "&" or "&&" || Returns true if both inputs are true. || <tt>true && false = false</tt>
 +
|-
 +
| OR || performed using "<nowiki>|</nowiki>" or "<nowiki>||</nowiki>" || returns true if one or both inputs are true. || <tt>true <nowiki>||</nowiki> false = true</tt>
 +
|-
 +
| XOR || Performed using "^" || Returns true if both inputs are different. || <tt>true ^ false = true</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.

Latest revision as of 21:30, 30 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.

Comparison Operators

Operation Character used Description Example
Equal Performed using == Returns true if both inputs are the same. value == "test"
Not Equal performed using != returns true if both inputs are not the same value != "test"
Less Than Performed using < Returns true if value 1 is less than or equal to value 2. value < 0
Greater Than performed using > Returns true if value 1 is greater than value 2. value > 1
Less Than or Equal Performed using <= Returns true if value 1 is less than or equal to value 2. value <= 0
Greater Than or Equal performed using >= Returns true if value 1 is greater than or equal to value 2. value >= 1

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.