This chapter is from the book

This chapter is from the book

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.

Investigate variable and value conversions.

Explore bitwise operationsif you're brave enough.

Displaying Basic Information

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.

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?

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:

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 1213 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 1516 show the
System.Console.Write() routine. These two lines print on the same line.
There is no return linefeed after printing. Lines 1920 show each of these
routines with the use of a parameter. Lines 23 and 25 also show these routines
printing multiple values from variables.