SlideShare a Scribd company logo
1 of 30
1
C++ Syntax and Semantics
The Development Process
2
Why Study This Chapter?
To be able to
create identifiers
To declare
variables and
constants
To write
assignment and
I/O statements
To design and
write simple programs
3
C++ Program Structure
All sub programs are called functions
Every program has a function called main( )
Other modules
(sub programs or
functions) are
declared by
the programmer
4
Syntax & Semantics
Syntax <=> The formal rules governing how
valid instructions are written in a
programming language
Semantics <=> the set of rules determining
the meaning of instructions written in the
programming language
5
Naming Identifiers
Identifiers are used to name things
Made up of
letters (upper, lower case)
numerals (0-9)
under score _
Must begin with letter or underscore
6
Style Issues
Use meaningful identifiers
makes the program more self documenting
Use readable identifiers
7
Identifiers
Check whether legal and/or readable
Identifier legal
?
readable
?
r2d2
electric Bill
23_skidoo
Total_Amount
8
Data and Data Types
Distinction made between integers,
characters, rationals, etc.
Data type <=> specific set of data values
along with a set of operations on those values
Simple
- integers
- float
- char
Structured
- struct
- array
- union
- class
Address
- reference
- pointers
9
C++ Data Types
Structured
array struct union class
Address
pointer reference
Simple
Integral Floating
char short int long enum
float double long double
10
Integer Types
Whole numbers with no fractional part
23 876 -915
No commas allowed
Other variations of integer
short, long
different sizes available (for memory use)
Also char
‘a’ ‘Z’ ‘8’ ‘%’ ‘$’
11
Floating Point Types
Represent rational numbers
float, double, long double
Stored in the computer in scientific notation
+3.94x10-3
Leading
sign Significant
digits
Sign of
power of ten
Power of ten
12
2.7E4 means 2.7 x 10
4
=
2.7000 =
27000.0
2.7E-4 means 2.7 x 10
- 4
=
0002.7 =
0.00027
Scientific Notation
13
Declarations
Statement in a program that associates a
name (an identifier) with a memory location
Use the name to access or alter the contents
of the memory location
14
Variables
Characteristics
a location in memory
referenced by an identifier
contents of the location can be changed
Example:
int x, y = 0; x : ? y : 0
Unknown or “garbage” value for x
Value for y
initialized at
declaration
15
Variables
Characteristics
a location in memory
referenced by an identifier
contents of the location can be changed
Example:
int x, y = 0;
x = 5;
x : 5 y : 0
16
Variables
Characteristics
a location in memory
referenced by an identifier
contents of the location can be changed
Example:
int x, y = 0;
x = 5;
y = 3;
x : 5 y : 3
Old value
of 0 lost
17
Variables
Characteristics
a location in memory
referenced by an identifier
contents of the location can be changed
Example:
int x, y = 0;
x = 5;
y = 3;
x = y + 7;
x : 10 y : 3
Value stored in y
accessed, added to 7,
result stored in x
18
Using Named Constants
Characteristics
a location in memory
referenced by an identifier
value cannot be changed
Example
const int lines_per_page = 66;
19
Using Named Constants
Characteristics
a location in memory
referenced by an identifier
value cannot be changed
Example
const int lines_per_page = 66;
lines_per_page = 123;
ERROR 
Cannot alter a
constant
20
Style : Capitalization of Identifiers
Used as a visual clue to what an identifier
represents
Standard for our text
Variables:
begin with lower
case, cap
successive words
Functions:
begin with
upper case,
cap
successive
words
Named
Constants:
all caps, use
underscore
between words
21
Executable Statements
Output : send results of calculations, etc. to
screen, printer, or file
Example:
22
Executable Statements
Output : send results of calculations, etc. to
screen, printer, or file
Alternate Example:
Only a single cout
statement used. Repeat
use of << operator
23
Variable cout is predefined to denote an
output stream that goes to the standard
output device (display screen).
The insertion operator << called “put to”
takes 2 operands.
The left operand is a stream expression,
such as cout. The right operand is an
expression of simple type or a string
constant.
Insertion Operator ( << )
24
SYNTAX
These examples yield the same output.
cout << “The answer is “ ;
cout << 3 * 4 ;
cout << “The answer is “ << 3 * 4 ;
Output Statements
cout << ExprOrString << ExprOrString . . . ;
25
Program Comments
Purpose
for the human reader
the compiler ignores
Your programs
should contain
info as shown:
/* comments between */
// Comments follow
26
Program Construction
Shown is a typical program with one
function, the main( ) function
Variable
declaration
Assignment
statement
Output statement
Return for int
function main( )
27
Compound Statements
Body of a function is an example of a block
or compound statement
They are enclosed between a pair of curly
brackets { }
28
The C++ Preprocessor
#include statements tell the preprocessor
where to get certain declarations
Preprocessor runs before the compiler
All preprocessor commands preceded by #
29
Program Entry and Execution
Source code is a text file created by text
editor
Program is compiled
 Compiler notifies you of syntax (and some semantic) errors
Program is linked
 Code from #include’s is linked to your compiled code
Program is then run. You must check for
remaining semantic, logic errors
30
Testing and Debugging Hints
Watch for misspelled or undeclared
identifiers
C++ is case sensitive, watch for improper
Caps
Watch for integer division
int_x / int_y yields an integer result
Don’t confuse 0’s (zeros) with O’s in your
source code
Make sure statements end with semicolons ;

More Related Content

Similar to chapter-2.ppt

Similar to chapter-2.ppt (20)

C intro
C introC intro
C intro
 
Oop lec 1
Oop lec 1Oop lec 1
Oop lec 1
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Hello world! Intro to C++
Hello world! Intro to C++Hello world! Intro to C++
Hello world! Intro to C++
 
dinoC_ppt.pptx
dinoC_ppt.pptxdinoC_ppt.pptx
dinoC_ppt.pptx
 
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
PPT ON VHDL subprogram,package,alias,use,generate and concurrent statments an...
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Compiler Construction.pptx
Compiler Construction.pptxCompiler Construction.pptx
Compiler Construction.pptx
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
Unit 1
Unit  1Unit  1
Unit 1
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Introduction to Computer and Programming - Lecture 03
Introduction to Computer and Programming - Lecture 03Introduction to Computer and Programming - Lecture 03
Introduction to Computer and Programming - Lecture 03
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 

More from XanGwaps

MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptxMSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptxXanGwaps
 
AdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptxAdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptxXanGwaps
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptxXanGwaps
 
Chapter-OBDD.pptx
Chapter-OBDD.pptxChapter-OBDD.pptx
Chapter-OBDD.pptxXanGwaps
 
virtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptxvirtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptxXanGwaps
 
Java ConstructorsPPT.pptx
Java ConstructorsPPT.pptxJava ConstructorsPPT.pptx
Java ConstructorsPPT.pptxXanGwaps
 
AdvanceSQL.ppt
AdvanceSQL.pptAdvanceSQL.ppt
AdvanceSQL.pptXanGwaps
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxXanGwaps
 
mod5_cabling-LANsWANs-2.ppt
mod5_cabling-LANsWANs-2.pptmod5_cabling-LANsWANs-2.ppt
mod5_cabling-LANsWANs-2.pptXanGwaps
 
globodox-presentation-v14.ppsx
globodox-presentation-v14.ppsxglobodox-presentation-v14.ppsx
globodox-presentation-v14.ppsxXanGwaps
 
Requirements Analysis.pptx
Requirements Analysis.pptxRequirements Analysis.pptx
Requirements Analysis.pptxXanGwaps
 
Java Constructors.pptx
Java Constructors.pptxJava Constructors.pptx
Java Constructors.pptxXanGwaps
 
Java Constructors.pptx
Java Constructors.pptxJava Constructors.pptx
Java Constructors.pptxXanGwaps
 
CS530-Tuesday01.ppt
CS530-Tuesday01.pptCS530-Tuesday01.ppt
CS530-Tuesday01.pptXanGwaps
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxXanGwaps
 
Functions of Operating System.pptx
Functions of Operating System.pptxFunctions of Operating System.pptx
Functions of Operating System.pptxXanGwaps
 
