What are loops and when are they used?


I have a general idea of how loops are used, but i'm not really grasping the fundamental truth of what they are and when they would like to be used outside of using them to incrementally increase a value of something like a variable.

any help/examples, etc would be appreciated. thanks
Lets say you need multiple entries from the User.


for(i=0, i>10, i++)
  // output statement for user input
  // Input from user
  // Maybe create an array for the input values, and put them in the array?
  // Really your choice...

There are many reasons to use a loop. I suggest that you Google code examples for different types of loops.

Loop types: for, while, and do...while

Look them up for starters.

ok thanks, i'll try and find some google code examples.

i sort of see how they're used, but i'd like to really know what a 'for' loop is used for.

if i understand, a loop is a block of code that is run multiple times, often adding something or taking away something.

i didn't know that while blocks were loops.
For loops are mostly used for when you know specifically how many times you want the code within it to run. While and Do-While loops will only run if a certain condition is met, with the exception of the Do-While loop running once before checking if that condition is met or not.
For loops are only useful because of the way you use them, you could replicate them with a while loop so they are not required. The idea is that a for loop will happen a number of times that is known in advance and a while loop will happen a number of times that is not known in advance, but that's just good practice - you can deviate if you want, but then people will be confused reading your code.
Topic archived. No new replies allowed.