This is a draft cheat sheet. It is a work in progress and is not finished yet.
Definitions
Algorithm |
A guide of steps to solution. |
Flowcharts |
Graphical representation of algorithm |
Pseudocode |
Language used to represent algorithm. |
Preprocessing Directives |
Commands that are preceded by the # sign |
Memory |
Used to store information 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<<"text"; |
cin (Request input) |
cin>>variable |
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 |
"string" |
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 conversion. |
Format:
static_cast<type>(expression)
PEMDAS (Order of Operations)
Parentheses |
[], () |
Multiplication & Division |
*, /, % |
Addition and Subtraction |
+, - |
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 |
- |
Subtraction |
x-y |
* |
Multiplication |
x*y |
/ |
Division |
x/y |
% |
Modulus (integer arithmetic) |
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 expressions are true |
!! |
OR |
New relation expression is true if either is true |
! |
NOT |
Reverses the value of an expression. 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 ( conditional )
{
// do something
}
else if ( another_conditional )
{
// do something else
}
else
{
// do something as default
} |
Control Flow - While Loops
while ( conditional )
{
// do something
} |
For Loops
for ( initialization; test; command )
{
// do something
} |
Converting Between For & While
for ( initialization; test; command )
{
// do something
} |
this is equivalent to:
initialization;
while( test )
{
// do something
command;
}
|