click below
click below
Normal Size Small Size show me how
Operators
In C#, operators are like special symbols or keywords that you use to perform op
Parent Term | Parent Definition | Sub Term 1 | Sub Definition 1 | a) term | b) definition | c) example | Sub Term 2 | Sub Definition 2 |
---|---|---|---|---|---|---|---|---|
C# Operators: Why? | an operation is a calculation that takes 2 numbers and does something with them to get a result. Addition and multiplication are operations | |||||||
operations | 1. operators (+ or * ) 2. operands (what the operators work on like 2,3,etc, | |||||||
Operators | + - * % / | |||||||
Operands | the number or what the operator is working on | |||||||
Binary Operators | Require 2 operands | |||||||
Unary Operators | one operand | Increment: | the ++ operator is used to increment the value of an integer. | pre-increment operator | ++ placed before the variable name, the value will increment instantly | int x = 3; int y = ++x; // x is 4 and y is 4. | Post-increment Operatorq | placed after the variable name. it's value is preserved temporarily until the execution of this statement and gets updated before the execution of the next statements for ex: x++ |
Ternary Operators | 3 operands | |||||||
Arithmetic Operator | These are used to perform arithmetic/mathematical operations on operands | |||||||
Binary Arithmetic Operators: | addition subtraction, multiplication, division, modulus | |||||||
Modulus | % - this operator returns the remainder when the first operand is divided by the second. ex: x%y | |||||||
Decrement: Unary Operator | the -- operator is used to decrement the value of an integer | pre-decrement operator | placed before the variable name and the value is decremented instantly | int b = 5; Console.WriteLine(--b); //outputs 4 | post-decrement operator | value is preserved temporarily until the execution of this statement and gets updated before the execution of the next statement | int a = 4; Console.WriteLine(a--); //outputs 4 | |
Relational Operators | used for comparison of two values | see next 6 parent terms | ||||||
== Equal To operator | checks to see if two operands are equal to each other and will return true or false | 5==5 will return true | Relational Operator | |||||
!= Not Equal To operator | checks if the operands are not equal to each other. | 5!=5 will return false | Relational Operator | |||||
> Greater than | returns true if the first operand is greater than the second | 6>5 will return true | Relational Operator | |||||
< Less than operator | returns true if the first operand is less than the 2nd. | 6<5 will return false | Relational Operator | |||||
>= Greater than or Equal To operator | checks if 1st operand is greater than or equal to the second. if yes, will return true | Relational Operator | ||||||
<= Less than or Equal To | see last definition | Relational Operator | ||||||
Logical Operators | Used to combine Two or More conditions or to complement the evaluation of the original condition in consideration | see next 3 parent terms and definitions | ||||||
&& AND Operator | returns true when BOTH of the conditions in consideration are satisfied. | 5==5 and 5>10 returns false because both are not true | (logical operator) | |||||
|| The OR Operator | returns true when one or BOTH conditions are satisfied | (logical operator) | ||||||
! The NOT Operator | returns true if the condition in consideration is not satisfied. otherwise it returns false. | !a returns true if a is false | logical operator | aka the Opposite Operator | ||||
Opposite Operator | !: The NOT Operator | |||||||
Assignment Operators | Used to assign a value to a variable | The left side operand of the assignment operator is a variable and the right side operand is the value. | The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error | 6 assignment operators | ||||
= Simple Assignment Operator | a= 10; b= 20; ch = 'y'; | |||||||
=+ Add Assignment | combination of + and = operators. first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left. | (a+=b) // same as (a = a + b) // If the initial value stored in a is 5 and b is 6, then (a+b) = 11. | int a = 5; int b = 6; a += b; Console.WriteLine(a); // output is 11 | |||||
-= Subtract Assignment | First subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left. | ( a -= b) // same as (a = a - b) // if he initial value stored in a is 5 and b is 6, then (a-=b)=-1 | int a = 5; int b = 6; a -= b; Console.WriteLine(a); // outputs -1 | |||||
*= Multiply Assignment | First multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left. | (a *= b) // same as (a = a * b) | ||||||
/= Division Assignment | first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left. | (a /= b) // same as (a = a / b) | ||||||
%= Modulus Assignment | combination of % and = operators. First modulos the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left. | (a %= b) // same as (a = a % b) // if the initial value stored in a is 5 and b is 6, then (a %= b) = 5 | ina a =5; int b =6; a %= b; Console.WriteLine(a); // outputs 5 (special case) | |||||
Inline-If Operator (aka ternary operator) | shorthand version of the if-else statement and has 3 operands. | will return one of two values depending on the value of a Boolean expression. | ||||||
Condition ? | ternary operator that must be evaluated to be true or false | |||||||
trueScope : | if the condition is true, the trueScope is evaluated and becomes the result | |||||||
falseScope; | If the condition is false, the falseScope is evaluated and becomes the result. | |||||||
var i = 1; | var positiveMessage = (i > 0) ? "You are positive!" : "You're not positive."; | |||||||
Null Coalescing Operator | like saying if the first thing is empty (null) then use the second thing. | allows you to conditionally evaluate an expression and provides a fallback value in the event the expression is null | ||||||
Null | Nothing or empty. | If a variable is set to 'null' it doesnt have any value or object assigned to it. -- like an empty box waiting to be filled | ||||||
Null-Conditional Operator | applies an operation to its operand only if the operand is non-null. Otherwise, the result of applying the operator is null | '?' IS IMPORTANT because it is used as a Verification Process. It will ONLY execute an operation if the code is properly set up. Acts as a null check at run-time. | ||||||
C# Operators: | Special symbols or keywords that you use to perform operations on different pieces of data | 1. arithmetic operators 2. Relational or Comparison Operators 3. Logical Operators 4.Assignment Operators | ||||||
arithmetic operators | used for basic math operations | +, =, *, /, % | ||||||
relational operators | help you compare two values. | ==, !=, <, >, <=, >= | ||||||
logical operators | these are used to combine multiple conditions | &&, ||, ! | ||||||
assignment operators | used to assign value to variables | =, +=, -=, *=, /=, |