Professional Documents
Culture Documents
Build
Function
Shortcut Key
Build
Ctrl + F9
Ctrl + Shift + F9
Run
Ctrl + F10
F9
Rebuild
Ctrl + F11
Files
Function
Shortcut Key
Ctrl + N
Ctrl + O
Ctrl + S
Ctrl + Shift + S
Ctrl + F4 / Ctrl + W
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 (\)
Command
Int
Usage
Double
String
string mystring;
cin >> mystring;
Lecture 0
Basic commands
#include <iostream>
#include <iomanip>
Using namespace std
Int main ()
{
}
#include <iostream>
int main ()
{
std::cout << "Hello World! ";
std::cout << "I'm a C++ program";
return 0;
}
Bool v
false)
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
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
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)
Lecture 2 - Operators
Filler
Operators Commands and observations
operator
description
addition
subtraction
multiplication
division
modulo
+
*
/
%
y=x++
Y is x y=3
Increment of y by 1.
X contains 3, y contains 4
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/
Syntax
while (expression) statement
Output:
Explanation:
10, 9, 8, 7, 6, 5, 4, 3, 2,
1, liftoff!
Do-while
Sample Code:
// echo machine
int main ()
{
string str;
do {
cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str
<< '\n';
} while (str != "goodbye");
}
For
Sample Code:
10, 9, 8, 7, 6, 5, 4, 3, 2,
1, liftoff!
switch (variable)
Depends on the value of
x.
Case n
X is n
- 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