+ 1
When to use for loop in a code?
Answer in a simple way.......
4 ответов
+ 4
Use it when you know how many times you need to repeat the loop.
other loop code are more general, for is quite useful when you need to put values in a array, because you know how many times you're going to repeat the process
+ 1
*Summary*
The prime example is a loop that requires a counter. Giving you the means to include a counter in the loop definition is what makes the for loop differ from the other kinds of loops (do-while and while loop).
*Clarification*
This specialty doesn't mean you can't define a counter with the other loops or you can define a for loop that exactly behaves like a while or do-while loop.
Using the for loop makes the cases where you need a counter easier to read as it's introduced and widely used exactly for this purpose.
*Examples*
Counters and therefore for loops are often required, if you need to make a piece of code run a specific number of times or use the counter to access consecutive pieces memory, or as an input to a mathematical function.
Even though the answer is not short, I hope it's easy to understand the finer points in the differences.
+ 1
when we want to repeat a statement according to requirement
0
when u know how many times you need to take the value from the user.like in a 3×3matrix we need to repeat the loop 9 times and take the value from user 9 times