bool
char
short
int
long
float
double
C++ has
applied
applied
natural
Data Type
bool
char
int
double
c-string
char firstNname[16];
string lastName;
Relational Operators
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
==
Equal to
!=
Not equal to
Logical Operators
&&
AND
||
OR
!
NOT
Increment/Decrement
++
Increment
-Decrement
Increment/Decrement (used in prefix and postfix mode)
prefix: inc(dec) variable, then use in larger expression
postfix: use in larger expression, then inc(dec) variable
//firstName is a c-string
//lastName is a string object
Selection Structure
Information
Arithmetic Operators
+
Addition
Subtraction
*
Multiplication
/
Division (floating-point or integer)
2.0/3.0 =.666667 (floating-point), 2/3 = 0 (integer)
Assignment Operators
=
Assignment
+=
Combined addition/assignment
-=
Combined subtraction/assignment
*=
Combined multiplication/assignment
/=
Combined division/assignment
%=
Combined modulus/assignment
Conditional Operator ? :
(Simplified if-else)
Example
if (expression)
statement;
if (x < y)
x++;
if/else
Example
if (expression)
statement;
else
statement;
if (x < y)
x++;
else
x--;
Example
if (expression)
statement;
else
if (expression)
statement;
else
statement;
if (x < y)
x++;
else
if (x < z)
x--;
else
y++;
The
"expression" in
the parentheses
for an
if statement
or
loop
is often also
referred to as a
"condition"
Escape Sequences
Special characters in Java
\n
\t
\"
\'
\\
newline character
tab character
double quote
single quote
backslash
'\n'
'\t'
'\"'
'\''
'\\'
Example
if (expression)
{
statement;
statement;
}
if (x < y)
{
x++;
cout << x << endl;
}
( )
---------*, /, %
---------+, Precedence of operator types in a mixed
expression:
(1) arithmetic
(2) relational
(3) logical
(4) assignment
while, for
C++ Post-test loop
dowhile
Loop Control:
Counter-controlled
aka definite loops have
3 expressions:
Initialization, Test, Update
Sentinel-controlled
aka indefinite loops have 2 expressions:
Form:
switch (expression)
{
case int-constant :
statement(s);
[ break; ]
Example:
switch (choice)
{
case 0 :
cout << You selected 0. << endl;
break;
case int-constant :
statement(s);
[ break; ]
case 1:
cout << You selected 1. << endl;
break;
default :
cout << Select 0 or 1. << endl;
}
The type of the "expression" is integral - usually an expression of type int but it could also be
an expression of type char.
[ default :
statement; ]
Form:
Use the break keyword to exit the structure (avoid falling through other cases).
Use the default keyword to provide a default case if none of the case expressions match
(similar to trailing else in an if-else-if statement).
The collection can be a C-style array, array object, vector object, etc.
array<int, 20> myArray; //declare a 20 element array class object of type int
for (int param : myArray) //would iterate over all 20 elements
Each iteration variable param would be assigned the value of the current element
in myArray. Access to the collection is read-only unless a reference variable is used;
Reference variable example: for (int & param : MyArray)
param *= 2;
Example:
init;
while (test)
{
statement(s);
update;
}
int x=0;
while (x < 100)
{
cout << x << endl;
x++;
}
Theauto
forkeyword
Loop can be used to declare the parameter (auto [&] param)
The
Form:
Example:
Form:
Example:
do
do
Description
changes mode to fixed -number displays withinteger and decimal
digits
sets the number of significant digits or decimal digits if used in
conjunction with fixed.
sets field width (used for input and output )
sets left justification
sets right justification
forces decimal point & trailing zeros to display
sets scientific notation
statement;
endl;
while (expression);
do
{
do
{
statement;
statement;
} while (expression);
Description
.getline(array, size)
Creating and using file stream objects: Requires fstream header file: #include<fstream>
Classes
for creating file stream objects:
ifstream
create a file stream object for use with an input file
ofstream
create a file stream object for use with an output file
.get(array, size)
.get(ch)
.get( )
.ignore( )
.ignore(50,'\n')
.fail( )
.clear( )
.eof( )
.peek( )
.unget( )