Upload
iium
View
159
Download
0
Embed Size (px)
Citation preview
A First Book of C++A First Book of C++
Chapter 3 (Pt 1)Chapter 3 (Pt 1)Assignment and Interactive InputAssignment and Interactive Input
In this chapter, you will learn about: Assignment Operators Formatted Output Mathematical Library Functions Interactive Keyboard Input Symbolic Constraints Common Programming Errors Errors, Testing, and Debugging
A First Book of C++ 4th Edition 2
ObjectivesObjectives
Basic assignment operator Format: variable = expression; Computes value of expression on right of = sign,
assigns it to variable on left side of = sign If not initialized in a declaration statement,
a variable should be assigned a value before used in any computation
Variables can only store one value at a time Subsequent assignment statements will overwrite
previously assigned values
A First Book of C++ 4th Edition 3
Assignment OperatorsAssignment Operators
Operand to right of = sign can be: A constant A variable A valid C++ expression
Operand to left of = sign must be a variable If operand on right side is an expression:
All variables in expression must have a value to get a valid result from the assignment
A First Book of C++ 4th Edition 4
Assignment Operators Assignment Operators (cont'd.)(cont'd.)
Expression: combination of constants and variables that can be evaluated Examples:
sum = 3 + 7; diff = 15 –6; product = .05 * 14.6; tally = count + 1; newtotal = 18.3 + total; average = sum / items; slope = (y2 – y1) / (x2 – x1);
A First Book of C++ 4th Edition 5
Assignment Operators Assignment Operators (cont'd.)(cont'd.)
variable
constant
Assignment Operators Assignment Operators (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 6
constantexpression
Value on right side of a C++ expression is converted to data type of variable on the left side
Example:If temp is an integer variable (int temp), the
assignmenttemp = 25.89;
causes integer value 25 to be stored in integer variable temp
A First Book of C++ 4th Edition 7
CoercionCoercion
A First Book of C++ 4th Edition 8
CoercionCoercion
Program
Output
CompilationWarning
sum = sum + 10; is a valid C++ expression The value of sum + 10 is stored in variable sum
Not a valid algebra equation (in formal Mathematics)
A First Book of C++ 4th Edition 9
Assignment VariationsAssignment Variations
Assignment Variations Assignment Variations (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 10
// Initialization value for use in accumulation
// Accumulation
A First Book of C++ 4th Edition 11
Assignment Variations Assignment Variations (cont'd.)(cont'd.)
Assignment expressions such as: sum = sum + 25;
can be written by using the following shortcut operators:+= -= *= /= %=
Example:sum = sum + 10;
can be written as:sum += 10; (this means sum = sum + 10)
A First Book of C++ 4th Edition 12
Assignment Variations Assignment Variations (cont'd.)(cont'd.)
The following statements add the numbers 96, 70, 85, and 60 in calculator fashion:
A First Book of C++ 4th Edition 13
AccumulatingAccumulating
Accumulating (cont'd.)Accumulating (cont'd.)
A First Book of C++ 4th Edition 14
Has the form:variable = variable + fixedNumber;Each time statement is executed, value of
variable is increased by a fixed amountIncrement operators (++), (--)
Unary operator for special case when variable is increased or decreased by 1
Using the increment operator, the expression:variable = variable + 1; can be replaced byeither: ++variable; or variable++;
A First Book of C++ 4th Edition 15
CountingCounting
Examples of counting statements:i= i + 1;n = n + 1;count = count + 1;j = j + 2;m = m + 2;kk = kk + 3;
A First Book of C++ 4th Edition 16
Counting (cont'd.)Counting (cont'd.)
Examples of the increment operator:
A First Book of C++ 4th Edition 17
Counting (cont'd.)Counting (cont'd.)
Counting (cont'd.)Counting (cont'd.)
A First Book of C++ 4th Edition 18
Prefix increment operator: the ++ or -- operator appears before a variable
# The expression k = ++n performs two actions:n = n + 1; // increment n firstk = n; // assign n’s value to k
Postfix increment operator: the ++ or -- operator appears after a variable
# The expression k = n++ works differently:k = n; // assign n’s value to kn = n + 1; // and then increment n
A First Book of C++ 4th Edition 19
Counting (cont'd.)Counting (cont'd.)
A program must present results attractively Field width manipulators: control format of
numbers displayed by cout Manipulators included in the output stream
A First Book of C++ 4th Edition 20
Formatted OutputFormatted Output
Formatted Output (cont'd.)Formatted Output (cont'd.)
A First Book of C++ 4th Edition 21
manipulators
A First Book of C++ 4th Edition 22
Formatted Output (cont'd.)Formatted Output (cont'd.)
A First Book of C++ 4th Edition 23
//Program 3.5
#include <iostream>#include <iomanip>using namespace std;
int main(){
cout << setw(4) << 6 << endl // set total width of display to 4 << setw(4) << fixed << setprecision(2) << 18.5 << endl << setw(4) << setiosflags(ios::fixed) << 124 << endl << “---\n” << (6+18.5+124) << endl;return 0;}
Field justification manipulator Example: cout << “|” << setw(10) << setiosflags(ios::left) << 142 << “|”;
A First Book of C++ 4th Edition 24
The The setiosflags()setiosflags() ManipulatorManipulator
Field justification manipulator Example: cout << “|” << setw(10) << setiosflags(ios::left) << 142 << “|”;
A First Book of C++ 4th Edition 25
The The setiosflags()setiosflags() ManipulatorManipulator
oct and hex manipulators are used for conversions to octal and hexadecimal
Display of integer values in one of three possible numbering systems (decimal, octal, and hexadecimal) doesn’t affect how the number is stored in a computer All numbers are stored by using the
computer’s internal codes
A First Book of C++ 4th Edition 26
Hexadecimal and Octal I/OHexadecimal and Octal I/O
Hexadecimal and Octal I/O Hexadecimal and Octal I/O (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 27
A First Book of C++ 4th Edition 28
Hexadecimal and Octal I/O Hexadecimal and Octal I/O (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 29