Operator and Expressions

In your programming you are going to need or build expressions which will help you in your calculation. For example you calculate average of some number, for that purpose you are going to build expression. Expressions are built with operators and operands. Operators are symbols or functions that tells compiler to perform specific mathematical or logical operations and operands are something on which operators work.
We are going to discuss following topics in this article.
  1. Arithmetic Operator (+,-,*, /, %)
  2. Assignment Operator (=, +=, -=, /=, *=, %=)
  3. Relational Operator (==, >=, <=, >, <, !=)
  4. Logical Operator (&&, ||, !)
  5. Increment and Decrement Operator
  6. Postfix and Prefix Notation
  7. Ternary Operator (? :)
  8. Operator Precedence
Arithmetic Operator
These are simple mathematical operation you must be familiar with. It is simple addition, subtraction, multiplication and division. But there is one operator with whose use you might not be familiar with and that is modulo operator (%). Modulo operator (%) also known as remainder operator is used to get the remainder of two numbers. For example if you divide 10 with 3 then the left over (remainder) is 1 that will be the answer if you use modulo operator rather than division operator (/).

Example
6+3 = 9
6-3 = 3
6*3 = 18
6/3 = 2
6%4 = 2

Assignment Operator
If you want to assign a value to your variable then you have to use assignment operator (=). Unlike traditional mathematics when you use equal to sign with variable and any number it does not mean that they are equal but that means your variable should hold that value.

Example
int a = 5; //it means that the value of a is 5
a = 7;      //it means from now on the value of a will be 7

You can also assign the value of one variable to another variable and you can also perform some mathematical operation and then assign the value to the variable.

Example
int result = a + b;

Here the values of a and b will be added and there result will be stored in the variable named result. Not that in this operation the value of a and b will be unchanged.

You can also perform mathematical operation on a variable and store the value back in to it.

Example
balance = balance + 10;

In this case the 10 will be added in the value of balance. Actually this operation is so common that there is short hand notation for this.

balance += balance;

That will do exactly the same as above statement works. And it is true for all mathematical operations.

Relational Operator
These are operators that are used for comparison. These operators are used to ask questions from complier if certain relation exist. Like if you want to perform a equality check you can use a relational operators of equality check that are equal to (==) and not equal to (!=).

Example

int a = 5;
int b = 5;
int c = 10;
a == b; // ask complier if a is equal to b? The complier will answer true.
a != c; // ask compiler if a is not equal to c?. Compiler will answer true.
Just like this we have following other operators.
a < b; // ask compiler if a is less than b?
a <= b; // ask compiler if a is less than or equal to b?
a > b; // ask compiler if a is greater than b?
a >= b; // ask compiler if a is greater than or equal to b?
These operator are generally used in conditional blocks like if else or in while loops.

Logical Operators
Logical operator can be used to combine more than one conditional statement. Depending on which conditional operator you use the answer will change. There are following logical operators.

AND Operator (&&)
AND operator will only result in true if all the conditions that are combined with AND operator is result in true. Note that AND operator is written with two ‘&’ signs without space.

Example

if (a == b && c == d)
            {
                Console.WriteLine("Both conditions result in true.");
            }

OR Operator (||)

OR operator will result in true if any of the conditions that are combined with OR operator is result in true. Note that OR operator is written with two ‘|’ signs without space.

Example
if (a == b || c == d)
            {
                Console.WriteLine("Atlest one of condition is true.");
            }
NOT Operator (!)
NOT operator works as negotiation. You can put the NOT operator in front of any Boolean expression and it will change its value to opposite.

Example

bool a = true;
bool b = !a;   // the value of b will be false

Increment and Decrement Operator
We have already discussed mathematical operator in which we studied addition and subtraction. It is so general in programming that we have short hand notation for it (i.e. += and -=). But when we want to just increment or decrement the value of variable just by one we have increment operator and decrement operator (i.e. ++ and --).

Example

            int a = 10;
            a++;
            Console.WriteLine(a);  //11
            a--;
            Console.WriteLine(a);  //10

Postfix and Prefix Notation
Using increment and decrement operator we have option to use them before the variable or after the variable but that does have some effect on the function of program.

Postfix: When increment or decrement operator is used after the variable then it is called postfix. When postfix notation is used then the statement is executed after that value of variable is increased.

Example
            int a = 10;
            Console.WriteLine(a++); //printed value of a will be 10 then it will be incremented to 11.
Prefix: When increment or decrement operator is used before the variable then it is called prefix. When prefix notation is used then the value of variable is changed then statement is executed.

Example
            int a = 10;
            Console.WriteLine(a++); //the value will be incremented to 11 and that will be printed.

Ternary Operator (?:)

Ternary operator is bit different from other operators because it works on three operands. It works like a very compact if-else statement.

Working

 (Condition) ? true : false
? : is ternary operator. You use condition before it and if that condition proves to be true then first value that is after “?” symbol is returned and if condition proves to be false then the value second value that is after “:” symbol is returned.

Example

        static void Main(string[] args)
        {
            int a = 10;
            int b = 12;
            String result;
            result = a < b ? "condition appears to be valid" : "condition appears to be invalid";
            Console.WriteLine(result);   //output: condition appears to be valid
        }

Operator Precedence

This is one key factor that produces bugs in programming. So you should have good idea about operator precedence. It tells you which operation should be executed first and which should be executed after. For example if someone ask you what will be the result of 2+2*2. If you don’t know the operator precedence then you might solve it from left to right which will give the answer as 8 but it is wrong. The right way to do this is to first do the multiplications then do the addition because multiplication has its precedence over addition. In this way the answer will be 6.

Following is the list of precedence. The top has maximum precedence and last has minimum
  1. Multiplication                *, /, %
  2. Addition                +, -
  3. Comparison                >, <, <=, >=
  4. Equality                    ==, !=
  5. Conditional AND            &&
  6. Conditional OR                ||
  7. Ternary                    ? :
  8. Assignment                =

This was brief discussion on operator. The more you use them the more you will be comfortable with it. Hope this was helpful. Thanks

<< Decision Making: Switch Statements and tertiary Operator
Tags: C-sharp
comments powered by Disqus