Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.

Similar presentations


Presentation on theme: "Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition."— Presentation transcript:

1 Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition

2 Introduction to Programming with C++, Fourth Edition2 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

3 Introduction to Programming with C++, Fourth Edition3 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

4 Introduction to Programming with C++, Fourth Edition4 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

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

6 Introduction to Programming with C++, Fourth Edition6 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

7 Introduction to Programming with C++, Fourth Edition7 Naming Rules

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

9 Introduction to Programming with C++, Fourth Edition9 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

10 Introduction to Programming with C++, Fourth Edition10 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

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

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

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

14 Introduction to Programming with C++, Fourth Edition14 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

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

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

17 Introduction to Programming with C++, Fourth Edition17 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

18 Introduction to Programming with C++, Fourth Edition18 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

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

20 Introduction to Programming with C++, Fourth Edition20 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)

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

22 Introduction to Programming with C++, Fourth Edition22 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)

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

24 Introduction to Programming with C++, Fourth Edition24 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

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

26 Introduction to Programming with C++, Fourth Edition26 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)

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

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

29 Introduction to Programming with C++, Fourth Edition29 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

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

31 Introduction to Programming with C++, Fourth Edition31 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

32 Introduction to Programming with C++, Fourth Edition32 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

33 Introduction to Programming with C++, Fourth Edition33 Arithmetic Operators

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

35 Introduction to Programming with C++, Fourth Edition35 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)

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

37 Introduction to Programming with C++, Fourth Edition37 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

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

39 Introduction to Programming with C++, Fourth Edition39 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


Download ppt "Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition."

Similar presentations


Ads by Google