A Second Simple Program


Perhaps no other construct is as important to a programming language as the assignment of a value to a variable. A variable is a named memory location that can be assigned a value. Further, the value of a variable can be changed during the execution of a program. That is, the content of a variable is changeable, not fixed.

The following program creates two variables called x and y.

 // This program demonstrates variables. using System; class Example2 {   public static void Main() {     int x; // this declares a variable     int y; // this declares another variable     x = 100; // this assigns 100 to x     Console.WriteLine("x contains " + x);     y = x / 2;     Console.Write("y contains x / 2: ");     Console.WriteLine(y);   } }

When you run this program, you will see the following output:

 x contains 100 y contains x / 2: 50

This program introduces several new concepts. First, the statement

 int x; // this declares a variable

declares a variable called x of type integer. In C#, all variables must be declared before they are used. Further, the kind of values that the variable can hold must also be specified. This is called the type of the variable. In this case, x can hold integer values. These are whole numbers. In C#, to declare a variable to be of type integer, precede its name with the keyword int. Thus, the preceding statement declares a variable called x of type int.

The next line declares a second variable called y.

 int y; // this declares another variable

Notice that it uses the same format as the first except that the name of the variable is different.

In general, to declare a variable, you will use a statement like this:

 type var-name;

Here, type specifies the type of variable being declared, and var-name is the name of the variable. In addition to int, C# supports several other data types.

The following line of code assigns x the value 100:

 x = 100; // this assigns 100 to x

In C#, the assignment operator is the single equal sign. It copies the value on its right side into the variable on its left.

The next line of code outputs the value of x preceded by the string “x contains “.

 Console.WriteLine("x contains " + x);

In this statement, the plus sign causes the value of x to be displayed after the string that precedes it. This approach can be generalized. Using the + operator, you can chain together as many items as you want within a single WriteLine( ) statement.

The next line of code assigns y the value of x divided by 2:

 y = x / 2;

This line divides the value in x by 2 and then stores that result in y. Thus, after the line executes, y will contain the value 50. The value of x will be unchanged. Like most other computer languages, C# supports a full range of arithmetic operators, including those shown here:

+

Addition

Subtraction

*

Multiplication

/

Division

Here are the next two lines in the program:

 Console.Write("y contains x / 2: "); Console.WriteLine(y);

Two new things are occurring here. First, the built-in method Write( ) is used to display the string “y contains x / 2:”. This string is not followed by a new line. This means that when the next output is generated, it will start on the same line. The Write( ) method is just like WriteLine( ), except that it does not output a new line after each call. Second, in the call to WriteLine( ), notice that y is used by itself. Both Write( ) and WriteLine( ) can be used to output values of any of C#’s built-in types.

One more point about declaring variables before we move on: It is possible to declare two or more variables using the same declaration statement. Just separate their names by commas. For example, x and y could have been declared like this:

 int x, y; // both declared using one statement




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net