So you don't know how a loop works?
Well there are for loops, while loops, do loops, and some other misc. methods to loop, like goto, but which are too less practiced to worth mentioning.
Basically, everything inside a loop repeats a couple of times, until some condition is met.
The for loop:
for(int i=0;i<10;i++){...};
- You declare a variable i and you set it to 0.
- You set the loop condition to i<10. This way, the statements inside the loop block will be repeated until i is no longer smaller than 10.
- You set the progression to i++ (or i+=1). This way, every time the loop ends, it increases 1 to variable i. This way, after 10 times, if the value of i is not modified from within the loop, the execution will end and the program will continue executing further statements.
The while loop:
1 2 3 4 5
|
int i=0;
while(i<10){
i++;
...
};
|
- Pretty much the same thing, except that this time you have more flexibility in declaring the loop condition and you have to update things yourself.
The do loop:
1 2 3 4 5
|
int i=0;
do{
i++;
...
}while(i<10);
|
- It's very similar to the while loop, except that this time the statements inside the loop will be executed at least once, in the case that the condition isn't met, unlike the other two, in which the loop will be skipped in a case like this:
for(int i=0;i<0;i++){...};
Got it?
Later edit: seems like the first time I've read this details weren't given...