When to use for loop in a code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When to use for loop in a code?

Answer in a simple way.......

15th Jun 2016, 3:35 PM
jwjajajsn
4 Answers
+ 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
15th Jun 2016, 4:16 PM
Evelyn Coronel
Evelyn Coronel - avatar
+ 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.
15th Jun 2016, 4:26 PM
Stefan
Stefan - avatar
+ 1
when we want to repeat a statement according to requirement
16th Jun 2016, 11:25 AM
DJ Shiva & DJ Baba Shiv-Shekhar
DJ Shiva & DJ Baba Shiv-Shekhar - avatar
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
18th Jun 2016, 7:08 AM
Vivek Sharma
Vivek Sharma - avatar