Loops in programming language C and C++
Often, programming requires one command or multiple commands to repeat a number of times. Let's imagine now that we should print out certain commands 100, 1000, or 10000 times. It would be a difficult, almost impossible job. The job would be made easier if these commands, which we want to repeat, were written only once, and with another command repeated.
These commands, for which we allow controlled repetition, are called cycles. In C, the C++ language is used for, while, and the dowhile command.
For example. Imagine that we want to print a star 20 times.
We can do this with a command cout that needs to be repeated 20 times, so:
These commands, for which we allow controlled repetition, are called cycles. In C, the C++ language is used for, while, and the dowhile command.
For example. Imagine that we want to print a star 20 times.
We can do this with a command cout that needs to be repeated 20 times, so:
cout << "*" << endl; cout << "*" << endl; cout << "*" << endl; . . . 
Image by Dirk Wouters from Pixabay

Instead, we'll use the for. The syntax would look something like this:
for(int i=0; i<20; i++)
cout<< "*" << endl;
The terms in a small bracket serve to provide the appropriate number of repetitions, in this case 20.
The first term serves to introduce some variable to which some initial value is assigned and whose value will change during the cycle.
The second term is actually the condition of the loop from which it depends whether it goes to the next cycle or not. This is actually a logical expression whose value is a logical data type of bool, so it can have a value true, if the value of the expression is correct, or false, if it is incorrect. In case the value is true, the next cycle will be executed.
For example, if we introduced the variable "i" and set its initial value to zero in the first expression, and the second expression is <20, it means that the printing will be repeated until the expression is correct. If the variable "i" did not change during the cycle, it would mean that this condition will always be satisfied, which means that it will be infinite cycles (the program is continuously executed).
A third term is required to change the variable. It defines how much the variable that is introduced in the first expression is changed. In our case, i ++ means a change for 1 and an increase.
So, if i is starting at i = 0, increment step 1, and loop condition <20, this means that this condition will be satisfied until the value of the variable "i" reaches a value of 20. So 20 <20 is no longer true and the cycle is interrupted .
The phrase three could be written as i = i + 1. This means that the memory labeled with and assigns a value that was previously in that memory increased by 1.
In general, the syntax for commands would be
for loop in c and c++, syntax
for(izraz_1; izraz_2; izraz_3)
{
{
COMMANDS
}Here we see that the commands are covered with curly braces. In the case of only one command brackets can be absent.
Example 1: Print a sequence of numbers from 100 to 100 that are divisible by 3
To print 1 number, we will use the command printf for the C programming language, or cout if it is a c++ language. We will use a for loop to repeat the statement. We will mark the variable that we introduce inside the for to achieve the required number of cycles with a and use it to display the values of the numbers in the sequence.
for(int a=100; a<100; a++)
{
{
printf("%2d",broj);
}This would print all numbers between  100 and +100. In order for the program to print only those numbers that are divisible by 3, we will do the following.
We will move the initial value of the number a to the first next number that is divisible by 3, i.e. at 99
The step of changing the variable a should be set to 3, because every third number, starting from 99, is also divisible by 3, so a=a+3
So the solution to the task now looks like:
We will move the initial value of the number a to the first next number that is divisible by 3, i.e. at 99
The step of changing the variable a should be set to 3, because every third number, starting from 99, is also divisible by 3, so a=a+3
So the solution to the task now looks like:
for(int a=99; a<100; a=a+3)
{
{
printf("%2d ",x);
}Similarly in the C++ programming language the code would be:
for(int a=99; a<100; а=а+3)
{
{
cout<<x<<" ";
}After starting, the screen will show:
99,96,93, ....0,3,6,........99
99,96,93, ....0,3,6,........99
The "for" loop is often used to process data arrays. Read more about strings on the site:
The arrays in C and C++
The arrays in C and C++
How to get the sum of a series of natural numbers from 1 to 10
And this task could be solved with the command:
int sum=1+2+3+4+5+6+7+8+9+10;
printf( "sum = %d",sum);
printf( "sum = %d",sum);
Or in C++ programming language:
int sum = 1+2+3+4+5+6+7+8+9+10;
cout<< "sum = %d" << sum << endl;
cout<< "sum = %d" << sum << endl;
But we want to show how to get the sum using the for cycle (suitable when there are a lot of numbers). The idea is to add one natural number in each cycle to a variable that represents the sum. Those natural numbers in the cycle would be marked with the variable "i" as in the previous example (it is convenient because that variable changes from 1 to 10 during the cycle with step 1, so that through the cycles it actually represents those natural numbers that need to be added.
First, it is necessary to introduce a variable that will represent the sum:
int sum=0;
Then one number at a time should be added to the sum. Written without the cycle it would look like:
sum = sum + 1;
sum = sum + 2;
sum = sum + 3;
sum = sum + 4;
...
sum = sum + i;
...
sum = sum + 10;
but instead we use a loop:
First, it is necessary to introduce a variable that will represent the sum:
int sum=0;
Then one number at a time should be added to the sum. Written without the cycle it would look like:
sum = sum + 1;
sum = sum + 2;
sum = sum + 3;
sum = sum + 4;
...
sum = sum + i;
...
sum = sum + 10;
but instead we use a loop:
for(int i = 1; i<=10; i= i+1)
{
{
sum= sum+ i;
}The final total will be formed only after the completion of the for cycle. A complete example is shown in the image below:
C
int sum = 0; //sum must have been initialized with 0 for(int i = 1; i<=10; i= i+1) {
sum = sum + i;
}printf("sum = %d",sum );//Print the sum 
C++
int sum = 0; for(int i = 1; i<=10; i= i+1) {
sum = sum + i;
}cout << "sum = " << sum << endl; //Print the sum 
Determination of the average
Example: Enter 5 integers and find the mean value
First you need to enter those numbers. In order to determine the average value (arithmetic mean), it is first necessary to add up those values, and the previous example can be used for that. Then the resulting sum is divided by the number of those numbers, which in the previous example is 5.
In the general case, we can enter the number of numbers as some n and enter or set that value first. The average must be a variable of real type, double or float, because dividing two numbers, whether they are integer or real, can get a real number. Also, in order to get the exact value of the average, and not an integer, at least one of the divided numbers (sum or n) must be set or programmatically converted to a real type. For example. if the sum were 24 and n=5, dividing would give 24/5= 4.0, if the numbers 24 and 5 remained integer.
If, instead of 24, the value was real, such as 24.0, then as a result of division, 24.0/5= 24.0/5.0=4.8 would be obtained.
The solution to the previous example would be:
In the general case, we can enter the number of numbers as some n and enter or set that value first. The average must be a variable of real type, double or float, because dividing two numbers, whether they are integer or real, can get a real number. Also, in order to get the exact value of the average, and not an integer, at least one of the divided numbers (sum or n) must be set or programmatically converted to a real type. For example. if the sum were 24 and n=5, dividing would give 24/5= 4.0, if the numbers 24 and 5 remained integer.
If, instead of 24, the value was real, such as 24.0, then as a result of division, 24.0/5= 24.0/5.0=4.8 would be obtained.
The solution to the previous example would be:
After starting, a console window of the application will be displayed in which numbers should be entered. An example of the execution of the application can be seen in picture number 2:
In the example shown, for the entered numbers:
4, 5, 5, 5, 5,
an average of 4.8 is obtained, which represents the correct value. In the code in line 16, it should be noted that the data "sum", which is declared as an int data, is casted. Casting turns that data into a double, the number n will automatically be converted into a double, so that by dividing, a result of type double is obtained, and therefore the correct value is obtained. Otherwise, only the integer part would be obtained as a result, i.e. 4.0.
4, 5, 5, 5, 5,
an average of 4.8 is obtained, which represents the correct value. In the code in line 16, it should be noted that the data "sum", which is declared as an int data, is casted. Casting turns that data into a double, the number n will automatically be converted into a double, so that by dividing, a result of type double is obtained, and therefore the correct value is obtained. Otherwise, only the integer part would be obtained as a result, i.e. 4.0.
Introduction to cyclessimulation of uniform motion
Let's look at the following problem:
We want to simulate the change of position with [m] with uniform motion for time change. Hence, visual simulation will not be made, but only the printing of the current position of the body position for each time change for the dt interval.
We will observe the position changes for each small increase of time from dt [s]
Let's take this change for dt = 0.05s
We want to simulate the change of position with [m] with uniform motion for time change. Hence, visual simulation will not be made, but only the printing of the current position of the body position for each time change for the dt interval.
We will observe the position changes for each small increase of time from dt [s]
Let's take this change for dt = 0.05s
Let the time change during 1s 20 changes by 0.05s = 1s
The starting values are s = 0; t = 0; in the user input
So 20 times would repeat the following commands:
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route. See more about the cout command in the lesson strings in C/C++
This code is not good:
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route.
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route.
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route.
...
We see that 3 commands are repeated 20 times. Instead, you need to write 3 commands one time and then use some other command that will cyclically repeat them as many times as we want.
These are the commands we call cycles (loops):
The starting values are s = 0; t = 0; in the user input
So 20 times would repeat the following commands:
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route. See more about the cout command in the lesson strings in C/C++
This code is not good:
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route.
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route.
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout<< “s=“<<s<<endl; //Print the value of the traveled route.
...
We see that 3 commands are repeated 20 times. Instead, you need to write 3 commands one time and then use some other command that will cyclically repeat them as many times as we want.
These are the commands we call cycles (loops):
 for
 while
 dowhile
The loops (cycles) in JAVA have a very similar syntax for writing commands. Read more in the lesson: Loops in JAVA
for loop
Now, the previous simulation would be solved in the following way:
for(int i=0; i<20; i++)
{
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout << “s=“ << s << endl; //Print the value of the traveled route.
}
{
t=t+dt; //time change for 0.05s
s=s+v*dt; //change position for 0.05s
cout << “s=“ << s << endl; //Print the value of the traveled route.
}
for the cycle is used when we know the number of cycles in advance.
If we do not know the advance number of the cycle?
Then the number of cycles depends on some condition and then we use it
while the command or,
dowhile
If we do not know the advance number of the cycle?
Then the number of cycles depends on some condition and then we use it
while the command or,
dowhile
while command
This command, unlike for the command, is used when we do not know the advance number of the cycle.
Note that in a small bracket, we only have one logical type or logical variable type bool. Commands in the body while the commands will be repeated as long as the term is correct, ie, while the value is true. Since the condition is at the beginning of the loop, it is examined before executing the command. It may happen that in the first test the value of the bool expression is false, which means that in this case, the commands would not be executed at any time.
If, for any reason, it is important for us to execute orders at least one time, then it is more convenient for the condition to be in the end, which is the case with the doiton order.
Note that in a small bracket, we only have one logical type or logical variable type bool. Commands in the body while the commands will be repeated as long as the term is correct, ie, while the value is true. Since the condition is at the beginning of the loop, it is examined before executing the command. It may happen that in the first test the value of the bool expression is false, which means that in this case, the commands would not be executed at any time.
If, for any reason, it is important for us to execute orders at least one time, then it is more convenient for the condition to be in the end, which is the case with the doiton order.
while, syntax
Repeat commands while the bracketing condition is satisfied
while( true )
{
while( true )
{
COMANDS
}While algorithm
Figure 3 shows the while loop execution algorithm. The statements in the loop will be repeated as long as the set condition is true.
Example: Enter the integer N. Remove the zero from the right. For example. for entering N = 12000, the input should be 12
This task is suitable for the application of the cycle. Zero removal will be done by dividing by 10 times as long as the transformation number is divisible by 10. We do not know how many times this will be the path because it is not known in advance for entering the number. So it is not suitable for, but while the cycle where the execution will cyclically repeat until the delimitation condition is satisfied, the remainder of division N and number 10 is zero:
int N;
cin >> N; //Loading the whole number
while(N % 10 == 0)
{
cout << "N=" << N << endl;
cin >> N; //Loading the whole number
while(N % 10 == 0)
{
N=N/10; //The new value of the number N is divided by 10
}cout << "N=" << N << endl;
Example: Free Fall Simulation
Text: Enter the initial body height and make a free fall simulation by printing time, instantaneous speed and current height on each 0.05s
We see various body positions after each dt = 0.05s.
During this time, the height h and the velocity v are changed.
During this time, the height h and the velocity v are changed.
 Time changes for dt: t = t + dt;
 Height for v * dt + g * dt * dt / 2. Hence h = h (v * dt + g * dt * dt / 2)
 Speed for g * dt. Thus, v = v + g * dt
In the previous example: Simulation of free fall is preferable to use while the loop. In each cycle there is a change in time, current body height and current body speed. The cycle is repeated until the height is greater than zero, i.e. until the body falls to the ground.
double h;
cin >> h;
while(h>=0)
{
cin >> h;
while(h>=0)
{
t=t+dt; //time change for 0.05s
h=hv*dtg*dt*dt/2; //change position during 0.05s
v=v+g*dt; //change speed at 0.05s
cout << "h=" << h << "m" << endl;
}h=hv*dtg*dt*dt/2; //change position during 0.05s
v=v+g*dt; //change speed at 0.05s
cout << "h=" << h << "m" << endl;
dowhile loops
We use instead of a cycle when commands have to be done at least once, and then, if the condition is satisfied, the commands are repeated, as long as the condition of the loop is satisfied, i.e. has the value true.
The previous example could be done with dowhile if you knew the initial conditions before we entered the cycle, e.g.
if we know it is safe h0> 0 and
Initial conditions:
h = h0; t = 0;
then it would look like:
The previous example could be done with dowhile if you knew the initial conditions before we entered the cycle, e.g.
if we know it is safe h0> 0 and
Initial conditions:
h = h0; t = 0;
then it would look like:
Repeat commands while the bracketing condition is satisfied
do
{
while(h>=0);
do
{
t=t+dt; //time change for 0.05s
h=hv*dtg*dt*dt/2; //change position during 0.05s
v=v+g*dt; //change speed at 0.05s
cout << "h=" << h << "m" << endl;
}h=hv*dtg*dt*dt/2; //change position during 0.05s
v=v+g*dt; //change speed at 0.05s
cout << "h=" << h << "m" << endl;
while(h>=0);
If the initial h was zero, then this method does not make sense because an iteration that does not have to be done is done, since the body is already on the ground.
If we know it is safe h0> 0
Then the condition is set in the end, so we use the dowhile command.
If we know it is safe h0> 0
Then the condition is set in the end, so we use the dowhile command.
do  while , syntaks
Repeat commands while the bracketing condition is satisfied
do
{
while( true );
do
{
COMMANDS
}while( true );
The dowhile loop algorithm
Figure 5 shows the dowhile loop execution algorithm. The statements in the loop will be repeated as long as the set condition is true. Unlike the while loop, where it may happen that the condition encountered immediately at the beginning of the statement is not true even the first time, so the statement will not be executed even once, with the dowhile loop the condition is set at the end, so commands must be executed at least once.
Next
Nested loops in C/C++ > 
Related articles
Loops in C/C++ examples
Loops in programming languages JAVA
Arrays  examples
Array of Fibonacci
Data in C/C++ languages
Loops in programming languages JAVA
Arrays  examples
Array of Fibonacci
Data in C/C++ languages