Loops are used in programming when we want to repeat a similar task many times. Instead of just writing out each line of code we can use a loop to repeat code for a set number of times or until a condition is met. There are three types of loops that we will be discussing here: for loop, while loop and do loop.
An example of something that you may want to do in programming that would be repetitive task is counting from 1 to 10. From what you learned in the past chapters you may try to count like this:
int count; count = 0; count = count + 1; count = count + 1; count = count + 1; count = count + 1; count = count + 1; count = count + 1; count = count + 1; count = count + 1; count = count + 1; count = count + 1;
Clearly this task is very repetitive and with loops we can write it out in a much better way. Let’s see how to do the simple example above using each type of loop that we have available so far starting with the for loop.
For Loops
The for loop is used when we know how many times that we want to repeat a task. So, our counting problem from above is a good example of a task that a for loop could help with since we know that we simply want to add one to the count integer variable 10 times. Here is what the for loop will look like for our counting problem.
int count = 0; for(int i=0;i<10;i++){ count = count + 1; }
Letâs see how to count from one to 10 using a for loop. The for loop starts with the for keyword. Next is the three bits of code in parenthesis that control how many times code in the loop will execute. The first thing in this part of the for loop is the integer variable i. This is what is used to control the loop. This variable is initially set to 0 (int i=0;). Next the ending condition is defined (i<10;). Finally, each time the loop executes the variable i is incremented by 1 (i++).
What this all means is that when the code reaches the loop a temporary variable called i will be created with an initial value of zero. When we use a variable in a for loop like i we refer to it as the control variable because it controls the loop. Each time the code in the loop (that will appear after the for statement) executes the value of i will be increased by 1. The loop will keep on going until i is no longer less than 10.
Now that we have our loop we need to include the code that will be executed each line the loop cycles. This is sometimes called iterating through the loop. The first thing we did was to define a region of code with curly braces .
The next part is the code that we want to repeat 10 times. So to use the example that we started with we could replace some of those lines of code with count = count +1;.
The for loop is very powerful and you could probably do most of your repetitious programming with just this loop. But, there are two other ways to do loops in programming that may also use.
While Loops
While loops operate in the much the same way as for loops. The only real difference other than the use of the while keyword is that placement of the control variable and the operations associated with the control variable. Here is how we would count from one to ten using a while loop.
int i = 0; while (i < 10){ count = count + 1; i++; }
To start coding a while loop we will need to first declare and set an initial value for an integer variable. This will serve as our control variable. Next we will use the while keyword to specify the while loop. We must also include the ending condition in parenthesis here.
Next we use curly braces {} to define our region of code.
When using while loops be careful to make sure to keep incrementing the control variable. In our example, we are using i to keep track of how many times we have gone through the loop. Each time the while loop executes, it checks on the value of i to see if it is still less than 10. If it is, then the loop will iterate one more time. If not, then the loop stops executing and the program moves on. If you forget to increment the control variable each time then your app may get stuck repeating this loop forever.
The last part of the while loop here is the same as what we have for the for loop. It is the code we want to repeat which is count = count + 1;.
As you can see this loop works just like a for loop but the syntax is a little bit different. Both the for loop and the while loop could have a situation where the code in the loop never executes. This happens when the control variable was already equal to 10 (and not zero like we demonstrated above). If that were to happen then these loops would not execute any code at all. Try this variation of the example above yourself to see what I mean:
int i = 10; while (i < 10){ count = count + 1; i++; }
If you want to make sure that the code in your loop is guaranteed to execute at least once you must use a do loop.
Do Loop
The do loop is distinguished from both the for loop and the while loop in that a do loop will execute its code at least once even if the condition is already met. Here is what a do loop looks like.
int i = 0; do{ count = count + 1; i++; }while (i < 10);
To start to code a do loop you start by declaring the control variable and setting its initial value. The next step is to simply type in the do keyword and the curly braces we will need.
In contrast to the previous two loops we will put the ending condition at the end of this loop. We need to be careful to increment the control variable here as well.
Now that we have the loop all set up we can again stick in our counting code to complete the example. Finally, we can add the code that we want to repeat.
The way that this do loop is set up right now is equivalent to the other two loops that we used to count with. However, there is a subtle difference here that will only be seen when the control variable would seem to make the loop not execute at all. In our example this would be when i = 10.
int i = 10; do{ count = count + 1; i++; }while (i < 10);
The above code would execute the counting code once time. This is because the expression that controls whether the loop executes does not get evaluated until the end so the code gets at least one chance to execute.
Hands On Time
Code the factorial of 5 using a loop. In math, a factorial is the product of all positive numbers equal to or less than the number. This is noted with an exclamation point after the number. So, the factorial 5 would be noted as 5!. To figure out what the factorial of 5 is you can simple multiple all the numbers that are equal to or less than the number like this: 5! = 5 * 4 * 3 * 2 * 1 = 120. So, the factorial of 5 is 120.
Write code to solve the factorial of any number. Use loops to do this and make sure to write code three times using each loop.