SlideShare a Scribd company logo
1 of 29
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 postfixSelf-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 algorithmDevaKumari 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 designDINESH DEVIREDDY
 
Cse presentation ratul
Cse presentation ratulCse presentation ratul
Cse presentation ratulHassan Ratul
 
Lecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorsLecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorseShikshak
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,pptAllNewTeach
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackSoumen Santra
 
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 utilzationKaushal Patel
 

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
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
 
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
 

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-s14IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14IIUM
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04IIUM
 
C++ Programming.pdf
C++ Programming.pdfC++ Programming.pdf
C++ Programming.pdfMrRSmey
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionalish sha
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c languageTanmay Modi
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
[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 PrecedenceMuhammad Hammad Waseem
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentalsumar78600
 
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 FunctionsDhivyaSubramaniyam
 

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
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
 
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 and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc 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
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhostIIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimediaIIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblockIIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice keyIIUM
 
Group p1
Group p1Group p1
Group p1IIUM
 
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 seoIIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lformsIIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answerIIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midtermIIUM
 
Heaps
HeapsHeaps
HeapsIIUM
 
Report format
Report formatReport format
Report formatIIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelinesIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotationsIIUM
 
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
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 

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