# Teaching Myself C Sharp Lesson 4 Working with Operators

Oh awesome we will be working with operators, which in my mind I imagine some lady at a telephone exchange or switch board connecting me to the person I am after. Lets hope it’s that simple.

This is now Lesson 4 and all my notes and research come from:
Sams Teach Yourself C# in 21 Days

Working With Operators
Operators are used to manipulate information, they are used to perform operations such as addition, multiplication, comparison and much more and can be divided into a number of categories:
♣ The basic assignment operator.
♣ Mathematical/arithmetic operators.
♣ Relational Operators.
♣ The Conditional operator.
♣ Other operators (type, size).

In turn there are three types of operator structures:
Unary Operator Types (Not urinary ok!)
Unary operators impact a single variable. eg negative one is -1. The negative requires only one variable, so it is unary.
Binary Operator Types (Bi = 2)
Binary operator types work with…surprise – two variables. eg. The addition operator adds two values together.
Ternary Operator Types (Ter = 3)
Ternary operators are the most complex and as the name suggests, this type of operator works on three variables. fortunately C# has only one true (wahoo! I hear you say) ternary operator – the conditional operator.

Punctuators
Punctuators are a special form of operator that helps to format the code, do multiple operations at once as well as signal information to the compiler. These include:

Semicolon – (;) Primarily used to end each C# statement.
Comma – (,) Comma is used to stack multiple commands on the same line.
Parenthesis – (()) Parenthesis are used in multiple places within functions or to force the                                order of execution.
Braces – ({}) Braces are used to group pieces of code and are always used in pairs.

The Basic Assignment Operator
The basic assignment operator is an equal (=) sign. It is used to assign values. The compiler takes the value that is on the right side and places it in the variable on the left side.
eg. x = 345 – x is assigned the value of 345.

Mathematical/Arithmetic Operators
Within C# are all the basic math functions including +, -, x , ÷ and modules, there are also compound operators that make doing some of the operations more concisely.
The additive operators are used for addition (+) and subtraction (-). Which is pretty logical so therefore to perform an addition or subtraction
NewValue1 = Value1 + Value2.
NewValue2 = Value1 – Value2.
Therefore the result of NewValue1 is the result of Value1 and Value2 added together and NewValue2 is the result of Value1 subtracting Value2. (logical like I said) Any preexisting values in NewValue1 and NewValue2 are overwritten by the operators.

Multiplicative Operators
There are three multiplicative operators commonly used in c#

Multiplying
The first multiplicative operative is the multiplier operator (derrr!) which is symbolised as an asterisk (*) not the short little guy in those wonderful comics I used to get.
eg. NewValue1 = Value1 * Value2 or
Answer = 2 * 50 which means the value of Answer will be 100.

Dividing
To do division the division operator is used, which is the forward slash (/).
eg. NewValue1 = Val1 / Val2 or
Answer = 56/2 which means the value of Answer will be 28.

Working with Remainders
There are times when performing division that you want only the remainder. eg. 4/3 = 1 plus 1 remainder. You can work out this remainder only using the remaindering operator, which is the percentage (%) sign.
eg. Value1 = 4 % 3
Value1 = 1
Another example is if you have 3 yummy scrumptious chocolate cakes that can be divided into 6 pieces and 13 people are clamouring for a piece of yummy scrumptious chocolate cake.  How many pieces or slices will be left over.
PiecesofPie = 3 * 6; //therefore PiecesofPie will equal 18.
PiecesleftOver = PiecesofPie % 13; //therefore PiecesleftOver will equal 5

Compound Arithmetic Assignment Operators

The compound operators provide a concise method for performing math operations. As I am not becoming a programmer per sae I prefer to personally use the non-compound equivalent but can see how using the compound method uses less key strokes and does not require me to put in comments so that anyone looking at my code can understand what is happening (I am saying this cos most probably it will be fellow developers who don’t know this short cut 😉  )

Doing Unary Math
The arithmetic operators described and explained so far are all binary, that is they require two values to operate. There are also a number of unary operators that work with one value or variable. The unary arithmetic operators are the increment operator (++) and the decrement operator (–).
These operators add or subtract 1 to or from the value of the variable.
eg. ++x; adds 1 to x which is the same as x = x + 1
likewise
–x; subtracts 1 from x which is the same as x = x – 1
Increment and decrement operators are handy when you need to step through a lot of values 1 by 1.

As you can quite plainly see that the display shows Value1 increasing by 1 each time and Value2 decreasing by 1 each time.
Using Pre- and Post-Increment Operators
The increment and decrement operators have a unique feature that can cause new programmers to get confused. (Way ahead of you there buddy!)
eg. If we assume that x = 10 then:
y = ++x
then the value of y = 11 but if you use:
y = x++ then y = 10 not  11 (hey what the? Yes I’m confused!!)

