Course Content

C# Basics

## C# Basics

# Basic Operators & Expressions

Operators are symbols or a combination of symbols that perform various operations on values or variables.

On the other hand, an expression is a combination of values and operators that output (or return) an evaluated value. For example `7 + 9`

is an expression that returns `16`

, and `7 * 9`

is an expression that returns `63`

as the `*`

operator is the multiplication operator.

You can write expressions inside the `System.Console.Write`

or `System.Console.WriteLine`

method to see their output:

main

`System.Console.WriteLine(7 + 9);`

You can also store the result of expressions inside variables:

main

`var result = 17 + 27; System.Console.WriteLine(result);`

In this chapter, we will look at the Arithmetic operators. Most of the remaining operators will be discussed in the later sections where relevant.

**Example Usage:**

The operators are always evaluated from left to right. For-example, if we have the statement `200 / 10 / 5 / 2`

, the order of operations will be:

`200 / 10 / 5 / 2`

→ `20 / 5 / 2`

→ `4 / 2`

-> `2`

.

A statement having multiple arithmetic operators is evaluated based on the **BODMAS** (also known as **PEMDAS**) rule by default.

**BODMAS** is an acronym for Brackets, Order (Exponent), Division, Multiplication, Addition, and Subtraction. It defines the order of operations from the highest to the lowest priority of execution:

- Brackets
- Exponent
- Division
- Multiplication
- Addition
- Subtraction

The following diagram shows the general order of operations in visual form:

Note

C# does not have an operator for exponents, instead we use a method when we want to raise a number to some power.

Here is an example which shows the order of execution:

main

`int result = (10 + 5) * 2 - 8 / 4 + 1; System.Console.WriteLine(result);`

The statement in the code above is executed in the following order:

The steps explained in the picture are executed below:

- Expression:
`(10 + 5) * 2 - 8 / 4 + 1)`

- Step 1:
`15 * 2 - 8 / 4 + 1`

- Step 2:
`15 * 2 - 2 + 1`

- Step 3:
`30 - 2 + 1`

- Step 4:
`28+1`

- Step 5:
`29`

Similarly, in the case of nested brackets, inner brackets are solved first:

main

`int result = ((20 - 4) * 2) + 4; System.Console.WriteLine(result);`

Process: `((20 - 4) * 2) + 4`

→ `((16) * 2) + 4`

→ `(32) + 4`

→ `36`

We can also store values inside variables and perform operations on them:

main

`namespace TestConsoleApp { internal class Program { static void Main(string[] args) { var value_1 = 10; var value_2 = 7; System.Console.WriteLine("Value 1: " + value_1); System.Console.WriteLine("Value 2: " + value_2); var sum = value_1 + value_2; System.Console.WriteLine("Sum: " + sum); var result = (value_1 + 10) / 2; System.Console.WriteLine("(Value_1 + 10) / 2: " + result); } } }`

Note

An

expressioncan have a combination of operators, numbers and variables. Examples of expressions from the above code are

`value_1 + value_2`

and`(value_1 + 10) / 2`

. Each expression always has aresultantorreturn value.

Everything was clear?