Fundamental Operators

Introduction to Operators

An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by combining existing values. An operation uses an operator and one or more operands.

Introduction to Unary Operators

A unary operator is an operator that performs its operation on one operand. A binary operator is an operator that acts on two operands.

The Assignment =

The assignment operation, represented with the = symbol, gives a value to a variable or an object. The formula to use it is:

`variable-name = value`

Here is an example:

```// Declaring and initializing a variable using the assignment operator
var salary = 12.55;
}```

You can also declare various variables and initialize each variable by assigning it the desired value before the comma or the semi-colon. Here is an example:

```// Initializing various variables when declaring them with the same data type
var value1 = 224.58, value2 = 1548.26;
}```

The Negative Operator -

The negative operator uses the - symbol to represent a number less than 0.

Introduction

The addition is used to add two or more values. Thie operation uses the + (plus) symbol.

Besides numbers, you can add strings. This is done using the + operator between the strings. An example is "Pie" + "Chart". Like the addition of numbers, the addition of strings produces a new string. For example, "Pie" + "Chart" produces "PieChart".

If the strings are stored in variables, you can perform the addition on those variables. Here is an example:

```var prefix = "De";
var rest = "Niro";

var name = prefix + rest;```

This would produce:

`DeNiro`

Just like you can add various numbers, you can add different strings. Here is an example:

```var firstName = "Samuel";
var lastName = "Eto'o";
var suffix = "Fils";

var name = firstName + " " + lastName + ", " + suffix;;```

This would produce:

`Samuel Eto'o, Fils`

Incrementing a Variable

To increment a value, add 1 to it. Here is an example:

```var value = 12;

value = value + 1;```

An alternative is to use the ++ unrary operator. That is, instead of writing value = value + 1, you can write value++ and you would get the same result. Here is an example:

```var value = 12;

value++;```

Pre and Post-Increment

When using the ++ operator, the position of the operator with regards to the variable it is modifying can be significant. Here is an example of the ++ operator applied before a variable:

```var value = 12;

++value;```

Here is an example of the operator used after the operator:

```var value = 12;

value++;```

Introduction

Consider the following code:

```var value = 12.75;
var newValue;

newValue = value + 2.42;```

This technique requires that you use an extra variable in your application. As an alternative, to add a value to a variable and change the value that the variable is holding, you can combine the assignment "=" and the addition "+"� operators to produce a new operator as +=. Here is an example:

```var value = 12.75m;

value += 2.42;```

Consider the following example:

```var name = "Jim";
name = name + "my";```

This would produce the name variable as:

`Jimmy`

As an alternative, you can use the compound addition, the same way it is used for numbers. Here is an example:

`name += "my";`

The Multiplication Operations

Introduction

The multiplication is done using the * operator. Here is an example:

```var value1 = 224.58, value2 = 1548.26;
var result = value1 * value2;```

The operation can involve two or more operands, as in a * b * c, which is the same as c * b * a.

Compound Multiplication

You can multiply a value by a variable and assign the result to the same variable. Here is an example:

```var value = 12.75;

value = value * 2.42;```

As an alternative, C-based languages support the compound multiplication assignment operator represented as *=. To apply it, use the *= operator and assign the desired value to the variable. Here is an example:

```var value = 12.75;

value *= 2.42;```

The Subtraction Operations

Introduction

The subtraction is performed with the - symbol. Here is an example:

```var value1 = 224.58, value2 = 1548.26;
var result = value1 - value2;```

Decrementing a Variable

Consider the following code:

```var value = 12;

value = value - 1;```

As an alternative, you can use the decrement operator --. Here is an example:

```var value = 12;

value--;```

Pre-Decrementing a Value

The -- operator can be be used before its operand. Here is an example:

```var value = 12;

--value;```

The operator can also be used after its operand. Here is an example:

```var value = 12;

Value--;```

Compound Subtraction

To decrement a value from a variable, use the -= operator. Here is an example:

```var value = 12.75;

value -= 2.42;```

The Division Operation

Introduction

The division is used with the / operator to get the fraction of one number in terms of another. When performing this operation, never divide a number by zero (0).

Compound Division

Consider the following example:

```var value = 12.75;

value = value / 2.42;```

An alternative is to use the /= operator. Here is an example:

```var value = 12.75m;

value /= 2.42;```

The Remainder

Introduction

The remainder operation is used to get the value remaining after a division renders a natural result. This operation is performed with the percent sign (%). Here is an example:

```var players = 18;

var remainder = players % 11;```

The Compound Remainder

Consider the following example:

```var players = 18;

int rem = players % 11;```

As an alternative, you can use the compound remainder operator represented as %=. Here is an example:

```var players = 18;

players %= 11;``` Practical Learning: Ending the Lesson