If a big university, such as the University of Minnesota, wants to send a personalized email to the whole list of its students, alumni, or staff, it would not be efficient to copy, paste, and edit each email out since it would take an extremely long time to finish such simple tasks. As one should expect, programmers are always trying to solve problems to make every piece of software and application work quickly and efficiently. This is why programmers always use for loops to complete almost all straightforward and repetitive tasks. With the for loops, the University of Minnesota can send email greetings to any personalized names, classes, or groups more easily.
Many popular programming languages, including, but not limited to, Ruby, Java, JavaScript, Python, C, C++ support the use of for loops; they are absolutely critical and useful to programmers who want to test and run a specific task multiple times. They can be created at any moment in a text editor when a programmer wants to write, compile1, and then run the file
There are two types of for loops: traditional for and enhanced for; each has its own specific use. In the case of a specific problem, one would be preferred over the other. The two figures shown below display both traditional and enhanced for loops, which iterate2 through what a 2 programmer has set before running the code. The figures are based on the Java programming language. Line numbers are shown on the left side, and the code is shown on the right.
Lines 1-3 on Figure 1A shows an abstract of what a typical, traditional for loop should look like. Inside the parenthesis on line 1, there are three slots that need to be filled in, and they are called statements. These three statements are needed to create a for loop that works properly: initialize, terminate, and update the condition. Each statement is separated by a semicolon to help programmers differentiate the purpose between one and another to prevent confusion:
Initialize: The starting step which gets executed only once at the beginning of the loop. It
must be declared and initialized to a specific primitive type3 and a value of either a number,
3 word, or a list of both, are then assigned to any variable name, let’s call it myVariableName
and
set it equal to 0:
int myVariableName = 0
Terminate: Every time the loop repeats itself, this statement gets to check if the condition is correct (true) or not (false): is 5 less than 10? This is rightfully true. When the condition is true, the code continues to run until it reaches the end of the loop, and repeats this process all over again. However, if the condition is false, the loop stops completely. One must check by using simple math comparisons to see if either the condition is true or false by using >=, >, ==, <, or <= operators:
myVariableName < 100
As a result, myVariableName
is always true since it holds the value of zero, and zero is
accurately less than one hundred.
Update: One must increment or decrement the variable from the initialize
statement for
the next iteration, such as adding or subtracting one. Each iteration may get closer to the
terminate
statement (known as ending point), but some do not and programmers must avoid this
at all cost. In this case, the variable name, myVariableName
, increases from the number 0 up to
the ending point, which is less than 100. This variable gets closer to an ending point every time
the loop repeats itself.
To increment each loop by one, use:
myVariableName++
Otherwise, to increment each loop by ten, use:
myVariableName += 10
myVariableName
eventually gets repeated 10 times: 10, 20, 30, 40, 50, 60, 70, 80, 90,
and 100. Notice, the last number, 100, stops the loop entirely by the terminate
statement since
100 is not less than 100.
Line 2 in Figure 1B shows an example of printing “I will never sleep in WRIT3562 again!”
exactly one hundred times. The terminate
statement gets repeated 101 times because the
updating
statement is incremented by one in each iteration, starting from zero; the loop then gets
terminated at the beginning of the 101st iteration.
Enhanced for loop runs similarly to the traditional for loops. However, enhanced for loops go through an entire list of items and have two statements; each statement is separated by a colon and is shown in lines 6-8 on both Figure 1A and 1B. To start, one must declare and initialize a primitive variable list outside the loop, such as:
int[] myIntegers = {4, 5, 2, 3}
// or
String[] myFruits = {"Apple", "Orange", "Mango", "Pear"}
Doing so, programmers can begin to create an enhanced for loop. The first statement must only
be declared as a primitive type and a variable name, for instance, String fruit
. The second
statement requires the list variable name that has been created outside of the loop, myFruits
.
The loop then repeats itself through all the fruits inside myFruits
’s list. As one should expect,
the result gets printed out every single fruit from the myFruits
’s list:
The programming and human-language syntax are very alike, we can say “For each of my fruits in the fruit basket, I’d like to eat it.” Likewise, we can do other things with the fruit inside the loop function and in real life: bake, cook, chop, boil, etc.
Similarly, as sending emails to students, alumni, and staff, programmers can use both the traditional and the enhanced for loop to complete a repetitive task such as sending out personalized greeting emails. An easier option is to use an enhanced for loop since it goes through everyone in the entire email list individually. The traditional for loop can also accomplish this task but one must know the size of the list ahead in order to terminate the loop. A straightforward way to remember which for loop to use is that traditional for loops will loop through a large quantity of numbers whereas the enhanced for loop goes through every single element in the entire list.
1 Compile: most programming languages need to convert the programmed code into a machine code for computers to understand in order to run properly.
2 Iterate: a process that goes through the programmer’s instructions repeatedly until a condition is met.
3 Primitive type: a type of data that must be defined and declared before each variable; there are many category types in our world, such as animals, shoes, schools, etc… Similarly, primitive types in programming has its own categories: byte, short, int (an integer), long (a larger integer), float (a number with decimals), double (a larger number with decimals), char (a character), String (a string of characters), and boolean (true or false).