Flylib.com

Books Software

 
 
 

Expressions


Expressions

Now that you've seen how to declare and initialize variables , it's time to look at manipulating them. C# contains a number of operators for this purpose, including the = assignment operator you've used already. By combining operators with variables and literal values (together referred to as operands when used with operators), you can create expressions , which are the basic building blocks of computation.

The operators available range from the simple to the highly complex, some of which you might never encounter outside of mathematical applications. The simple ones include all the basic mathematical operations, such as the + operator to add two operands, and the complex ones include manipulations of variable content via the binary representation of this content. There are also logical operators specifically for dealing with Boolean values, and assignment operators such as = .

In this chapter, you concentrate on the mathematical and assignment operators, leaving the logical ones for the next chapter, where you examine Boolean logic in the context of controlling program flow.

Operators can be roughly classified into three categories:

  • Unary operators, which act on single operands

  • Binary operators, which act on two operands

  • Ternary operators, which act on three operands

Most operators fall into the binary category, with a few unary ones, and a single ternary one called the conditional operator (the conditional operator is a logical one, that is it returns a Boolean value; this is discussed in Chapter 4).

Let's start by looking at the mathematical operators, which span both unary and binary categories.

Mathematical Operators

There are five simple mathematical operators, two of which have binary and unary forms. In the next table, I've listed each of these operators, along with a quick example of its use and the results when it's used with simple numeric types (integer and floating point).

Operator

Category

Example Expression

Result

+

Binary

var1 = var2 + var3;

Var1 is assigned the value that is the sum of var2 and var3 .

-

Binary

var1 = var2 - var3;

Var1 is assigned the value that is the value of var3 subtracted from the value of var2 .

*

Binary

var1 = var2 * var3;

Var1 is assigned the value that is the product of var2 and var3 .

/

Binary

var1 = var2 / var3;

Var1 is assigned the value that is the result of dividing var2 by var3 .

%

Binary

var1 = var2 % var3;

Var1 is assigned the value that is the remainder when var2 is divided by var3 .

+

Unary

var1 = +var2;

Var1 is assigned the value of var2 .

-

Unary

var1 = -var2;

Var1 is assigned the value of var2 multiplied by –1.



Summary

In this chapter, you covered a fair amount of ground on the way to creating usable (if basic) C# applications. You've looked at the basic C# syntax and analyzed the basic console application code that VS generates for you when you create a console application project.

The major part of this chapter concerned the use of variables. You have seen what variables are, how you create them, how you assign values in them, and how you manipulate them and the values that they contain. Along the way, you've also looked at some basic user interaction, by showing how you can output text to a console application and read user input back in. This involved some very basic type conversion, a complex subject that is covered in more depth in Chapter 5.

You have also seen how you can assemble operators and operands into expressions and looked at the way these are executed, and the order in which this takes place.

Finally, you looked at namespaces, which will become more and more important as the book progresses. By introducing this topic in a fairly abstract way here, the groundwork is completed for later discussions.

In this chapter, you learned:

  • How basic C# syntax works

  • What Visual Studio does when you create a console application project

  • How to understand and use variables

  • How to understand and use expressions

  • What a namespace is

So far, all of your programming has taken the form of line-by-line execution. In the next chapter, you see how to make your code more efficient by controlling the flow of execution using looping techniques and conditional branching.