Note: All code snippets in this code are shown assuming the readers have used using namespace std
in their code.
What is C++?
C++ is a high-level, general-purpose programming language by the Danish Computer Scientist Bjarne Stroustrup, released in 1985 as an extension of C and has undergone various changes over time and is used in various applications such as Game Development, OS Development, Compilers and more.
Structure of a C++ program
The base of a C++ program has the following structure
#include <iostream>
using namespace std;
// driver code
int main() {
// block of code
return 0
}
Let us understand the code line by line
In line 1, we import a library using
#include
; we are importing the iostream library, which allows us to perform various input/output operations.In line 2,
using namespace std
allows developers to use identifiers from the standard libraries without needing to prefix them withstd::
, as a result, we can usecout <<
instead ofstd::cout <<
In line 4, we see that the method
main
has a return typeint
; this is because after the program is executed, the program tells the OS that it has completed/failed its execution. Themain
is the starting execution point for any C++ program.In line 5, we see we're returning
0
; this tells the OS that the program has successfully been executed; if it's any Non-Zero value indicates that the program has failed its execution. Ifreturn
is not explicitly mentioned, the compiler automatically adds areturn 0
by default.
Variables
-
int
: stores integers (whole numbers), ex: 103, 152312 -
double
: stores decimal numbers, ex: 12.23, 142.231 -
char
: stores single characters, such as 'a' or 'b', and must be within single quotes. -
string
: stores a sequence of characters as text, such as "This is an example text", and must be within double quotes -
bool
: stores only two values: true or false
I/O Operations
In C++, the two standard ways of accepting input and displaying information is as follows:
Input
cin >>
To accept a single variable input, you can do it as
cin >> variable_name
And if you have multiple input data, you can do it as
cin >> variable1 >> variable2 >> variable3
Output
cout <<
To display a single variable, you can do it as follows
cout << variable_name
If you want to display multiple data, you can do it as follows
cout << variable1 << variable2 << variable3
You can end your cout
line with endl
to move the cursor to the newline to display the following output.
You can format your output in two ways.
- Using the standard
cout
cout << "Value of a is: " << a << " and the value of b is " << b;
- Using the
format
library (Available for C++ 20) :
string formatted_string = format("Value of a is: {} and the value of b is {}", a, b);
cout << formatted_string<<endl;
Here, {}
acts as placeholders, and the data displayed is mapped to the occurrence of the placeholder and the corresponding variable. In this example, the first placeholder is after :
, and the corresponding variable is a
, so the value of a
will be displayed in the first placeholder, and similarly, the value of b
will be displayed in the second placeholder.
Conditional Statements
Conditional Statements are statements that help control the flow of your program based on conditions. There are three types of conditional statements: Simple If
, If-Else
and If-Else If
.
-
Simple If
In this, if a certain condition is met, a block of code is executed before continuing with the rest of program. This is used when you want to run only a single block of code based on a condition. ASimple If
can be written as shown below:
if (condition) {
// your code block here
}
//rest of your code
-
If-Else
In this, you will have two blocks of code, if a condition is met, the first block of code is executed and the second block of code is ignored before continuing with the rest of the program and if the condition is not met, the first block of code is ignored and the second block of code is executed. TheIf-Else
statement can be written as follows:
if (condition) {
// block 1
} else {
// block 2
}
-
If - Else If
In this, you will have multiple blocks of code, each to be executed on distinct conditions. It follows the same logic as If-Else but for multiple blocks. TheIf-Else If
statement can be written as follows:
if (condition 1) {
// block 1
} else if(condition 2) {
// block 2
} else if(condition 3) {
// block 3
} else if(condition 4) {
// block 4
} else {
// block 5
}
Loops
A loop allows you to execute a certain block of code as long as a condition is met. There are three types of loops in C++: for loop
, do while loop
, and while loop
.
for loop
This loop allows you to execute a block of code repeatedly as long as a condition is met by using an iterator (a variable that is initialized in the header of the loop), which is checked against a condition and whose value changes in each iteration. A for loop
can be written as follows:
for (data_type iterator_name; condition; value_modification) {
// block of code
}
Here:
-
data_type
can beint
,float
,double
-
value_modification
changes the value of the iterator; you can either increment usingiterator_name++
or++iterator_name
, decrement usingiterator_name--
or--iterator_name
, or perform other operations like multiplication, division, modulus, etc.
while loop
A while loop
is an entry-controlled loop; it first checks if the condition is met before running the block of code. As a result, the block of code may not be executed at all if the condition is not met initially.
while (condition) {
// block of code
}
do while loop
A do while loop
is an exit-controlled loop due to its structure. The block of code is first executed and then the condition is checked. As a result, the block of code is guaranteed to execute at least once.
do {
// block of code
} while (condition);
For each
A For each
loop is an iterator through each element in an array or string. In this loop, you access the element directly instead of using the value as an index to access array elements. Here the data type of the iterator depends on the data type of the array, if the array contains only int
values, the iterator will be int
and if it's a string
(C++ 20) or a character array, the iterator will be char
. The two ways of using For each
is shown below:
- Strings (C++ 20)
string a = "This ai is cool";
for(char c: a){
cout << c << endl;
}
- Arrays
char a[] = "This ai is cool";
for(char c: a){
cout << c << endl;
}
Functions
Functions is a block of code that is used to perform a specific operation. Functions are useful when you have blocks of code that can be re-used. A function in C++ can be declared as follows:
RD function_name(optional parameters: data_type variable_name) {
// block of code
// return value (if function is not void)
}
Here RD: Return Data Type: A function may or not return a value to the caller of the function, if this is the case, the return data type will be void
, else if there is a value to be returned, it can be of the type to be returned, such as char
, int
, float
, double
, etc.
int sum(int a, int b) {
int s = a + b;
return s;
}
Top comments (0)