Chapter 4: Variables, Constants, and Arithmetic Operators

1 / 39

# Chapter 4: Variables, Constants, and Arithmetic Operators - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## Chapter 4: Variables, Constants, and Arithmetic Operators

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition

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

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

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

5. Circle Area Problem Introduction to Programming with C++, Fourth Edition

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

7. Naming Rules Introduction to Programming with C++, Fourth Edition

8. Valid Names and Keywords Introduction to Programming with C++, Fourth Edition

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

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

11. Names of Memory Locations for the Circle Area Problem Introduction to Programming with C++, Fourth Edition

12. Some of the Data Types Available in C++ Introduction to Programming with C++, Fourth Edition

13. Data Type Assigned to the Memory Locations for the Circle Area Problem Introduction to Programming with C++, Fourth Edition

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

15. Comparison of the Decimal and Binary Number Systems Introduction to Programming with C++, Fourth Edition

16. Partial ASCII Chart Introduction to Programming with C++, Fourth Edition

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

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

19. Examples of Numeric, Character, and String Literal Constants Introduction to Programming with C++, Fourth Edition

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

21. Type Casting (continued) Introduction to Programming with C++, Fourth Edition

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

23. Initial Values Assigned to Memory Locations for the Circle Area Problem Introduction to Programming with C++, Fourth Edition

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

25. Declaring a Named Constant (continued) Introduction to Programming with C++, Fourth Edition

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

27. Syntax and Examples of Instructions that Reserve and Initialize Variables in C++ Introduction to Programming with C++, Fourth Edition

28. C++ Statements Reserving the radius, radiusSquared, and area Variables Introduction to Programming with C++, Fourth Edition

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

30. Using an Assignment Statement to Store Data in a Variable (continued) Introduction to Programming with C++, Fourth Edition

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

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

33. Arithmetic Operators Introduction to Programming with C++, Fourth Edition

34. Arithmetic Operators (continued) Introduction to Programming with C++, Fourth Edition

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

36. Getting Data from the Keyboard (continued) Introduction to Programming with C++, Fourth Edition

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

38. The ignore() Function (continued) Introduction to Programming with C++, Fourth Edition

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