You are on page 1of 10

Shortcuts (link)

Build
Function

Shortcut Key

Build

Ctrl + F9

Compile current file

Ctrl + Shift + F9

Run

Ctrl + F10

Build and Run

F9

Rebuild

Ctrl + F11

Files
Function

Shortcut Key

New file or project

Ctrl + N

Open existing file or project

Ctrl + O

Save current file

Ctrl + S

Save all files

Ctrl + Shift + S

Close current file

Ctrl + F4 / Ctrl + W

Close all files

Ctrl + Shift + F4 / Ctrl + Shift + W

Remember to save as .cpp!

Escape code
\n
\r
\t
\v
\b
\f
\a
\'
\"
\?
\\

Source: cplusplus

Description
newline
carriage return
tab
vertical tab
backspace
form feed (page feed)
alert (beep)
single quote (')
double quote (")
question mark (?)
backslash (\)

The big list of commands


Variables
Type
Integer
Real
numbers
String

Command
Int

Usage

Double
String

string mystring;
cin >> mystring;

Proper coding format

Lecture 0
Basic commands

#include <iostream>
#include <iomanip>
Using namespace std
Int main ()
{
}

Required for field width,set precision


etc
Allows entry of cout instead of std::cout
Body of the programme

Body outline of a C++ code


OR if you dont want to use
namespace std
#include <iostream>
Using namespace std; Remember
the semi colon!
Int main ()
{
cout<<Hello, this better work!<<
endl;
return 0;
}

#include <iostream>
int main ()
{
std::cout << "Hello World! ";
std::cout << "I'm a C++ program";
return 0;
}

Lecture 1 Data Types


Commands:
Int x,y
Double x,y
Setprecision(g)
Char t
Type of text character with memory size
of exactly one byte (8 bits) => 256
possible values
Range of char type is -128 to 127
Range of unsigned char is 0 to 255
Difference between char and unsigned
char:
Declared as char => Left-most bit is
considered a sign bit. If sign bit =1, it is
negative
Declared as unsigned char, then there
is no sign bit

Bool v

Declare two integer variables x and y


Declare two double-precision floatingpoint
Use g-digit precision (make sure
<iomanip> is included)
Declare a character variable t (Strings)
Two ways
Eg char x1=A or char x2=65; The
values of x1 and x2 are the same.
Use int(xn) to reveal the integer of x1

Declare a Boolean variable v (True or

false)

Two ways of defining variables


At the start
#include <iostream>
using namespace std;

After declaring the variables


#include <iostream>
using namespace std;

int main()
{int x=30,y=50,z=70,sum;
sum=x+y+z;
cout<<sum<<endl;
return 0;
}

int main()
{int x,y,z,sum;
x=30;
y=50;
z=70;
sum=x+y+z;
cout<<sum<<endl;
return 0;
}

Computer data
-Bit
Only one of two values (0 and 1)
-Byte
A byte contains of eight bits b7 b6 b5 b4 b3 b2 b1 b0
Two possible values of each bit => 2^8 = 256 possible values for each byte
This is the Base-2 (binary) number system
Base-10 number system
Eg The value of 326 = 3x10^2 + 2x10^1 + 6x10^0
For a 4-bit binary number b3 b2 b1 b0, its decimal value is:
B3x2^3
Points to take note when using char
Code used:
Cout<<z1 is <<z1<<Its integer value is <<int(z1)<<endl
Cout<<z2 is <<z1<<Its integer value is <<int(z2)<<endl

Two cases showing the dangers:

Char y1=9 char y2=9


The values of y1 and y2 are different
(using int(x))
Character with integer value 9 is not
printable

Char z1=35 char z2=35


Char z1: will give us an overflow
Char z2:two characters cannot be
stored into one byte, only 5 is stored

Result:
Y1 is 9 its integer value is 57
Y2 is blank its integer value is 9

Result:
Z1 is # its integer value is 35
Z2 is 5 its integer value is 53

Data type: bool


Boolean type with only two possible values, and the memory size is one byte
The value of a bool variable is true or false
-Boolean false is stored as an integer with value 0
-Boolean true is stored as an integer with value 1
Eg bool x;
X=true;
Cout << x is <<x<<endl
Result: x is 1
Note that when a non-zero value is assigned to a bool variable, it is the same as
assigning 1 to that variable which gives true
Data type: Int
4 Types:
Short int
Int
Long int
Long long int
Eg int x,y;

2
4
4
8

bytes
byte
bytes
bytes

X=56
Y=76.89
Cout<<x is <<x<<endl
Cout<<y is <<y<<endl
Gives us x is 56, y is 76 (Only stores the integer portion)

Data type: Double


-Specified in either of two formats (6-digit precision)
Fixed point format: 34.567
Scientific format: 5.71e103

Lecture 2 - Operators
Filler
Operators Commands and observations

operator

description
addition
subtraction
multiplication
division
modulo

+
*
/
%

Increments (++ and --)


Int x,y;
x=3
Order of increments matter!
y=++x
Increment of x by 1. X: 3 => 4
y is x y=4
X contains 4, y contains 4

y=x++
Y is x y=3
Increment of y by 1.
X contains 3, y contains 4

Compound assignments (Only use them if all variables are defined)


Expression
x+=y
x-=5
x/=y
Price *= units+1
If a variable is not defined, eg

Equivalent to
x=x+y
X=x-5
X=x/y
Price = price*(units+1)

Int x=3,y;
y+=3 will result in overflow!
But y=+3 is okay, and will result in 3
operator

==
!=

description
Equal to
Not equal to

<
>
<=
>=

Less than
Greater than
Less than or equal to
Greater than or equal to

http://www.cplusplus.com/doc/tutorial/operators/

Lecture 3 Control Statements


Control statement
While
Sample Code:

Syntax
while (expression) statement
Output:
Explanation:

// custom countdown using while


int main ()
{
int n = 10;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "liftoff!\n";
}

10, 9, 8, 7, 6, 5, 4, 3, 2,
1, liftoff!

Do-while
Sample Code:

do (statement) while (expression)


Output:
Explanation:

// echo machine
int main ()
{
string str;
do {
cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str
<< '\n';
} while (str != "goodbye");
}

Enter text: hello


You entered: hello
Enter text: who's there?
You entered: who's there?
Enter text: goodbye
You entered: goodbye

For
Sample Code:

for (initialization; condition; increase) statement


Output (same as while
Explanation:
example)

// countdown using a for loop


int main ()
{
for (int n=10; n>0; n--) {
cout << n << ", ";
}
cout << "liftoff!\n";
}

10, 9, 8, 7, 6, 5, 4, 3, 2,
1, liftoff!

Switch (and break)


Sample code:
switch (x) {
case 1:
cout << "x is 1";
break;
case 2:
cout << "x is 2";
break;
default:
cout << "value of x unknown";
}

switch (variable)
Depends on the value of
x.
Case n
X is n

Explanation: The command


takes a variable x and gives
different output based on the
cases defined.
Cases need not be case 1,2,3,
can be any integer!
i.e x=5;
case 5 will be called

Lecture 4 and 5 Functions


Sneak preview of whats to come
// function example
#include <iostream>
using namespace std;
int addition(int,int); OPTIONAL. Also called the function prototype.
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}

type name ( parameter1, parameter2, ...) { statements }


Where:
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type followed by an identifier, with
each parameter being separated from the next by a comma. Each parameter looks very much like a
regular variable declaration (for example:int x), and in fact acts within the function as a regular
variable which is local to the function. The purpose of parameters is to allow passing arguments to the
function from the location where it is called from.

- statements is the function's body. It is a block of statements surrounded by braces { } that specify
what the function actually does.

Factorial function:
// A recursive function is one that calls itself
int getFactorial(int number)
{
int sum;
if(number == 1) sum = 1;
else sum = (getFactorial(number - 1) * number);
return sum;

// getFactorial(2) [Returns 2] * 3
// getFactorial(1) [Returns 1] * 2 <This value goes above>
// 2 * 3 = 6

Lecture 6 Arrays and random numbers


Declaring arrays two ways:
Int bar[3]={n1,n2,n3} 3 elements only
Int bar[]={n1,n2,n3,...,nk } Let the compiler decide the number of elements

You might also like