For some programs, some statements have to be ran more than once. While simply using the same few commands several times in a program is one way of handling it, it is not a very good way to handle tasks where we may not know how many times iteration must be done. Furthermore, we have to have control of if a loop needs to be utilized at all. To accomplish effective iteration in programming, loops are utilized to have both control and flexibility over using progresses multiple times to accomplish a task.
The first loop that will be covered are while loops. Essentially, a while loop uses a boolean variable or conditional operator to determine if the loop is to be ran or not. The best case scenario for using a while loop is for a situation where the number of times a loop has to be performed is unknown. Here are two examples of using a while loop:
This first example shows a while loop using less than (<) as a conditional operator to check if the condition is true. If it is true, the loop is ran, incrementing the x variable until it reaches 5 and for each time the loop is ran, it prints out a line to the console that states "I should print five times!" Upon reaching 5, the condition will be false, thus ending the loop and moving on with the program.
Programming Language notes: The method used to output a line to the console is from Java. To output a line in C, "printf("I should print five times!\n");" would be used with \n being used to go to the next line. As for C++, "cout << "I should print five times!\n";" is used. For C# "System.Console.WriteLine("I should print five times!");" would be used.
For the second example, I'm going to show how a boolean variable can be used in a while loop to determine how many times it should be run. Note that I am using an exclamation point to turn whatever the boolean statement is into its inverse: (In other words, it turns true into false, and false into true.)
In this loop, note that while the boolean variable done is false, the while loop will run as the exclamation point will make it true. When the value becomes greater than or equal to five in the if statement I've added, then the done variable will be changed to true, which will result in the condition becoming false.
There is also a variant of the while loop known as the do while loop. This loop executes the series of conditions within the loop at least once before checking the condition. This would be written as seen in the following:
In the example given, x starts at five, which makes the while statement false, but the series of statements within the do while loop will be ran once until the condition is checked. As a result, the variable integer x will be 6.
The second loop that can be used for iteration is the for loop. For most cases, a for loop is usually used when we do know how many times we have to run the same series of statements in order to complete a task. With this in mind, the conditions of the for loop control the creation of a variable to increment in addition to a condition to check to see if the loop should be executed or not. In the following example, I show how a for loop would handle when printing a line.
The third loop that can be used by some programming languages is the for each loop. This loop is best used when showing the contents of an array or a list (Lists will most likely not be covered in the basic programming concepts guide, but may be covered in another series of guides at some point.) A for each loop assigns each part of the array or list to a matching variable, then runs the loop for the length of the array as seen in this example using Java:
Now, when this for each loop is executed, the statement "Hi, my name is Brak!" will be printed on the screen. As I used print and not println, it will not create a new line each time it is executed. Here's a C# example of for each in action as well:
Note: To my knowledge, for each is not in C and the original standards for C++. It does appear that the new C++11 will implement a for each in the same manner as Java, with the only difference being that the variable being declared has an ampersand (&) in front of it.