Show Menu
Cheatography

C++ Exam 1 Cheat Sheet (DRAFT) by

Made by Fahad Alfataih

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Defini­tions

Algorithm
A guide of steps to solution.
Flowcharts
Graphical repres­ent­ation of algorithm
Pseudocode
Language used to represent algorithm.
Prepro­cessing Directives
Commands that are preceded by the # sign
Memory
Used to store inform­ation for immediate use
Bit
Stores 0 or 1
Byte
Group of eight bits

Comments

Line comment
// text
Block comment
/ * text * /

Output & Input Statements

cout (Print)
cout<<­"­tex­t";
cin (Request input)
cin>>v­ariable

Constants

Used to represent values used throughout the program.
Constants should be named in uppercase letters.

Escape Sequences

\n
Newline
Cursor moves to next line
\t
Tab
Cursor moves to next tab stop
\b
Backspace
Cursor moves one space to left
\r
Return
Cursor returns to beginning of current line
\\
Backslash
Backslash is printed
\'
single quotation
single quotation is printed
\"
Double quotation
Double quotation is pritned
 

Literals

integer
4
floating point
3.1415926
string
"­str­ing­"
Boolean
false
character
'A'

Data types

int
integer literals
float
floating point literals
double
floating point literals
string
string literal
char
character literal
bollean
boolean literal

Hierarchy of Types

long double (Highest)
double
float
unsigned long
long
unsigned int
int
char (Lowest)

Type Conversion

Implicit Type Coercion
Automatic change in type of value
Type Coercion
Automatic change of an operand to another type
Promotion
Convert to higher type
Demotion
Convert to lower type

Casting

Also called type casting. Used for manual data type conver­sion.
Format:
static­_ca­st<­typ­e>(­exp­res­sion)

PEMDAS (Order of Operat­ions)

Parent­heses
[], ()
Multip­lic­ation & Division
*, /, %
Addition and Subtra­ction
+, -

Switch Statements

switch ( variable )
{
case value1:
// do something
break;
case value2:
// do something else
break;
default:
// do something by default
break;
}

Converting between Switch & if

switch ( variable )
{
case value1:
// do something
break;
case value2:
// do something else
break;
default:
// do something by default
break;
}
This is equivalent to:

if ( variable == value1 )
{
// do something
}
else if ( variable = value2 )
{
// do something else
}
else
{
// do something by default
}
 

Arithmetic Operators

+
Addition
x+y
-
Subtra­ction
x-y
*
Multip­lic­ation
x*y
/
Division
x/y
%
Modulus (integer arithm­etic)
x % y

Relational Operators

<
Less than
x < y
>
Greater than
x > y
<=
Less than or equal to
x <= y
>=
Greater than or equal to
x >=y
!=
Not equal to
x != y

Logical Operators

&&
AND
NEw relational expression is true if both expres­sions are true
!!
OR
New relation expression is true if either is true
!
NOT
Reverses the value of an expres­sion. True to false and false to true
Order from highest to lowest: !, &&, ||

Precedence for ALL Operators

Arithmetic Operators (Highest)
Relational Operators
Logical Operators (Lowest)

Increment and Decrement

prefix
++x,--y
postifx
x++,y--
For prefix, the value changes and then get evaluated, and vice versa for the postfix.

Control Flow - If Statements

if ( condit­ional )
{
// do something
}
else if ( anothe­r_c­ond­itional )
{
// do something else
}
else
{
// do something as default
}

Control Flow - While Loops

while ( condit­ional )
{
// do something
}

For Loops

for ( initia­liz­ation; test; command )
{
// do something
}

Converting Between For & While

for ( initia­liz­ation; test; command )
{
// do something
}
this is equivalent to:
initia­liz­ation;
while( test )
{
// do something
command;
}