C++ Note
C++ Note
What is C++?
C++ gives programmers a high level of control over system resources and
memory.
The language was updated 5 major times in 2011, 2014, 2017, 2020, and
2023 to C++11, C++14, C++17, C++20, and C++23.
C++ is portable and can be used to develop applications that can be adapted
to multiple platforms.
C++ Variables
C++ Operators
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Arithmetic Operators
x%
% Modulus Returns the division remainder
y
++ Increment Increases the value of a variable by 1 ++x
Assignment Operators
Comparison Operators
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.
Logical Operators
As with comparison operators, you can also test for true (1) or false (0)
values with logical operators.
C++ Syntax
1. #include <iostream>
2. using namespace std;
3.
4. int main() {
5. cout << "Hello World!";
6. return 0;
7. }
Line 1: #include <iostream> is a header file library that lets us work with
input and output objects, such as cout (used in line 5). Header files add
functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and
variables from the standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the
code more readable.
Line 4: Another thing that always appear in a C++ program is int main().
This is called a function. Any code inside its curly brackets {} will be
executed.
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Line 7: Do not forget to add the closing curly bracket } to actually end the
main function.
Example:
1. Write a C++ program that will print your first and last name
#include <iostream>
using namespace std;
int main() {
You have already learned that cout is used to output (print) values. Now we
will use cin to get user input.
cin is a predefined variable that reads data from the keyboard with the
extraction operator (>>).
Example2. Write a C++ program that ask a user to input your first name and
display the output.
#include <iostream>
using namespace std;
int main() {
string firstName;
cout << "Type your first name: ";
cin >> firstName;
cout << "Your name is: " << firstName;
return 0;
}
C++ Booleans
Very often, in programming, you will need a data type that can only have one
of two values, like:
YES / NO
ON / OFF
TRUE / FALSE
Example:
#include <iostream>
int main() {
return 0;
You already know that C++ supports the usual logical conditions from
mathematics:
You can use these conditions to perform different actions for different
decisions.
The if Statement
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
EXAMPLE:
#include <iostream>
int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
return 0;
}
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example:
#include <iostream>
int main(){
int age;
cin>> age;
Else {
Cout<< “You are eligible to vote” << endl;
return 0;
Use the else if statement to specify a new condition if the first condition is
false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false
}
Example:
#include <iostream>
int main(){
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example:
The example below uses the weekday number to calculate the weekday
name:
#include <iostream>
using namespace std;
int main() {
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
C++ Loops
The while loop loops through a block of code as long as a specified condition
is true:
Syntax
while (condition) {
// code block to be executed
}
EXAMPLE: In the example below, the code in the loop will run, over and
over again, as long as a variable (i) is less than 5:
#include <iostream>
int main(){
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
return 0;
The do/while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat
the loop as long as the condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
EXAMPLE: The example below uses a do/while loop. The loop will always
be executed at least once, even if the condition is false, because the code
block is executed before the condition is tested:
#include <iostream>
int main(){
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
return 0;
When you know exactly how many times you want to loop through a block
of code, use the for loop instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
EXAMPLE:
#include <iostream>
int main(){
return 0;
Example explained
Statement 2 defines the condition for the loop to run (i must be less than 5). If
the condition is true, the loop will start over again, if it is false, the loop will
end.
Statement 3 increases a value (i++) each time the code block in the loop has
been executed.
C++ Arrays
To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should
store:
- One-Dimensional Array Example
#include <iostream>
int main() {
cout << "numbers[" << i << "] = " << numbers[i] <<
endl;
return 0;
#include <iostream>
#include <string>
int main() {
cout << "fruits[" << i << "] = " << fruits[i] <<
endl;
return 0;
#include <iostream>
int main() {
int size;
array[i] = i + 1;
cout << "array[" << i << "] = " << array[i] <<
endl;
delete[] array;
return 0;
#include <iostream>
int main() {
int array[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
columns
row
return 0;