HCI 1 Module 2.pptx
HCI 1 Module 2.pptxHCI 1 Module 2.pptx
HCI 1 Module 2.pptxXanGwaps
 
The virtual box.pptx
The virtual box.pptxThe virtual box.pptx
The virtual box.pptxXanGwaps
 
Presentation (10).pptx
Presentation (10).pptxPresentation (10).pptx
Presentation (10).pptxXanGwaps
 
Presentation (4).pptx
Presentation (4).pptxPresentation (4).pptx
Presentation (4).pptxXanGwaps
 

More from XanGwaps (20)

MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptxMSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
MSITSytemDesignAndDesignasdsdsdsdsdsds.pptx
 
AdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptxAdvanceDatabaseChapter6Advance Dtabases.pptx
AdvanceDatabaseChapter6Advance Dtabases.pptx
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
 
Chapter-OBDD.pptx
Chapter-OBDD.pptxChapter-OBDD.pptx
Chapter-OBDD.pptx
 
virtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptxvirtualization-220403085202_Chapter1.pptx
virtualization-220403085202_Chapter1.pptx
 
Java ConstructorsPPT.pptx
Java ConstructorsPPT.pptxJava ConstructorsPPT.pptx
Java ConstructorsPPT.pptx
 
AdvanceSQL.ppt
AdvanceSQL.pptAdvanceSQL.ppt
AdvanceSQL.ppt
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptx
 
mod5_cabling-LANsWANs-2.ppt
mod5_cabling-LANsWANs-2.pptmod5_cabling-LANsWANs-2.ppt
mod5_cabling-LANsWANs-2.ppt
 
globodox-presentation-v14.ppsx
globodox-presentation-v14.ppsxglobodox-presentation-v14.ppsx
globodox-presentation-v14.ppsx
 
Requirements Analysis.pptx
Requirements Analysis.pptxRequirements Analysis.pptx
Requirements Analysis.pptx
 
Java Constructors.pptx
Java Constructors.pptxJava Constructors.pptx
Java Constructors.pptx
 
Java Constructors.pptx
Java Constructors.pptxJava Constructors.pptx
Java Constructors.pptx
 
CS530-Tuesday01.ppt
CS530-Tuesday01.pptCS530-Tuesday01.ppt
CS530-Tuesday01.ppt
 
Object-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptxObject-Oriented Systems Analysis and Design Using UML.pptx
Object-Oriented Systems Analysis and Design Using UML.pptx
 
Functions of Operating System.pptx
Functions of Operating System.pptxFunctions of Operating System.pptx
Functions of Operating System.pptx
 
HCI 1 Module 2.pptx
HCI 1 Module 2.pptxHCI 1 Module 2.pptx
HCI 1 Module 2.pptx
 
The virtual box.pptx
The virtual box.pptxThe virtual box.pptx
The virtual box.pptx
 
Presentation (10).pptx
Presentation (10).pptxPresentation (10).pptx
Presentation (10).pptx
 
Presentation (4).pptx
Presentation (4).pptxPresentation (4).pptx
Presentation (4).pptx
 

Recently uploaded

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

