SlideShare a Scribd company logo
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 (cont'd.)Assignment Operators (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 (cont'd.)Assignment Operators (cont'd.)
variable
constant
Assignment Operators (cont'd.)Assignment Operators (cont'd.)
A First Book of C++ 4th Edition 6
constant
expression
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 assignment
temp = 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
Compilation
Warning
∗ 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 (cont'd.)Assignment Variations (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 (cont'd.)Assignment Variations (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 (cont'd.)Assignment Variations (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 amount
Increment 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 by
either: ++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 first
k = 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 k
n = 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
TheThe setiosflags()setiosflags() ManipulatorManipulator
∗ Field justification manipulator
∗ Example: cout << “|” << setw(10) <<
setiosflags(ios::left) << 142 << “|”;
A First Book of C++ 4th Edition 25
TheThe 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 (cont'd.)Hexadecimal and Octal I/O (cont'd.)
A First Book of C++ 4th Edition 27
A First Book of C++ 4th Edition 28
Hexadecimal and Octal I/O (cont'd.)Hexadecimal and Octal I/O (cont'd.)
A First Book of C++ 4th Edition 29

More Related Content

What's hot

Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
DevaKumari Vijay
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfixSenthil Kumar
 
Project on digital vlsi design
Project on digital vlsi designProject on digital vlsi design
Project on digital vlsi design
DINESH DEVIREDDY
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
Kulachi Hansraj Model School Ashok Vihar
 
stack
stackstack
stack
Raj Sarode
 
P3
P3P3
P3
lksoo
 
Cse presentation ratul
Cse presentation ratulCse presentation ratul
Cse presentation ratul
Hassan Ratul
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Lecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorsLecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operators
eShikshak
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
Soumen Santra
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
AllNewTeach
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
Kaushal Patel
 
Concept of c
Concept of cConcept of c
Concept of c
Rohan Gajre
 

What's hot (18)

Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Lab 1
Lab 1Lab 1
Lab 1
 
Project on digital vlsi design
Project on digital vlsi designProject on digital vlsi design
Project on digital vlsi design
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
 
stack
stackstack
stack
 
P3
P3P3
P3
 
Cse presentation ratul
Cse presentation ratulCse presentation ratul
Cse presentation ratul
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Lecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorsLecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operators
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
Concept of c
Concept of cConcept of c
Concept of c
 

Viewers also liked

Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04IIUM
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
HUST
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
Online
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
Abdul Hannan
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
thesaqib
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
KurdGul
 

Viewers also liked (10)

Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 
Loops c++
Loops c++Loops c++
Loops c++
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 

Similar to Csc1100 lecture03 ch03-pt1-s14

Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14IIUM
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
MLG College of Learning, Inc
 
C++ Programming.pdf
C++ Programming.pdfC++ Programming.pdf
C++ Programming.pdf
MrRSmey
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionalish sha
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
Yuvraj994432
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
C++ Language
C++ LanguageC++ Language
C++ Language
Syed Zaid Irshad
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
umar78600
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
ramanathan2006
 

Similar to Csc1100 lecture03 ch03-pt1-s14 (20)

Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Ch03
Ch03Ch03
Ch03
 
C++ Programming.pdf
C++ Programming.pdfC++ Programming.pdf
C++ Programming.pdf
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Apclass
ApclassApclass
Apclass
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
IIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
IIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
IIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
IIUM
 
Group p1
Group p1Group p1
Group p1
IIUM
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
IIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
IIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
IIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
IIUM
 
Heaps
HeapsHeaps
Heaps
IIUM
 
Report format
Report formatReport format
Report format
IIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
IIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
IIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
IIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
IIUM
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
IIUM
 

More from IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Csc1100 lecture03 ch03-pt1-s14

  • 1. 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
  • 2. ∗ 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
  • 3. ∗ 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
  • 4. ∗ 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 (cont'd.)Assignment Operators (cont'd.)
  • 5. ∗ 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 (cont'd.)Assignment Operators (cont'd.) variable constant
  • 6. Assignment Operators (cont'd.)Assignment Operators (cont'd.) A First Book of C++ 4th Edition 6 constant expression
  • 7. 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 assignment temp = 25.89; causes integer value 25 to be stored in integer variable temp A First Book of C++ 4th Edition 7 CoercionCoercion
  • 8. A First Book of C++ 4th Edition 8 CoercionCoercion Program Output Compilation Warning
  • 9. ∗ 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
  • 10. Assignment Variations (cont'd.)Assignment Variations (cont'd.) A First Book of C++ 4th Edition 10 // Initialization value for use in accumulation // Accumulation
  • 11. A First Book of C++ 4th Edition 11 Assignment Variations (cont'd.)Assignment Variations (cont'd.)
  • 12. 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 (cont'd.)Assignment Variations (cont'd.)
  • 13. ∗ The following statements add the numbers 96, 70, 85, and 60 in calculator fashion: A First Book of C++ 4th Edition 13 AccumulatingAccumulating
  • 14. Accumulating (cont'd.)Accumulating (cont'd.) A First Book of C++ 4th Edition 14
  • 15. Has the form: variable = variable + fixedNumber; Each time statement is executed, value of variable is increased by a fixed amount Increment 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 by either: ++variable; or variable++; A First Book of C++ 4th Edition 15 CountingCounting
  • 16. ∗ 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.)
  • 17. ∗ Examples of the increment operator: A First Book of C++ 4th Edition 17 Counting (cont'd.)Counting (cont'd.)
  • 18. Counting (cont'd.)Counting (cont'd.) A First Book of C++ 4th Edition 18
  • 19. Prefix increment operator: the ++ or -- operator appears before a variable ̶ The expression k = ++n performs two actions: n = n + 1; // increment n first k = 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 k n = n + 1; // and then increment n A First Book of C++ 4th Edition 19 Counting (cont'd.)Counting (cont'd.)
  • 20. ∗ 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
  • 21. Formatted Output (cont'd.)Formatted Output (cont'd.) A First Book of C++ 4th Edition 21 manipulators
  • 22. A First Book of C++ 4th Edition 22
  • 23. 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; }
  • 24. ∗ Field justification manipulator ∗ Example: cout << “|” << setw(10) << setiosflags(ios::left) << 142 << “|”; A First Book of C++ 4th Edition 24 TheThe setiosflags()setiosflags() ManipulatorManipulator
  • 25. ∗ Field justification manipulator ∗ Example: cout << “|” << setw(10) << setiosflags(ios::left) << 142 << “|”; A First Book of C++ 4th Edition 25 TheThe setiosflags()setiosflags() ManipulatorManipulator
  • 26. ∗ 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
  • 27. Hexadecimal and Octal I/O (cont'd.)Hexadecimal and Octal I/O (cont'd.) A First Book of C++ 4th Edition 27
  • 28. A First Book of C++ 4th Edition 28
  • 29. Hexadecimal and Octal I/O (cont'd.)Hexadecimal and Octal I/O (cont'd.) A First Book of C++ 4th Edition 29