SlideShare a Scribd company logo
1 of 24
Download to read offline
Introduction to Programming in C++
Eight Edition
Lesson 4.1:
Completing the Problem-Solving
Process
An Introduction to Programming with C++, Eight Edition
• Get numeric and character data from the keyboard
• Display information on the computer screen
• Write arithmetic expressions
• Type cast a value
• Write an assignment statement
• Code the algorithm into a program
• Desk-check a program
• Evaluate and modify a program
Objectives
2
An Introduction to Programming with C++, Eight Edition
• The fourth step in the problem-solving process is to
code algorithm into a program
• Begin by declaring a memory location for each input,
processing, and output value in IPO chart
• Optionally initialize each value (highly preferred)
• Next, you code the instructions for the algorithm
Finishing Step 4 in the Problem-
Solving Process
3
An Introduction to Programming with C++, Eight Edition
Getting Data from the Keyboard
4
• C++ uses stream objects to perform input/output
operations
• A stream is a sequence of characters
• The cin object is used to obtain information from the
keyboard (program pauses while user enters data)
• The extraction operator (>>) takes information out of
cin object and stores it in internal memory
– Syntax: cin >> variableName;
An Introduction to Programming with C++, Eight Edition
Getting Data from the Keyboard
(cont’d.)
5
An Introduction to Programming with C++, Eight Edition
Getting Data from the Keyboard
(cont’d.)
6
An Introduction to Programming with C++, Eight Edition
Displaying Messages on the Computer
Screen
7
• You use a prompt (message) to let the user know what
data is to be entered
• The cout object is used with the insertion operator (<<)
To display information on the screen.
• Information can be any combination of literal constants,
named constants , and variables.
• Multiple items can be printed in the same statement
- Systax : cout << item1 [<<item2 << itemN];
- Part in brackets is optional
An Introduction to Programming with C++, Eight Edition
• A stream manipulator is used to manipulate (manage)
the characters in an input or output string
• endl is a stream manipulator that advances the cursor to
the next line on the screen
– Equivalent to pressing the Enter key (carriage return
and line feed)
Displaying Messages on the Computer
Screen (cont’d.)
8
An Introduction to Programming with C++, Eight Edition
Displaying Messages on the Computer
Screen (cont’d.)
9
An Introduction to Programming with C++, Eight Edition
• You can evaluate arithmetic expressions in C++ using
arithmetic operators
• Operators are negation (-), addition (+), subtraction (-),
multiplication (*), division (/), and modulus (%)
• Negation and subtraction use the same symbol, but
negation is a unary operator (one operand) and
subtraction is a binary operator (two operands)
• Modulus gives remainder when dividing two integers
Arithmetic Operators in C++
10
An Introduction to Programming with C++, Eight Edition
• Each operator has a precedence: determines in which
order operators in an expression are evaluated
• Operators with lower-precedence numbers are
evaluated before higher ones
• Parentheses have lowest-precedence number, so they
can be used to override precedence order
• Operators with the same precedence number are
evaluated from left to right
Arithmetic Operators in C++ (cont’d.)
11
An Introduction to Programming with C++, Eight Edition
Arithmetic Operators in C++ (cont’d.)
12
Figure 4-7 Standard arithmetic operators and their order of
precedence
An Introduction to Programming with C++, Eight Edition
Arithmetic Operators in C++ (cont’d.)
13
An Introduction to Programming with C++, Eight Edition
• Recall that the compiler will implicitly promote or
demote data types to match when possible
• Sometimes it is necessary to explicitly cast from one
data type into another
– Example: dividing two integers gives the result of
integer division (no remainder), but you would really like a
double result
– If one or both of the integers is a literal, you can
cast it to a double by adding .0 to the end of it
– If both are variables, you must use the static_cast
operator
Type Conversions in Arithmetic
Expressions
14
An Introduction to Programming with C++, Eight Edition
Type Conversions in Arithmetic
Expressions (cont’d.)
15
An Introduction to Programming with C++, Eight Edition
• Used to explicitly convert data from one data type to
another
• Called an explicit type conversion or type cast
• Syntax: static_cast(data)
– data can be a literal constant, named constant, or
variable
– dataType is the data type to which you want the
data converted
The static_cast Operator
16
An Introduction to Programming with C++, Eight Edition
The static_cast Operator (cont’d.)
17
An Introduction to Programming with C++, Eight Edition
The static_cast Operator (cont’d.)
18
An Introduction to Programming with C++, Eight Edition
• You use an assignment statement to assign a value to a
variable while a program is running
• Syntax: variableName = expression
– The = symbol is the assignment operator
• Tells computer to evaluate expression on right side of
assignment operator and store result in variable on left
side of the operator
– expression can include one or more literal
constants, named constants, variables, or arithmetic
operators
Assignment Statements
19
An Introduction to Programming with C++, Eight Edition
• Data type of expression in an assignment statement
must match data type of the variable
• If they don’t match, compiler will use implicit type
casting to get them to match
– Doesn’t always produce correct result
– Better to explicitly cast to correct data type
yourself
• Remember:
– Declaration statement creates a new variable
– Assignment statement assigns a new value to an
existing variable
Assignment Statements (cont’d.)
20
An Introduction to Programming with C++, Eight Edition
Assignment Statements (cont’d.)
21
An Introduction to Programming with C++, Eight Edition
• Allow you to abbreviate assignment statements that
contain an arithmetic operator
• Statement must be of the form
variableName = variableName arithmeticOperator value
• Abbreviated as variableName arithmeticOperator =
value
– Example: price = price*1.05; can be abbreviated
as price *= 1.05;
• Most common operators are += , -= , *= , /= , and %=
Arithmetic Assignment Operators
22
An Introduction to Programming with C++, Eight Edition
Arithmetic Assignment Operators
(cont’d)
23
An Introduction to Programming with C++, Eight Edition
The End ♥ ♥ ♥
24

