Chapter 02
Chapter 02
Computer Programming
Faculty Of Industrial Information Technology
C++ Programming:
From Problem Analysis
to Program Design, Second Edition
• Special
symbols
• + • ?
• - • ,
• * • <=
• / • !=
• . • ==
• ; • >=
• Word symbols
− Reserved words, or keywords
− Include:
• Int
• Float
• Double
• Char
• Void
• Return
C++ Programming: From Problem Analysis to Program Design, Second Edition 9
Identifiers
− Pointers
C++ Programming: From Problem Analysis to Program Design, Second Edition 12
Simple Data Types
• Examples:
− -6728
−0
− 78
• Positive integers do not have to have a + sign
in front of them
• No commas are used within an integer
• Commas are used for separating items in a
list
C++ Programming: From Problem Analysis to Program Design, Second Edition 15
bool Data Type
• bool type
• Mixed expression:
− Has operands of different data types
− Contains integers and floating-point
− static_cast<dataTypeName>(expression)
a = 45;
cout<<a;
produces an output of 45
#include <headerFileName>
#include <iostream>
#include <iostream>
#include <string>
• Declaration Statements
int a, b, c;
double x, y;
− Variables can be declared anywhere in the
program, but they must be declared before they
can be used
• Executable Statements have three forms:
a = 4; //assignment statement
cin>>b; //input statement
cout<<a<<endl<<b<<endl; //output statement
C++ Programming: From Problem Analysis to Program Design, Second Edition 53
Use of Blanks
• Use of Blanks
− One or more blanks separate input numbers
− Blanks are also used to separate reserved words and
identifiers from each other and other symbols
• Blanks between identifiers in the second
statement are meaningless:
− int a,b,c;
− int a, b, c;
• In the statement: inta,b,c;
no blank between the t and a changes the reserved
word int and the identifier a into a new identifier, inta.
C++ Programming: From Problem Analysis to Program Design, Second Edition 54
Semicolons, Brackets, & Commas
• Example:
x *= y;
− Output centimeters
• Variables
− int feet; //holds given feet
− int inches; //holds given inch
− int totalInches; //holds total inches
− double centimeters; // holds length in centimeters
• Named Constant
− const double conversion = 2.54;
− const int inchesPerFoot = 12;