Home > Articles

  • Print
  • + Share This
This chapter is from the book

Manipulating Variable Values with Operators

Now that you understand how to display the values of variables, it is time to focus on manipulating the values in the variables. Operators are used to manipulate information. You have used a number of operators in the programming examples up to this point. Operators are used for addition, multiplication, comparison, and more.

Operators can be broken into a number of categories:

  • The basic assignment operator

  • Mathematical/arithmetic operators

  • Relational operators

  • The conditional operator

  • Other operators (type, size)

Each of these categories and the operators within them are covered in detail in the following sections. In addition to these categories, it is important to understand the structure of operator statements. Three types of operator structures exist:

  • Unary

  • Binary

  • Ternary

Unary Operator Types

Unary operators are operators that impact a single variable. For example, to have a negative 1, you type this:

-1

If you have a variable called x, you change the value to a negative by using this line:

-x

The negative requires only one variable, so it is unary. The format of a unary variable is one of the following, depending on the specific operator:

[operator][variable]

or

[variable][operator]

Binary Operator Types

Whereas unary operator types use only one variable, binary operator types work with two variables. For example, the addition operator is used to add two values. The format of the binary operator types is as follows:

[variable1][operator][variable2]

Examples of binary operations in action are shown here:

5 + 4
3 – 2
100.4 – 92348.67

You will find that most of the operators fall into the binary operator type.

Ternary Operator Types

Ternary operators are the most complex operator type to work with. As the name implies, this type of operator works on three variables. C# has only one true ternary operator, the conditional operator. You will learn about it later today. For now, know that ternary operators work with three variables.

  • + Share This
  • 🔖 Save To Your Account