Hello. I have problems with loops . I can’t understand how they are working. I know what are doing, but I cannot use them
Making your question more specific with more details helps you getting better answer.
Basically, loops do similar tasks (the loop body) many times, within subtle difference (rooted in the loop variable) from one round to another, and then stop (the stop condition).
Example:
unsigned int sum = 0;
for (size_t i = 0; i != 100; ++i) {
sum += i + 1;
}
Here, the loop body represent the codes inside the block ({ ... }) and the codes in the increment part of for loop (++i), the stop condition is !(i != 100), and the loop variable is i. For every round, the loop body does similar task (adding i + 1 to sum), and the loop will stop when the stop condition becomes true. When you look into a round and compare to another, the only subtle difference is caused by the different value of i, which is the loop variable.
Another concept might help you to understand how loop works: the invariant condition. An invariant condition is a statement associated with a loop, and is always true at (1) the begining of the loop, (2) the end of the loop body of every round, and (3) the end of the loop. The correctness of invariant conditions of a loop helps you to be confidence of the correctness of the loop itself.
In the example, the invariant condition statement could be: "i is the number of how many numbers have been added to sum". You could easily check the correctness at the three timings of the loop, and thus the loop do the task for you: starting from i == 0, add i + 1 to sum for 100 times, with ++i as the increment after the addition every time.