Chapter 2 Programming I
Chapter 2 Programming I
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 2
or otherwise on a password-protected website for classroom
Objectives (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 3
or otherwise on a password-protected website for classroom
Objectives (3 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 4
or otherwise on a password-protected website for classroom
Introduction
• Computer program
• A sequence of statements whose objective is to accomplish a task
• Programming
• The process of planning and creating a program
• Real-world analogy: a recipe for cooking
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (1 of 5)
EXAMPLE 2-1
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (2 of 5)
• Sample Run:
Program to compute and output the perimeter and area of a rectangle.
Length = 6
Width = 4
Perimeter = 20
Area = 24
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (3 of 5)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (4 of 5)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (5 of 5)
FIGURE 2-4 Memory spaces after the statement length = 6.0; executes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10
or otherwise on a password-protected website for classroom
The Basics of a C++ Program
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 11
or otherwise on a password-protected website for classroom
Comments
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 12
or otherwise on a password-protected website for classroom
Special Symbols
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 13
or otherwise on a password-protected website for classroom
Reserved Words (Keywords)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 14
or otherwise on a password-protected website for classroom
Identifiers (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 15
or otherwise on a password-protected website for classroom
Identifiers (2 of 2)
employee Salary
There can be no space between employeeSalary
employee and Salary.
Hello!
The exclamation mark cannot Hello
be used in an identifier.
one+two
The symbol + cannot be used in onePlusTwo
an identifier.
2nd
An identifier cannot begin with second
a digit.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 16
or otherwise on a password-protected website for classroom
Whitespaces
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 17
or otherwise on a password-protected website for classroom
Data Types
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 18
or otherwise on a password-protected website for classroom
Simple Data Types (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 19
or otherwise on a password-protected website for classroom
Simple Data Types (2 of 2)
TABLE 2-2 Values and Memory Allocation for Simple Data Types
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 20
or otherwise on a password-protected website for classroom
int Data Type
• Examples
• -6728
• 0
• 78
• +763
• Positive integers do not require a + sign
• A comma cannot be used within an integer
• Commas are only used for separating items in a list
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 21
or otherwise on a password-protected website for classroom
bool Data Type
• bool type
• Two values: true and false
• Purpose: to manipulate logical (Boolean) expressions
• true and false
• Logical values
• bool, true, and false
• Reserved words
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 22
or otherwise on a password-protected website for classroom
char Data Type (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 23
or otherwise on a password-protected website for classroom
char Data Type (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 24
or otherwise on a password-protected website for classroom
Floating-Point Data Types (1 of 3)
TABLE 2-3 Examples of Decimal Numbers in Scientific and C11 Floating-Point Notations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 25
or otherwise on a password-protected website for classroom
Floating-Point Data Types (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 26
or otherwise on a password-protected website for classroom
Floating-Point Data Types (3 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 27
or otherwise on a password-protected website for classroom
Data Types, Variables, and Assignment Statements
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 28
or otherwise on a password-protected website for classroom
Arithmetic Operators, Operator Precedence, and Expressions (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 29
or otherwise on a password-protected website for classroom
Arithmetic Operators, Operator Precedence, and Expressions (2 of 2)
• When you use / with integral data types, the integral result is truncated (no
rounding)
• Arithmetic expressions contain values and arithmetic operators
• Operands are the numbers appearing in the expressions
• Operators can be unary (one operand) or binary (two operands)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 30
or otherwise on a password-protected website for classroom
Order of Precedence
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 31
or otherwise on a password-protected website for classroom
Expressions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 32
or otherwise on a password-protected website for classroom
Mixed Expressions (1 of 2)
• Mixed expression
• Has operands of different data types
• Contains integers and floating-point
• Examples of mixed expressions
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 33
or otherwise on a password-protected website for classroom
Mixed Expressions (2 of 2)
• Evaluation rules
• If operator has same types of operands
- The operator is evaluated according to the type of the operands
• If operator has both types of operands
- Integer is changed to floating-point
- Operator is evaluated
- Result is floating-point
• Entire expression is evaluated according to precedence rules
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 34
or otherwise on a password-protected website for classroom
Type Conversion (Casting) (1 of 2)
• Implicit type coercion: when the value of one type is automatically changed to
another type
• Cast operator (also called type conversion or type casting): provides explicit
type conversion
• static_cast<dataTypeName>(expression)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 35
or otherwise on a password-protected website for classroom
Type Conversion (Casting) (2 of 2)
EXAMPLE 2-9
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 36
or otherwise on a password-protected website for classroom
string Type
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 37
or otherwise on a password-protected website for classroom
Variables, Assignment Statements, and Input Statements
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 38
or otherwise on a password-protected website for classroom
Allocating Memory with Constants and Variables (1 of2)
EXAMPLE 2-11
Consider the following C++ statements:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 39
or otherwise on a password-protected website for classroom
Allocating Memory with Constants and Variables (2 of 2)
EXAMPLE 2-12
Consider the following statements:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 40
or otherwise on a password-protected website for classroom
Putting Data into Variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 41
or otherwise on a password-protected website for classroom
Assignment Statement (1 of 4)
• Expression is evaluated and its value is assigned to the variable on the left side
• A variable is said to be initialized the first time a value is placed into it
• In C++, = is called the assignment operator
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 42
or otherwise on a password-protected website for classroom
Assignment Statement (2 of 4)
EXAMPLE 2-13
Suppose you have the following variable declarations:
int num1, num2;
double sale;
char first;
string str;
Now consider the following assignment statements:
num1 = 4;
num2 = 4 * 5 - 11;
sale = 0.02 * 1000;
first = 'D';
str = "It is a sunny day.";
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 43
or otherwise on a password-protected website for classroom
Assignment Statement (3 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 44
or otherwise on a password-protected website for classroom
Assignment Statement (4 of 4)
• Given int variables x, y, and z. How is this legal C++ statement evaluated?
x = y = z
• The assignment operator is evaluated from right to left
• The associativity of the assignment operator is from right to left
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 45
or otherwise on a password-protected website for classroom
Saving and Using the Value of an Expression
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 46
or otherwise on a password-protected website for classroom
Declaring and Initializing Variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 47
or otherwise on a password-protected website for classroom
Input (Read) Statement (1 of 3)
• Causes the computer to get a value of type double and places it in the variable miles
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 48
or otherwise on a password-protected website for classroom
Input (Read) Statement (2 of 3)
• Using more than one variable in cin allows more than one value to be read at
a time
• Example: if feet and inches are variables of type int, this statement:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 49
or otherwise on a password-protected website for classroom
Input (Read) Statement (3 of 3)
EXAMPLE 2-17
Suppose we have the following statements:
int feet;
int inches;
Suppose the input is:
23 7
Next, consider the following statement:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 50
or otherwise on a password-protected website for classroom
Increment and Decrement Operators
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 51
or otherwise on a password-protected website for classroom
Output (1 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 52
or otherwise on a password-protected website for classroom
Output (2 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 53
or otherwise on a password-protected website for classroom
Output (3 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 54
or otherwise on a password-protected website for classroom
Output (4 of 4)
Return Cursor moves to the beginning of the current line (not the
\r next line)
\\ Backslash Backslash is printed
\' Single quotation Single quotation mark is printed
\" Double quotation Double quotation mark is printed
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 55
or otherwise on a password-protected website for classroom
Preprocessor Directives (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 56
or otherwise on a password-protected website for classroom
Preprocessor Directives (2 of 2)
• For example:
• Causes the preprocessor to include the header file iostream in the program
• Preprocessor commands are processed before the program goes through the
compiler
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 57
or otherwise on a password-protected website for classroom
namespace and Using cin and cout in a Program
• cin and cout are declared in the header file iostream, but within std
namespace
• To use cin and cout in a program, use the following two statements:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 58
or otherwise on a password-protected website for classroom
Using the string Data Type in a Program
• To use the string type, you need to access its definition from the header file
string
• Include the following preprocessor directive:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 59
or otherwise on a password-protected website for classroom
Creating a C++ Program (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 60
or otherwise on a password-protected website for classroom
Creating a C++ Program (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 61
or otherwise on a password-protected website for classroom
Creating a C++ Program (3 of 3)
• The statements enclosed between the curly braces ({ and }) form the body of
the function
• A C++ program contains two types of statements:
• Declaration statements declare things, such as variables
• Executable statements perform calculations, manipulate data, create output, accept
input, etc.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 62
or otherwise on a password-protected website for classroom
Debugging: Understanding and Fixing Syntax Errors (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 63
or otherwise on a password-protected website for classroom
Debugging: Understanding and Fixing Syntax Errors (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 64
or otherwise on a password-protected website for classroom
Program Style and Form: Syntax
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 65
or otherwise on a password-protected website for classroom
Use of Blanks
• In C++, you use one or more blanks to separate numbers when data is input
• Blanks are also used to separate reserved words and identifiers from each other
and from other symbols
• Blanks must never appear within a reserved word or identifier
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 66
or otherwise on a password-protected website for classroom
Use of Semicolons, Brackets, and Commas
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 67
or otherwise on a password-protected website for classroom
Semantics
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 68
or otherwise on a password-protected website for classroom
Naming Identifiers
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 69
or otherwise on a password-protected website for classroom
Prompt Lines
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 70
or otherwise on a password-protected website for classroom
Documentation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 71
or otherwise on a password-protected website for classroom
Form and Style
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 72
or otherwise on a password-protected website for classroom
More on Assignment Statements
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 73
or otherwise on a password-protected website for classroom
Quick Review (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 74
or otherwise on a password-protected website for classroom
Quick Review (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 75
or otherwise on a password-protected website for classroom
Quick Review (3 of 3)
• Use cin and the stream extraction operator to input from the standard
input device
• Use cout and the stream insertion operator to output to the standard
output device
• Preprocessor commands are processed before the program goes through the
compiler
• A file containing a C++ program usually ends with the extension .cpp
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 76
or otherwise on a password-protected website for classroom