More Related Content

What's hot (20)

Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Lesson 10
Lesson 10Lesson 10
Lesson 10
 
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
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in c
 
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game programLesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Chapter 02 - Problem Solving
Chapter 02 - Problem SolvingChapter 02 - Problem Solving
Chapter 02 - Problem Solving
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
Ppl
PplPpl
Ppl
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Data flow model -Lecture-4
Data flow model -Lecture-4Data flow model -Lecture-4
Data flow model -Lecture-4
 
Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Lesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulatorsLesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulators
 
Verilog operators
Verilog operatorsVerilog operators
Verilog operators
 
A quick introduction to c programming
A quick introduction to c programmingA quick introduction to c programming
A quick introduction to c programming
 
Functions
FunctionsFunctions
Functions
 
Fda unit 1 lec 1
Fda unit 1 lec  1Fda unit 1 lec  1
Fda unit 1 lec 1
 

Similar to Lesson 4.1 completing the problem solving process

Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Processmshellman
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variablesTony Apreku
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Blue Elephant Consulting
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Blue Elephant Consulting
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3ahmed22dg
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfmadihamaqbool6
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
Object oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and exampleObject oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and exampleVaibhav Khanna
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2Don Dooley
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105NUST Stuff
 
C++ Overview
C++ OverviewC++ Overview
C++ Overviewkelleyc3
 

Similar to Lesson 4.1 completing the problem solving process (20)

Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
 
C++ ch2
C++ ch2C++ ch2
C++ ch2
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
 
Chapter2
Chapter2Chapter2
Chapter2
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
intro to c
intro to cintro to c
intro to c
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
C programming language
C programming languageC programming language
C programming language
 
Object oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and exampleObject oriented programming 12 programming steps in cpp and example
Object oriented programming 12 programming steps in cpp and example
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105
 
