For Loops
Note: The example code I use below is Java. However, the only part that is Java is the commands inside the for loop. The actual format of the for loop can be expanded to many other languages where it is the same, such as C#, C++, C, Perl, and PHP.
For loops are used to perform a task a set amount of times. Commonly, this number of times is based off the number of elements in an array, or just anything you need to do over and over for multiple pieces of data.
The syntax for a for loop is:
Code: Select all
for (int counter = 1; counter <= 10; counter++) {
Console.WriteLine(counter);
}
Code: Select all
int counter = 1;
counter <= 10;
counter++;
For example:
Code: Select all
int counter;
for (counter = 1; counter <= 10; counter++) {
Console.WriteLine(counter);
}
Console.WriteLine(“Exited the for loop. counter = “ + counter);
The second part of the for loop is the conditional statement. In our example, it is “counter <= 10;” which basically tells the loop to repeat itself as long as counter is less than or equal to ten. Because we start at 1 (we know this because the first part of the loop sets counter to 1), this loop will first go through where counter is equal to 1, and then reach the end of the loop and see that it is indeed less than or equal to 10, and go through again.
The last part of the for loop is what prevents the loop from running forever, which is commonly called an infinite loop. In our example, the last part is “counter++”. The ++ on the end of a variable is shorthand for “counter = counter + 1”. This statement is run every time the end of the loop is reached.
So now that you know how a for loop functions, lets go through an example run of the above loop, just to make sure everything clicks in place.
Code: Select all
for (int counter = 1; counter <= 10; counter++) {
Console.WriteLine(counter);
}
Console.WriteLine(“Out of for loop!”);
Now, the program looks at the second of the three pieces of code in the for loop to check if it evaluates to true. Because counter (with a value of 1) is less than or equal to 10, the program enters the actual content of the for loop.
The line Console.WriteLine(counter) simply writes the value of counter to the screen. Right now, it prints out a 1.
Now, the program continues to the next line, which is the ending brace, }. This tells the program to head back to the beginning of the for loop and execute the last of the three parts of the for loop. counter++ increases the value of counter by 1, which means it now has a value of 2.
The program is now back at the start of the for loop, so it checks the middle statement again. 2 is less than or equal to 10, so it enters the loop again, where Console.WriteLine(counter) prints 2.
The second (conditional) part of the for loop (counter <= 10) always evaluates when the program is at the beginning of the for loop. The last piece of code in the for loop (counter++) always evaluates at the end of the for loop, before returning back to the top to check the conditional statement.
This process continues until Console.WriteLine(counter) prints out a 10. Afterwards, the program sees the ending brace again ()) and increments counter by 1, making it equal to 11.
Now, the program goes back to the top of the for loop and checks the conditional statement again. Because 11 is not less than or equal to 10, the program does not go back into the for loop again. Instead, it skips all the code until it gets to the ending brace } and continues to the rest of the program from there.
At this time, the program outputs, “Out of for loop!”
------
For loops in the most common languages:
C# / C++ / Java
Code: Select all
for (int i = 0; i < 10; i++) {
// Do stuff
}
Code: Select all
for (my $i = 0; $i < 10; $i++) {
// Do stuff
}
Code: Select all
for ($i = 0; $i < 10; $i++) {
// Do stuff
}
Code: Select all
for i in 0..10
# Do stuff
end
Code: Select all
for x in range (0, 10)
# Do stuff