(Now this is how the book explains it but I am not sure I understand)
The increment operator can operate as a pre-increment or a post-increment.
Pre-increment (++x) means the value is incremented before anything else.
Post-increment (x++) means the value is incremented after everything else.
(OK then, that’s as clear as mud)

After getting my head around that, I don’t think I want to use that method and stick to the ‘normal’ increment and decrement operators. I presume that the same works with the decrements, to check I changed all the ++ to — and it proved I was right in presuming this one.(Wahooooo!! In your face scripting!! (fist pump emoji))

Relational Operators
Relational operators are used to determine or compare the relationship between two values.

When using relational operators to make comparisons you get one of two results – true or false.

eg.
30<31
30 is less than 31 so this is true.
30>31
30 is not greater than 31 so this is false.
30==31
30 does not equal 31 so this is false.
30!=31
30 does not equal 31 so this is true.

All the results in the example above are either true or false which can then be used in…..

The ‘if’ Statement
The ‘if’ keyword can be used with the relational operators to make a decision that can be used to change the flow of your program.

If the comparison of Val1 to Val2 is true then the statements are executed. If the comparison is false then the statements are skipped.

If I used DOES NOT EQUAL (!=) in line 12 then the following would be displayed in the output:
Getting ready to do the if statement…
If condition was true
Done with the if statement

Notice also in Line 12 that there is no semi colon (;) after the if statement. If you make the mistake of putting a semi colon after the if statement, it will just skip the statements in the braces ({}) preceding it.

Conditional Logical Operators
There are times when you want to compare more than one statement to determine whether a block of code should be executed or skipped. Conditional logical operators will allow you to perform multiple comparisons with relational operators. The two conditional logical operators that are used are the AND operator (&&) and the OR operator (||).

Conditional AND Operator
The logical AND operator (&&) will allow you to verify a number of conditions are all met. You can place more than two relationships within a single ‘if’ statement. No matter the amount of comparisons, the comparisons on each side of the AND (&&) must be true.

Conditional OR Operator
There are times when you need only one of a number of conditions to be true. By using the logical OR operator (||) only one of the conditions needs to be true for the statements to be executed.

You can also use AND (&&) and OR (||) commands together but they must be separated by parenthesis punctuator.
eg
if (age >=21 AND (gender == male OR gender == female))
{
statement
}

The compiler will always start with the inner parenthesis first so in the example the (gender == male OR gender == female) will evaluate if either side is true. If this is true, the AND will compare the age value to see if the age value is greater than or equal to 21. If this is true then the following statement will execute. If false then the following statement will be skipped.

The Conditional Operators
As mentioned much earlier in this lesson there is one ternary operator that C# uses – the conditional operator.

The conditional operator is used to create a concise code but once again can be confusing to understand by us developers and designers but it is good to know.

Understanding Operator Precedence
The set order in which different types of operators are executed is known as operator precedence. Some operators have more precedence than others.
The arithmetic order of preference is BOMDAS:
B = Brackets
O = Of
M = Multiplication
D = Division
S = Subtraction
The operator precedence in C# is:

Conclusion Lesson 4 Working With Operators
Today I learnt about punctuators used for formatting code. These include:
• semicolon ; used to end each statement.
• Comma , used to stack multiple commands on the same line.
• Parenthesis () used to force the execution order as well as being used with functions.
• Braces {} used to group pieces of code.

I also learnt about unary (1) binary (2) and ternary (3) operator types where unary uses 1 variable, binary uses 2 variables and ternary uses 3 variables.

The basic assignment operator is = and is used to assign values.

I learnt about Mathematical/Arithmetic operators which includes adding + and subtracting as well as Multiplicative operators which includes multiplying * and dividing / .
When I want to only what the remaining amount is left over from a division equation I use the remaindering or modulus % operator.

I learnt about compound operators for more concise code +=  -=  *=  /=  %= .

I learnt about Increment ++ and decrement operators to add or subtract a value to a variable. I also learnt the difference between pre and post increment operators and how it affects the output display.

Relational operators are used to compare values and include < greater than, > less than,   == equal to, != not equal to, >= greater than or equal to, <= less than or equal to.

I learnt about ‘if’ statements and how they can be used to change the flow of your program.

The conditional AND operator && enables you to verify all conditions are met.
The conditional OR operator || enables you to match only one of a number of conditions.

The conditional operator is a ternary operator and is used to create concise code and comes in three parts. The first part being a condition followed by a ? mark to separate the statements from the condition. The : then separates the two statements.

I learnt about operator precedence and how some operators have more precedence and will be executed first.

So all in all quite an in depth lesson which has just made the light bulb in my head glow that ever more slightly. Another step in the right direction. Thank you for your company, I hope you got some value from this post.

You take care now.
Blessings and peace to you.