C program
C programC program
C program
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Recently uploaded (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

Lesson 4.1 completing the problem solving process

  • 1. Introduction to Programming in C++ Eight Edition Lesson 4.1: Completing the Problem-Solving Process
  • 2. An Introduction to Programming with C++, Eight Edition • Get numeric and character data from the keyboard • Display information on the computer screen • Write arithmetic expressions • Type cast a value • Write an assignment statement • Code the algorithm into a program • Desk-check a program • Evaluate and modify a program Objectives 2
  • 3. An Introduction to Programming with C++, Eight Edition • The fourth step in the problem-solving process is to code algorithm into a program • Begin by declaring a memory location for each input, processing, and output value in IPO chart • Optionally initialize each value (highly preferred) • Next, you code the instructions for the algorithm Finishing Step 4 in the Problem- Solving Process 3
  • 4. An Introduction to Programming with C++, Eight Edition Getting Data from the Keyboard 4 • C++ uses stream objects to perform input/output operations • A stream is a sequence of characters • The cin object is used to obtain information from the keyboard (program pauses while user enters data) • The extraction operator (>>) takes information out of cin object and stores it in internal memory – Syntax: cin >> variableName;
  • 5. An Introduction to Programming with C++, Eight Edition Getting Data from the Keyboard (cont’d.) 5
  • 6. An Introduction to Programming with C++, Eight Edition Getting Data from the Keyboard (cont’d.) 6
  • 7. An Introduction to Programming with C++, Eight Edition Displaying Messages on the Computer Screen 7 • You use a prompt (message) to let the user know what data is to be entered • The cout object is used with the insertion operator (<<) To display information on the screen. • Information can be any combination of literal constants, named constants , and variables. • Multiple items can be printed in the same statement - Systax : cout << item1 [<<item2 << itemN]; - Part in brackets is optional
  • 8. An Introduction to Programming with C++, Eight Edition • A stream manipulator is used to manipulate (manage) the characters in an input or output string • endl is a stream manipulator that advances the cursor to the next line on the screen – Equivalent to pressing the Enter key (carriage return and line feed) Displaying Messages on the Computer Screen (cont’d.) 8
  • 9. An Introduction to Programming with C++, Eight Edition Displaying Messages on the Computer Screen (cont’d.) 9
  • 10. An Introduction to Programming with C++, Eight Edition • You can evaluate arithmetic expressions in C++ using arithmetic operators • Operators are negation (-), addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) • Negation and subtraction use the same symbol, but negation is a unary operator (one operand) and subtraction is a binary operator (two operands) • Modulus gives remainder when dividing two integers Arithmetic Operators in C++ 10
  • 11. An Introduction to Programming with C++, Eight Edition • Each operator has a precedence: determines in which order operators in an expression are evaluated • Operators with lower-precedence numbers are evaluated before higher ones • Parentheses have lowest-precedence number, so they can be used to override precedence order • Operators with the same precedence number are evaluated from left to right Arithmetic Operators in C++ (cont’d.) 11
  • 12. An Introduction to Programming with C++, Eight Edition Arithmetic Operators in C++ (cont’d.) 12 Figure 4-7 Standard arithmetic operators and their order of precedence
  • 13. An Introduction to Programming with C++, Eight Edition Arithmetic Operators in C++ (cont’d.) 13
  • 14. An Introduction to Programming with C++, Eight Edition • Recall that the compiler will implicitly promote or demote data types to match when possible • Sometimes it is necessary to explicitly cast from one data type into another – Example: dividing two integers gives the result of integer division (no remainder), but you would really like a double result – If one or both of the integers is a literal, you can cast it to a double by adding .0 to the end of it – If both are variables, you must use the static_cast operator Type Conversions in Arithmetic Expressions 14
  • 15. An Introduction to Programming with C++, Eight Edition Type Conversions in Arithmetic Expressions (cont’d.) 15
  • 16. An Introduction to Programming with C++, Eight Edition • Used to explicitly convert data from one data type to another • Called an explicit type conversion or type cast • Syntax: static_cast(data) – data can be a literal constant, named constant, or variable – dataType is the data type to which you want the data converted The static_cast Operator 16
  • 17. An Introduction to Programming with C++, Eight Edition The static_cast Operator (cont’d.) 17
  • 18. An Introduction to Programming with C++, Eight Edition The static_cast Operator (cont’d.) 18
  • 19. An Introduction to Programming with C++, Eight Edition • You use an assignment statement to assign a value to a variable while a program is running • Syntax: variableName = expression – The = symbol is the assignment operator • Tells computer to evaluate expression on right side of assignment operator and store result in variable on left side of the operator – expression can include one or more literal constants, named constants, variables, or arithmetic operators Assignment Statements 19
  • 20. An Introduction to Programming with C++, Eight Edition • Data type of expression in an assignment statement must match data type of the variable • If they don’t match, compiler will use implicit type casting to get them to match – Doesn’t always produce correct result – Better to explicitly cast to correct data type yourself • Remember: – Declaration statement creates a new variable – Assignment statement assigns a new value to an existing variable Assignment Statements (cont’d.) 20
  • 21. An Introduction to Programming with C++, Eight Edition Assignment Statements (cont’d.) 21
  • 22. An Introduction to Programming with C++, Eight Edition • Allow you to abbreviate assignment statements that contain an arithmetic operator • Statement must be of the form variableName = variableName arithmeticOperator value • Abbreviated as variableName arithmeticOperator = value – Example: price = price*1.05; can be abbreviated as price *= 1.05; • Most common operators are += , -= , *= , /= , and %= Arithmetic Assignment Operators 22
  • 23. An Introduction to Programming with C++, Eight Edition Arithmetic Assignment Operators (cont’d) 23
  • 24. An Introduction to Programming with C++, Eight Edition The End ♥ ♥ ♥ 24