Topic :Introduction To Loops in C++ Programming Language:



Loops in C++ Programming Language:

Loops are used to perform a set of instructions repeatedly. Loops allow us to perform statements repeatedly and just as importantly, to stop the action when warranted. Frequently, it's decision-making that makes computer programs seem smart, but looping makes program powerful. by using loops, we can write one set of instruction that executes thousands or even millions of times.

The repetitive capabilities of computers make them good tools for processing large amounts of information. there may be a situation, when we need to execute a block of code several numbers of times. in our daily life, most of the things are repeated. Days and nights repeat themselves 30 times a months. Four seasons replace each other every year. we can see similar phenomenon in the practical life.

For Example: in the payroll system, some procedures are same for all the employees. These are repeatedly applied while dealing with the employees. So repetition is very useful structure in the programming.



loop concept

For Example:

Let's suppose we have to calculate the sum of first 10 whole numbers i.e. add the numbers from 1 to 10. Following statement may be helpful to do it.


int x = 1+2+3+4+5+6+7+8+9+10;
cout<<"The value of first 10 digits is: "<<x;


This method is perfectly fine as the syntax is right. The answer is also correct. But what's about if we need to calculate first 1000 integers (1-1000 digits).. can we use the same statement that we had used for 1-10 digits. yes, we can, but it's not suitable for computing the sum of these huge numbers. the addition of a very big number of digits will result in a very ugly and boring statement. but programming came in our life to make our life easier, not to bore us. well is there any way to which we can add huge numbers easily. yes, there is. C++ Provide us loop, by using loops we can do this task within just a few steps: let's analyze it carefully.

As our first integer is 1, is there any other way to find out what's the next integer? Yes, we can add 1 to the integer and get the next integer which is 2. To find the next integer (i.e. 3) we add 1 to the previous integer (i.e. 2) and get the next integer which is 3. so whenever we have to find out the next integer, we have to add 1 to the previous integer and so on. its loop, because the same thing happens again and again. so we don't need to do this task again and again. our computer can do this by itself using loops.

C++ programming language provides the following type of loops to handle looping requirements.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement