• No se han encontrado resultados

VI. P OÉTICA D EL T ALLER Y C REACIÓN D E M UNDOS

3. A PERTURA D E P OSIBILIDADES : P ARA C ONSTRUIR L A I DEA

to declare constants, you may be wondering, Why use constants at all? The reason is that constants make your code easier to read and maintain.

Although constants are useful for values that never will change, constants perhaps are even more useful for values that someday may change. For example, we've all paid sales tax on purchases. Assuming the tax rate is 8%, the amount of the tax is price * .08. Thus, throughout your code for a store you may have calculations such as the following:

[price variable] * .08;

One day the government decides to increase the sales tax to 8.25%. Now you have to find all the places in your code where you referred to the sales tax rate and change all those references from .08 to .0825. This not only is a pain, but the potential for error is obvious.

Alternatively, you could have declared the sales tax rate as a constant:

const double SALES_TAX_RATE = .08;

Thus, the tax calculation in your code would be this:

[price variable] * SALES_TAX_RATE;

Then, when the government increases the sales tax to 8.25%, you only have to make the change in one place in your code, and you're done:

Conclusion

Most programs need to keep track of information. That information may be about the subject of the program, such as the names and addresses of customers, or it may be about the program itself, such as the height, caption, or visibility of a form.

Data comes in different forms. Data may be numeric (such as the height of a form), text (such as the caption on a form), or Boolean (such as whether a form is visible). The type of information, whether number, string, or Boolean, is referred to as the data type.

Although the .NET Framework class library has many built-in properties to store data, Visual C# 2005 also enables you to create your own information storage locations, called variables. Variables must be declared before they are used.

Variables may be declared at the top of the code module, in which case they are called module level and will be available to all procedures in that module. Variables also may be declared inside a procedure, in which case they are called local and their scope is limited to the procedure in which they were declared.

Finally, certain values never change during the life of the program. These unchanging values are represented by constants, which are declared similarly to variables. However, unlike variables, constants must be initialized when they are declared, and their value cannot thereafter change during the lifetime of the program.

In this chapter, you used the assignment operator to provide values to

variables. In the next chapter, you will learn about arithmetic operators, which enable you to use the computer's unparalleled ability to quickly and accurately perform mathematical calculations.

Quiz

1. What does a data type signify?

2. What is a floating-point number?

3. Can you change the data type of a built-in property of a form, such as Height or Text?

4. What is the purpose of a variable?

5. Does C# require you to declare a variable before you refer to it in code?

6. What is a local variable?

7. What is a class member variable?

8. Do you have to assign a value to a variable when you declare it?

9. What is a difference between a constant and a variable?

10. Do you have to assign a value to a constant when you declare it?

Answers

1. A data type signifies whether the data is numeric, text, yes/no, and so forth.

2. A floating-point number is a number that may have a value to the right of the decimal point. 3. No, you cannot change the data type of a built-in property of a form.

4. The purpose of a variable is to store data of your choosing.

5. Yes, C# requires you to declare a variable before you refer to it in code. 6. A local variable is a variable declared inside of a procedure.

7. A class member-level variable is declared as a member of a class. 8. No, you do not have to assign a value to a variable when you declare it.

9. A constant's value cannot change during the life of the program, whereas a variable's value may change during the life of the program.

Chapter 5: Letting the Program Do the

Math—Arithmetic Operators

Overview

It is only fair that since my students have to listen to my recycled jokes, you have to read my recycled introductions. Back in Chapter 2, I complained that nowadays students don't need to be able to calculate arithmetic in their heads because they can rely on calculators. However, despite my complaining about calculators, they certainly are far faster and more accurate than I could ever hope to be. The reason is that a calculator is a computer, and computers are superstars when it comes to calculating. You harness the computer's calculating ability using arithmetic operators. You will learn in this chapter how to enable your applications to make fast and accurate calculations using arithmetic operators. At the end of this chapter, you will put what you learned into practice with the Change Machine project, a type of calculator that converts a number of pennies into dollars, quarters, dimes, nickels, and pennies.