Manipulating Values in Your C# Programs (
Page 1 of 2 )
This sample book chapter, from Sams Teach Yourself the C# Language in 21 Days, will give new C# programmers an introduction to operator precedence, bitwise operati
Now that you know how to store information in variables, you'll want to
do something with that information. Most likely, you'll want to manipulate
it by making changes to it. For example, you might want to use the radius of
a circle to find the area of the circle. Today you...
Learn two ways of displaying basic information.
Discover the types and categories of operators available in C#.
Manipulate information using the different operators.
Change program flow using the if command.
Understand which operators have precedence over others.
Before you learn how to manipulate values stored in variables, it is worth
taking a few minutes to learn how to display basic information. You can use two
routines to display information. When you understand these routines, you will be
able to display basic information to the console.
ADVERTISEMENT
The two routines that you will use throughout this book to display basic
information are as follows:
System.Console.WriteLine()
System.Console.Write()
Both print information to the screen in the same manner, with only one small
difference. The WriteLine() routine writes information and then goes to
a new line. The Write()routine does not go to a new line when
information is written.
The information that you will display on the screen is written between the
parentheses. If you are printing text, you include the text between the
parentheses and within double quotes. For example, the following prints the text
Hello World:
System.Console.WriteLine("Hello World");
This prints Hello World on the screen. The following examples
illustrate other text being printed:
System.Console.WriteLine("This is a line of text");
System.Console.WriteLine("This is a second line of text");
If you execute these consecutively, you see the following displayed:
This is a line of text
This is a second line of text
Now consider the following two lines. If these execute consecutively, what do
you see printed?
you are not correct! Instead, the following is printed:
Hello
World!
Notice that each word is on a separate line. If you execute the two lines
using the Write() routine instead, you get the results you want:
Hello World!
As you can see, the difference between the two routines is that
WriteLine() automatically goes to a new line after the text is
displayed, whereas Write() does not.
Displaying Additional Information
In addition to printing text between quotation marks, you can pass values to
be printed within the text. Consider the following example:
int nbr = 456;
System.Console.WriteLine("The following is a number: {0}", nbr);
This prints the following line:
The following is a number: 456
As you can see, the {0} gets replaced with the value that follows
the quoted text. In this case, the value is that of a variable, nbr,
which equals 456. The format is as shown here:
System.Console.WriteLine("Text", value);
Text is almost any text that you want to display. The {0} is a
placeholder for a value. The brackets indicate that this is a placeholder. The
0 is an indicator for using the first item following the quotation
marks. A comma separates the text from the value to be placed in the
placeholder.
You can have more than one placeholder in a printout. Each placeholder is
given the next sequential number:
System.Console.Write("Value 1 is {0} and value 2 is {1}", 123, "Brad");
This prints the following line:
Value 1 is 123 and value 2 is Brad
Listing 3.1 presents System.Console.Write and
System.Console.WriteLine in action.
Listing 3.1 Display.csUsing WriteLine() and
Write()
Remember that to compile this listing from the command line, you enter the
following:
csc Display.cs
If you are using an integrated development tool, you can select the Compile
option.
First WriteLine Line
Second WriteLine Line
First Write LineSecond Write Line
WriteLine: Parameter = 123
Write: Parameter = 456
WriteLine: val1 = 321 val2 = 123.45
Write: val1 = 321 val2 = 123.45
This listing defines two variables that will be printed later in the listing.
Line 9 declares an integer and assigns the value 321 to it. Line 10
defines a double and assigns the value 123.45.
Lines 1213 print two pieces of text using
System.Console.WriteLine(). You can see from the output that each of
these prints on a separate line. Lines 1516 show the
System.Console.Write() routine. These two lines print on the same line.
There is no return linefeed after printing. Lines 1920 show each of these
routines with the use of a parameter. Lines 23 and 25 also show these routines
printing multiple values from variables.
You will learn more about using these routines throughout this book.