Home > Articles

Manipulating Values in Your Programs

  • Print
  • + Share This
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?

System.Console.WriteLine("Hello ");

If you guessed that these would print

Hello World!

you are not correct! Instead, the following is printed:


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.cs—Using WriteLine() and Write()

 1: // Display.cs - printing with WriteLine and Write
 2: //-----------------------------------------------
 4: class Display
 5: {
 6:  public static void Main()
 7:  {
 9:    int iNbr = 321;
10:    double dNbr = 123.45;
12:    System.Console.WriteLine("First WriteLine Line");
13:    System.Console.WriteLine("Second WriteLine Line");
15:    System.Console.Write("First Write Line");
16:    System.Console.Write("Second Write Line");
18:    // Passing literal parameters
19:    System.Console.WriteLine("\nWriteLine: Parameter = {0}", 123 );
20:    System.Console.Write("Write: Parameter = {0}", 456);
22:    // Passing variables
23:    System.Console.WriteLine("\nWriteLine: val1 = {0} val2 = {1}", 
24:                iNbr, dNbr );
25:    System.Console.Write("Write: val1 = {0} val2 = {1}", iNbr, dNbr);
26:  }
27: }

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 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.

You will learn more about using these routines throughout this book.


The first placeholder is numbered 0, not 1.

  • + Share This
  • 🔖 Save To Your Account