430 likes | 682 Views
Chapter 4: Variables, Constants, and Arithmetic Operators. Introduction to Programming with C++ Fourth Edition . Objectives. Distinguish among a variable, a named constant, and a literal constant Select an appropriate name, data type, and initial value for a memory location
E N D
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition
Objectives • Distinguish among a variable, a named constant, and a literal constant • Select an appropriate name, data type, and initial value for a memory location • Explain how data is stored in memory • Declare and initialize a memory location • Type cast data Introduction to Programming with C++, Fourth Edition
Objectives (continued) • Use an assignment statement to assign data to a variable • Include arithmetic operators in an expression • Get string input using the getline() function • Ignore characters using the ignore() function Introduction to Programming with C++, Fourth Edition
Variables and Named Constants • Variable – the only type of memory location whose contents can change while a program is running • Named Constant – used for any item whose value will remain the same each time the program is executed Introduction to Programming with C++, Fourth Edition
Circle Area Problem Introduction to Programming with C++, Fourth Edition
Selecting a Name for a Memory Location • Identifier – a descriptive name assigned to each variable and named constant used in a program • Keyword (reserved word) - a word that has a special meaning in a programming language Introduction to Programming with C++, Fourth Edition
Naming Rules Introduction to Programming with C++, Fourth Edition
Valid Names and Keywords Introduction to Programming with C++, Fourth Edition
Selecting a Data Type for a Memory Location • Data types - control the type of data the memory location can store • Fundamental data types - basic data types built into the C++ language which must be typed using lowercase letters • Integers - whole numbers Introduction to Programming with C++, Fourth Edition
Selecting a Data Type for a Memory Location (continued) • Floating-point numbers - numbers with a decimal place • Characters - letters, symbols, and numbers that will not be used in calculations • Boolean values - true or false Introduction to Programming with C++, Fourth Edition
Names of Memory Locations for the Circle Area Problem Introduction to Programming with C++, Fourth Edition
Some of the Data Types Available in C++ Introduction to Programming with C++, Fourth Edition
Data Type Assigned to the Memory Locations for the Circle Area Problem Introduction to Programming with C++, Fourth Edition
How Data is Stored in Internal Memory • Numeric data – represented in internal memory using the binary (or base 2) number system • Character data - represented in internal memory using ASCII codes Introduction to Programming with C++, Fourth Edition
Comparison of the Decimal and Binary Number Systems Introduction to Programming with C++, Fourth Edition
Partial ASCII Chart Introduction to Programming with C++, Fourth Edition
Selecting an Initial Value for a Memory Location • Initializing - assigning an initial, or beginning, value to a memory location • Literal constant - an item of data that can appear in a program instruction, and can be stored in a memory location • Numeric literal constant - a number Introduction to Programming with C++, Fourth Edition
Selecting an Initial Value for a Memory Location (continued) • Character literal constant - a character enclosed in single quotation marks • String literal constant - zero or more characters enclosed in double quotation marks Introduction to Programming with C++, Fourth Edition
Examples of Numeric, Character, and String Literal Constants Introduction to Programming with C++, Fourth Edition
Type Casting • Implicit type conversion – in an assignment, if the data type of the expression does not match that of the memory location, the data is converted appropriately • Converts values to fit memory locations • Conversion can either promote values to a larger type or demote values to a smaller type (loss of data) Introduction to Programming with C++, Fourth Edition
Type Casting (continued) Introduction to Programming with C++, Fourth Edition
Type Casting (continued) • Type casting (explicit type conversion) – the forced conversion of data from one data type to another • Enclose an item of data in parentheses, preceded by the desired type • Example - type cast the double number 3.7 to the float data type by writing float(3.7) Introduction to Programming with C++, Fourth Edition
Initial Values Assigned to Memory Locations for the Circle Area Problem Introduction to Programming with C++, Fourth Edition
Declaring a Named Constant • To declare a named constant specify: • Name • Data type • Initial value • Can use the named constant anywhere the initial value can be used • Even in other named constants Introduction to Programming with C++, Fourth Edition
Declaring a Named Constant (continued) Introduction to Programming with C++, Fourth Edition
Declaring a Variable • To declare a variable specify: • Name • Data type • Initial value (optional) • If you omit the initial value, the variable may contain garbage (left over bits) Introduction to Programming with C++, Fourth Edition
Syntax and Examples of Instructions that Reserve and Initialize Variables in C++ Introduction to Programming with C++, Fourth Edition
C++ Statements Reserving the radius, radiusSquared, and area Variables Introduction to Programming with C++, Fourth Edition
Using an Assignment Statement to Store Data in a Variable • Use an assignment statement to change the contents of a variable while a program is running • Assigns the value of the expression appearing on the right side of the assignment operator (=) to the variable whose variablename appears on the left side • Remember that a variable can store only one value at a time: new assignments replace old Introduction to Programming with C++, Fourth Edition
Using an Assignment Statement to Store Data in a Variable (continued) Introduction to Programming with C++, Fourth Edition
Processing of Assignment Statements int temp = 3; temp = temp + 1; temp = temp * 2; • Declaration statement int temp = 3: creates a temp variable in the computer’s internal memory and initializes the variable to the integer 3 • The assignment statement temp = temp + 1: adds the number 1 to the contents of the temp variable, giving 4. It then replaces the 3 currently stored in the temp variable with the number 4 Introduction to Programming with C++, Fourth Edition
Processing of Assignment Statements (continued) • The assignment statement temp = temp * 2: first multiplies the contents of the temp variable (4) by 2, giving 8. It then removes the number 4 from the temp variable and stores the number 8 there instead Introduction to Programming with C++, Fourth Edition
Arithmetic Operators Introduction to Programming with C++, Fourth Edition
Arithmetic Operators (continued) Introduction to Programming with C++, Fourth Edition
Getting Data from the Keyboard • Use cin and the extraction operator (>>) to input data from the keyboard • Can input a single character or a string, as long as the string has no spaces • The getline() function gets string input from the keyboard (including embedded spaces) Introduction to Programming with C++, Fourth Edition
Getting Data from the Keyboard (continued) Introduction to Programming with C++, Fourth Edition
The ignore() Function • Instructs the computer to disregard, or skip, characters entered at the keyboard • The function reads and discards (consumes) the characters • Necessary when inputting numbers and characters Introduction to Programming with C++, Fourth Edition
The ignore() Function (continued) Introduction to Programming with C++, Fourth Edition
Summary • Variables and constants need: • Name, data type, initial value • Type casting converts values to fit memory locations • Assignments change the contents of a variable while a program is running • Use arithmetic operators in expressions • Use getline() to input strings with spaces • Use ignore() to consume unwanted characters from input stream Introduction to Programming with C++, Fourth Edition