C++ Programing Basics
C+ Terminology
• All procedure-like entities are called functions in C++.
• In other programming languages they are called
procedures, methods, functions, or subprograms.
• The main function in C++ is the function that the compiler
looks for to execute.
Simplest C++ Code Run/Execute
Compile and
Run button
button
Compile
button
The only content inside
the body of the main
function is the return 0;
statement.
Output of simple C++ Code
Nearly all statements (individual instructions)
in C++ must end with the terminator ( ; )
symbol.
The ‘system(“pause”);’ forces a pause with
the execution and waits for the user to press
any key so it can continue.
When any key is pressed, it continues to execute the
next statement
Breakdown of important parts
Iostream is the library that contains basic input-output
commands such as cout. It needs to be loaded.
By placing this line, we can just simply type cout when we
want to display onto the console. Without this line we have
to type the full command: std::cout whenever we want to
display an output onto console, even when we use cin.
Called the function header/head whose name is main.
Since int is the data type of the function (int main() ), then
before we end the body/definition of the function, we have
to return a value of that data type. In this case, just set the
value to 0.
The pair { } are called delimiters. They ‘group’ the
statements to indicate that it is a body of some function.
Traditional way of placing a ‘Newline’ in C++
\n is an escape sequence that triggers a new line.
Basically it forces what comes after to be written in the
next line instead of beside.
After a key is pressed…
Another way of placing a ‘Newline’ in C++
Appending (adding another ‘<<‘) endl to your cout
statement forces to what follows after the endl to be
placed in the next line.
After a key is pressed…
Appending the string…
If your message is long, you can append
how you write it in code by appending
(<<) the different parts. 1 cout statement
is broken down to 3 shortened parts.
As you can see, there is no difference in the output, it just makes the code easier to read that if cout was in 1
very long line.
Variables in C++
• Works the same way in Mathematics. They are
“containers“ for values that we may not yet know at the
moment.
Data
Value
Type
Memory
Identifier
Address
Variable
Identifier
Rules:
• Must always start with a letter
• Can have 1 or more characters
• Can use letters (both capital and small letters), digits and
the underscore (_) character.
• Must NOT contain spaces.
• It is case sensitive.
Case Sensitivity in Variables
Examples:
apples ≠ Apples
APPLES ≠ ApPLES
apples1forMe
Data Types
TYPE NAME DESCRIPTION MEMORY USED SIZE RANGE PRECISION
-2,147,483,648 to
int Integer (whole) 4 bytes Not applicable
2,147,483,647
-2,147,483,648 to
long Integer (whole) 4 bytes Not applicable
2,147,483,647
float Real (w/ fractions) 4 bytes Approx. 10-38 to 1038 7 digits
double Real (w/ fractions) 8 bytes Approx. 10-38 to 1038 15 digits
char 1 character 1 byte Not applicable
bool Boolean value 1 byte true, false Not applicable
Assignment Statement
Syntax: Variable = Expression
Example:
distance = rate * time; answer = 3 * (92 + 7);
count = count + 2; n = m = 2;
age = 15;
fruit = “durian”;
letter = ‘P’;
Basic math operators in C++
Operator Description Example
* Multiplication X*Y
/ Division X/Y
+ Addition X+Y
- Subtraction X–Y
% Modulus (remainder) X%Y
Compound Math Assignment Statements
Compound assignment statement Equivalent to
Count += 2; Count = Count + 2;
Bonus *= 2; Bonus = Bonus * 2;
Total -= Discount; Total = Total – Discount;
Time /= rushFactor; Time = Time / rushFactor;
Change %= 100; Change = Change % 100;
Amount *= num1 + num2; Amount = Amount * (num1 + num2)
Increment and Decrement Operators
INCREMENT operator: ++
It is a unary operator that adds a value of 1 to the
variable/expression it is attached to.
DECREMENT operator: --
It is a unary operator that subtracts a value of 1
from the variable/expression it is attached to.
Increment and Decrement Examples
int n = 1, m = 7; int n = 1, m = 7;
n++; m--;
cout << n ; cout << m ;
{the output would be 2} {the output would be 6}
Using Variables in C++ code
Rule:
You MUST declare the variable before you can use it in the
code.
Syntax: <Data Type> <Variable>;
<Data Type> <Variable1>, <Variable2>;
<Data Type> <Variable1> = <Value>;
Example of using Variables
Declaration of the variable
numOfSubjects that will hold integer
values and the direct assigning of the
value 5 to this variable.
Console Input / Output
cout – it displays to the console screen the values of
variables as well as strings of text.
cin – it is used to ‘read in’ the value or text typed in from the
keyboard onto the console.
CIN
Syntax: cin >> <Variable>;
cin >> <Variable> >> <Variable>;
Note:
It is advisable that you place a cout instructing what the
user is to type in before you place a cin.
User types in 3 and press the Enter key
After a key is pressed…