INTRODUCTION
C++ is a cross-platform language that can be used to create high-
performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C
language.
C++ gives programmers a high level of control over system resources
and memory.
SYNTAX
#include <iostream> using namespace std;
- header file library that lets us work - names for objects and variables from
with input and output objects the standard library.
int main() {} return 0;
- function - will be executed (inside) - successful execution or ends
the main function
<iostream> <vector>
cout - output/print (displaying text or variables) arrays
cin - input (keyboard input) <string>
endl – newline text and characters
cerr - error (displaying error message) <cctype>
clog - log (general logging message) tolower(), toupper()
<cmath> mathematical functions <cstdlib>
sqrt(), round(), log() exit or pause
<< - insertion (cout) \n - newline
>> - extraction (cin) \t - horizontal tab
== - comparison \\ - backslash
% - modulus (remainder) \" - double quote
ex. num % 2==0 \' - single quote
VARIABLES
Types Description Size
int integers (whole numbers) 2 or 4 bytes
double floating point numbers, with decimals, 15 digits 8 bytes
float with decimal points, 7 digits 4 bytes
char single characters, such as 'a' or 'B'. single quotes 1 byte
string text, double quotes none
bool values with two states: true or false 1 byte
when the value is returned, true = 1 and false = 0.
const unchangeable and read-only
auto automatically detects the type of a variable based on the value
you assign to it.
Syntax (proper way) - type variableName = value;
ARITHMETIC OPERATORS COMPARISON OPERATORS
+ addition - subtraction == equal to != not equal
* Multiplication / Division > greater than < less than
++ increment -- decrement >= greater than or equal to
% modulus <= less than or equal to
LOGICAL OPERATORS
Operators Name Description
&& and true if both statements are true
|| or true if one of the statements is true
! no reserve the result, false if the results is true
CONCATENATION
The + operator can be used between strings to add them together to make a
new string.
ex. string fullName = firstName + lastName;
length() or size() for string.
Ex. string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << [Link]();
// Ouputs 26
access string [], print first characters.
Ex. string myString = "Hello";
cout << myString[0];
// Outputs H
CONDITIONS AND IF STATEMENTS
if - to specify a block of code to be else - to specify a block of code to
executed, if a specified condition is true. be executed, if the same
condition is false
Example: Example:
if (20 > 18) { int time = 20;
cout << "20 is greater than 18"; if (time < 18) {
} cout << "Good day.";
} else {
cout << "Good evening.";
// Outputs "Good evening."
else if - to specify a new condition switch - to specify many alternative
to test, if the first condition is false. blocks of code to be executed.
Example: Example:
int time = 22; switch(expression) {
if (time < 10) { case x:
cout << "Good morning."; // code block
} else if (time < 20) { break;
cout << "Good day."; case y:
} else { // code block
cout << "Good evening."; break;
} default:
// Outputs "Good evening." // code block
Shorthand if else - ternary operator
Example:
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
LOOPS
While Loop Do/While Loop
while (condition) { do {
// code block to be executed } // code block to be executed }
while (condition);
For Loop
for (initialization(start); condition(stop); update(adds)) {
// code block to be executed }
Example:
for (int i = 1; i < 5; i++) {
cout << i << "\n"; }
ARRAYS
Example:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo