For Loops - How they are Critical for Programmers

28 Feb 2019

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.

Figure 1A: The abstract syntax conditions for traditional and enhanced for loops.

Figure 1B

TRADITIONAL FOR LOOPS

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:

As a result, myVariableName is always true since it holds the value of zero, and zero is accurately less than one hundred.

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 LOOPS

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).