120 likes | 134 Views
Chapter 2. Variables and Constants. Objectives. Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character variables. Explain the different floating-point types and use variables of those types. Describe Boolean variables.
E N D
Chapter 2 Variables and Constants
Objectives • Explain the different integer variable types used in C++. • Declare, name, and initialize variables. • Use character variables. • Explain the different floating-point types and use variables of those types. • Describe Boolean variables. • Use constants.
Understanding Variables • A variable is a data structure whose contents can change while a program is running. • In C++ you must select the data type that best fits the nature of the data to be stored.
Integer Data Types • An integer is a whole number. • There are many ways to store integers; each has its own range and memory requirements. • The integer data types in C++ are: char, unsigned char, short, unsigned short, int, unsigned int, long and unsigned long.
Declaring and Naming Variables • Indicating to the compiler the name and type of variable you want to use is called “declaring” the variable. • You must declare a variable for you can use it. • A typical variable declaration might look like the following: int i; // declare i as an integer
Initializing Variables • Initializing a variable means assigning a value to it. • Variable values are indeterminate when they are first declared. • You use the = sign to assign a value to a variable. • A typical variable initialization statement might look like the following: i = 2; // initialize i to 2
Naming Variables • Names of variables are called identifiers. • Identifiers must start with a letter or an underscore (_). • You can use letters, numerals, or underscores in the rest of the identifier. • Use names that make the purpose of the variable clear. • There can be no spaces in identifiers. • Keywords cannot be used as identifiers.
Characters and the Char Data Type • Characters are stored as numbers according to the ASCII codes. • C++ includes a char data type for storing characters. • Each variable of the char data type can hold only one character. • A group of characters put together to form a word or phrase is called a string.
Floating-point data types • Tasks such as working with money require floating-point data types. • Floating-point data types can store fractional numbers. • The floating-point data types in C++ are float, double and long double. • With these types, you can also use exponential notation: 6.378164e6
Boolean Variables • A Boolean variable can have two possible values. • One value usually represents true and one usually represents false. • Some C++ compilers do not support the Boolean data type.
Constants • Constants hold data that does not change as the program runs. • Constants must be given a data type and a name. • An example of a statement that declares a constant: constdouble PI = 3.14159;
Summary • Computers store data in data structures. • Data is stored in variables or constants. • Within one data type, there may be types with different ranges and memory requirements. • Variables must be declared before they are used. • Boolean variables can have one of two values.