chapter-2.ppt

  • 1. 1 C++ Syntax and Semantics The Development Process
  • 2. 2 Why Study This Chapter? To be able to create identifiers To declare variables and constants To write assignment and I/O statements To design and write simple programs
  • 3. 3 C++ Program Structure All sub programs are called functions Every program has a function called main( ) Other modules (sub programs or functions) are declared by the programmer
  • 4. 4 Syntax & Semantics Syntax <=> The formal rules governing how valid instructions are written in a programming language Semantics <=> the set of rules determining the meaning of instructions written in the programming language
  • 5. 5 Naming Identifiers Identifiers are used to name things Made up of letters (upper, lower case) numerals (0-9) under score _ Must begin with letter or underscore
  • 6. 6 Style Issues Use meaningful identifiers makes the program more self documenting Use readable identifiers
  • 7. 7 Identifiers Check whether legal and/or readable Identifier legal ? readable ? r2d2 electric Bill 23_skidoo Total_Amount
  • 8. 8 Data and Data Types Distinction made between integers, characters, rationals, etc. Data type <=> specific set of data values along with a set of operations on those values Simple - integers - float - char Structured - struct - array - union - class Address - reference - pointers
  • 9. 9 C++ Data Types Structured array struct union class Address pointer reference Simple Integral Floating char short int long enum float double long double
  • 10. 10 Integer Types Whole numbers with no fractional part 23 876 -915 No commas allowed Other variations of integer short, long different sizes available (for memory use) Also char ‘a’ ‘Z’ ‘8’ ‘%’ ‘$’
  • 11. 11 Floating Point Types Represent rational numbers float, double, long double Stored in the computer in scientific notation +3.94x10-3 Leading sign Significant digits Sign of power of ten Power of ten
  • 12. 12 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10 - 4 = 0002.7 = 0.00027 Scientific Notation
  • 13. 13 Declarations Statement in a program that associates a name (an identifier) with a memory location Use the name to access or alter the contents of the memory location
  • 14. 14 Variables Characteristics a location in memory referenced by an identifier contents of the location can be changed Example: int x, y = 0; x : ? y : 0 Unknown or “garbage” value for x Value for y initialized at declaration
  • 15. 15 Variables Characteristics a location in memory referenced by an identifier contents of the location can be changed Example: int x, y = 0; x = 5; x : 5 y : 0
  • 16. 16 Variables Characteristics a location in memory referenced by an identifier contents of the location can be changed Example: int x, y = 0; x = 5; y = 3; x : 5 y : 3 Old value of 0 lost
  • 17. 17 Variables Characteristics a location in memory referenced by an identifier contents of the location can be changed Example: int x, y = 0; x = 5; y = 3; x = y + 7; x : 10 y : 3 Value stored in y accessed, added to 7, result stored in x
  • 18. 18 Using Named Constants Characteristics a location in memory referenced by an identifier value cannot be changed Example const int lines_per_page = 66;
  • 19. 19 Using Named Constants Characteristics a location in memory referenced by an identifier value cannot be changed Example const int lines_per_page = 66; lines_per_page = 123; ERROR  Cannot alter a constant
  • 20. 20 Style : Capitalization of Identifiers Used as a visual clue to what an identifier represents Standard for our text Variables: begin with lower case, cap successive words Functions: begin with upper case, cap successive words Named Constants: all caps, use underscore between words
  • 21. 21 Executable Statements Output : send results of calculations, etc. to screen, printer, or file Example:
  • 22. 22 Executable Statements Output : send results of calculations, etc. to screen, printer, or file Alternate Example: Only a single cout statement used. Repeat use of << operator
  • 23. 23 Variable cout is predefined to denote an output stream that goes to the standard output device (display screen). The insertion operator << called “put to” takes 2 operands. The left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant. Insertion Operator ( << )
  • 24. 24 SYNTAX These examples yield the same output. cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; Output Statements cout << ExprOrString << ExprOrString . . . ;
  • 25. 25 Program Comments Purpose for the human reader the compiler ignores Your programs should contain info as shown: /* comments between */ // Comments follow
  • 26. 26 Program Construction Shown is a typical program with one function, the main( ) function Variable declaration Assignment statement Output statement Return for int function main( )
  • 27. 27 Compound Statements Body of a function is an example of a block or compound statement They are enclosed between a pair of curly brackets { }
  • 28. 28 The C++ Preprocessor #include statements tell the preprocessor where to get certain declarations Preprocessor runs before the compiler All preprocessor commands preceded by #
  • 29. 29 Program Entry and Execution Source code is a text file created by text editor Program is compiled  Compiler notifies you of syntax (and some semantic) errors Program is linked  Code from #include’s is linked to your compiled code Program is then run. You must check for remaining semantic, logic errors
  • 30. 30 Testing and Debugging Hints Watch for misspelled or undeclared identifiers C++ is case sensitive, watch for improper Caps Watch for integer division int_x / int_y yields an integer result Don’t confuse 0’s (zeros) with O’s in your source code Make sure statements end with semicolons ;