This is the last part of Introduction to C++. In the I part, we discussed basic things like code structure and Variables. If you still need to read it, please click this. In the II part, we talked about input, operators, and conditional statements. If you still need to read it, please click this. This post specifically focuses on the main fundamental concepts in C++ like loops and functions.
Loops
Not only C++ but also any programming language has loops. Let's discuss why these loops are important. For example, think you need to print 5 times "Hello World!". You can do it using the below code and the output is also shown.
The work is done. But it has only five. Think we need to print 1000 times "Hello World!". It's not easy. Not 1000 think 100000, it is not practical to do it in this method. As a solution for this, we can use loops. There are two main loops in C++. They are "For Loop" and "While Loop".
For Loop
Let's see the solution to the above problem using the "for" loop. After that, we can talk about the "for" loop. Here is the implemented code and output.
This is the syntax of the for loop. The "i" is called an iterator variable. It plays the main role in the for loop. The main idea of this loop is "i" starts with 0 and it increments one by one. When i=5 the for loop is over. In the above for loop,
- int i=0 ---> This is set the iterable variable as 0.
- i < 5 ---> This statement check whether the i<5 or not.
- i++ ----> This statement increases the value of i one by one.
Therefore "i" has 0,1,2,3,4 values. So, this loop iterates 5 times. Then the statements that are inside the for-loop execute 5 times.
Someone can say, "I use the previous method to do this because I can't remember this syntax". In the below video, you can see my answer to it.
While Loop
This loop is a fundamental programming construct used to execute a block of code repeatedly as long as a specified condition remains true. It's a control flow statement that allows you to automate repetitive tasks without the need to duplicate code. Alright, Let's see the syntax and output of that code.
This loop is working under 4 main processes. They are initialization, condition check, execution, and iteration. First, it initialized the count as 0. Then the condition checks whether the count < 5 or not. When the code executing count is increment by 1. As soon as the count value gets 5, the while loop is finished, and run the rest of the code. This is how a while loop works in C++.
Most of the work can be done by both of these loops. But sometimes if we choose for loop, our task can be done easily, on the other hand, sometimes if we choose while loop, our task can be done easily. It depends on our task. Therefore we should choose the suitable one for our task.
Functions
Functions are a fundamental concept in programming. They are blocks of code and they perform a specific task. In essence, functions allow you to break down your program into smaller, manageable pieces. This not only makes your code easier to understand and maintain but also promotes reusability.
Let's see the syntax of a function.
// Function definition
returnType functionName(parameter1Type parameter1, parameter2Type parameter2, ...) {
// Function body
// Code that defines what the function does
}
Don't panic; let's discuss this with an example. In a function, we should only give relative values, and then the function returns the answer. Think I need to implement an adding function. I need two values for it. I choose two integer values for it. When we add two integers the answer should be an integer. Therefore returnType of the function should be an integer. Alright, let's see our implementation and output.
The above add function gets two integer parameters and returns the integer value. However, if we want to work with a function we should call it in our main function. We can do it by initializing it as using a suitable variable in the main method. This function returns an integer value, so I initialize the value as an integer. That's how to implement functions in C++.
Conclusion
In this "Introduction to C++" section, I covered all the C++ fundamental things that you need to know as a beginner. In the first part, we discussed code structure and variables. After that, in the second part, we discussed how to get input, operators, and conditional statements. Finally, in this part, we talked about loops and functions. Now you can develop a simple calculator as an exercise using these fundamental concepts. I recommend you do that exercise to level up your skills. If you like you can send the code of the calculator through WhatsApp(see the Social Plugins Widget). Then I can evaluate your code.
Alright, this is the end of the Introduction to C++ section. Thank you for being with me and Happy Coding!

.png)







