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

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

Jan 20, 2018

Download

Documents

Rosemary Payne

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Chapter 4: Variables, Constants, and

Arithmetic Operators

Introduction to Programming with C++Fourth Edition

Page 2: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 3: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

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

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

Page 5: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 5

Circle Area Problem

Page 6: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 7: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 7

Naming Rules

Page 8: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 8

Valid Names and Keywords

Page 9: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 10: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 11: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 11

Names of Memory Locations for the Circle Area Problem

Page 12: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 12

Some of the Data Types Available in C++

Page 13: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 13

Data Type Assigned to the Memory Locations for the Circle Area

Problem

Page 14: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 15: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 15

Comparison of the Decimal and Binary Number Systems

Page 16: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 16

Partial ASCII Chart

Page 17: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 18: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 19: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 19

Examples of Numeric, Character, and String Literal Constants

Page 20: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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)

Page 21: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 21

Type Casting (continued)

Page 22: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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)

Page 23: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 23

Initial Values Assigned to Memory Locations for the Circle Area

Problem

Page 24: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 25: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 25

Declaring a Named Constant (continued)

Page 26: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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)

Page 27: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 27

Syntax and Examples of Instructions that Reserve and Initialize Variables

in C++

Page 28: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 28

C++ Statements Reserving the radius, radiusSquared, and area

Variables

Page 29: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 30: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 30

Using an Assignment Statement to Store Data in a Variable (continued)

Page 31: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 32: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 33: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 33

Arithmetic Operators

Page 34: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 34

Arithmetic Operators (continued)

Page 35: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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)

Page 36: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 36

Getting Data from the Keyboard (continued)

Page 37: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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

Page 38: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

Introduction to Programming with C++, Fourth Edition 38

The ignore() Function (continued)

Page 39: Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth…

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