Loops: These are used when you want to repeat a certain set of commands over and over again. Such as in a video game, when you want to constantly receive input from the user, or in an application that asks for the same input multiple times. There are several different types of loops, depending on the language you are using, but in this lab we will only consider two. In order to use both loop types, a good knowledge of Boolean expressions is required.
While Loop: This is the loop you use when you do not know how many times you will be looping. You know when to stop looping, but don't know how many iterations that will take. For example, you wish to continually get input from the user until they enter the word "Quit". Here you know when the loop will end, but not how many times the user will type in other input.
The structure of a while loop is:
while( expr )
{
// code
}
The expr is the spot where you would place a Boolean expression. An example of this is given below:
String a = "";
while(!a.equals("Quit"))
{
// code
} Recall that when using objects, such as a string, we need to use the equals method. It is very important that you not forget that the variable a must be updated inside the body of the loop. If it is not then a will never become "Quit" and you will have an infinite loop that will go on forever.
While loops can also be used as counters or to quit after a certain number of loops. An example is provided below:
int i = 0;
while (i != 8)
{
i++;
} Here, note that i is being updated in the body of loop. If it was not then it would be an infinite loop. In cases such as this though it is often better to use a for loop.
For Loop: The for loop is used when you know exactly how long you wish to loop for. You know how many iterations are needed. It therefore has a counter built into the loop structure for you. The structure of the for loop is provided below:
for (declarations; expr; incrementer)
{
// code
}
As can be seen from the structure, the for loop allows a lot of flexibility. The first part of the loop is the declaration. Here you can declare variables that will be used in the loop (they only exist in the loop and once the loop is over they can no longer be accessed). The expr is your Boolean expression again and tells the loop when to stop. The incrementer is the area that you update whatever your Boolean expression depends on. An example of a loop that loops seven times is shown below:
for (int i = 0; i < 7; i++)
{ System.out.println(i);
} // incrementer This code will output:
0Notice how it started outputting at 0 and did not output 7. In the future we will be dealing with something called arrays and this ability will be preferential. The incrementer part is only called at the end of the loop.
Finally, as stated above, any variables declared in the loop are only accessible inside the loop. Therefore doing something like:
for (int i = 0; i < 7; i++)
{ System.out.println(i);
} // incrementer
System.out.println(i); will result in error due to the last System.out.println(i) statement not knowing what i is. If you wanted to do this you would have to go:
int i;
for (i = 0 ; i < 7; i++)
{ System.out.println(i);
} // incrementer
System.out.println(i); This way the i variable is declared outside of the loop. The loop can use the variable and it is usable after the loop terminates.