For my fellow students/programmers (and tutees):
Since the primary purpose of any system is to reliably recreate the same transformation of its media over and over again, and programs
are systems, we need some means of creating repetition in our programs. This is done with loops.
Regardless of what programming language you're in, there's at least one way to perform a loop. However, this form of logic is so important that modern programming languages have four commonly used looping syntaxes.
The question becomes which one should you use?
To answer that question, I have a little spiel that give to those I'm tutoring.
Basically, the order of priority for looping syntaxes is followed best by answering a list of questions you should ask yourself in the following order (that parallels the priority of looping):
Do you have a collection (or even an array in some languages)? Then use the following loop type:
1) For each [item] in [grouping]
(repeated code block using [item] as a reference to the current item in this iteration)
For example:
Given an employee list is an ArrayList called employeeList and the for loop has access to a class called Employee...
**** For Each in VB .Net ****
For each emp as Employee in employeeList
[repeated code goes here]
[using emp as a reference to the loop's current Employee]
[object in your ArrayList called employeeList]
Next
****
In Java, given a HashMap of employees called employeeMap with a key-value pair of [employeeId and Employee object], your code would look like this:
**** "For Each" in Java ****
Set employeeSet = employeeMap.entrySet();
for (Iterator i = employeeSet.iterator(); i.hasNext();) {
Map.Entry mapEntry = (Map.Entry) i.next();
String empId = (String) mapEntry.getKey();
Employee emp = (Employee) mapEntry.getValue();
[repeated code goes here]
[using empId as a reference to each employeeId]
[String and emp as a reference to each Employee]
[object in your HashMap called employeeMap]
}
****
If you don't have a collection, do you have an array or at least an indexed grouping of variables or objects? Then use a simple for loop:
2) for [index] [initial starting value] [maximum value] [incremented by this value]
(repeated code block using [array] at [index] in this iteration)
For example:
Given an array of grades called letterGrades
****Simple for loop in VB .Net****
For i as Integer = 0 to letterGrades.length
[repeated code using letterGrades(i) as a reference to the current element in the array]
Next
****
****Simple for loop in Java ****
for (int i = 0; i<letterGrades.length;i++) {
[repeated code using letterGrades[i] as a reference to the current element in the array]
}
****
If you don't have a collection or an indexed grouping, do you need a loop that must be performed at least once? Then use a Do Until Loop
3) [Loop start]
[code to be repeated and must be executed at least once goes here]
[Loop end with condition]
For example:
Given a menu must be displayed in a console application at least once to receive input, even if the first option the the user selects is the quit option.
**** Do Until Loop in VB .Net ****
dim quitProgram as boolean = false
Do
[display menu here]
[receive user selection here]
[option selection handling goes here]
[if option to quit was selected]
quitProgram = true
Loop Until quitProgram = true
****
**** Do Until Loop in Java ****
while true {
[display menu here]
[receive user selection here]
[option selection handling goes here]
[if option to quit was selected]
break;
}
****
If all of the above options have been exhausted, it is likely you just need to loop an unknown number of times and the number of times may include 0 (i.e. don't loop at all). So use a simple while loop.
4) [While (condition is true) ]
[code to be repeated unknown number of times]
[end of while loop]
For example:
Let's say you need to read a file and process each line as a record.
**** Simple While Loop in VB .Net ****
At top of code: Imports System.IO
Dim sr As New StreamReader(New FileStream("[filepath]", FileMode.Open))
Dim input As String
Dim record() As String
While sr.Peek <> -1
input = sr.ReadLine
record = input.Split("[delimiters]".ToCharArray)
[code to manipulate each record's elements goes here]
End While
****
**** Simple While Loop in Java ****
At top of code: import java.io.*;
BufferedReader br = new BufferedReader(new FileReader(new File("[filepath]")));
String input;
String[] record;
while (br.ready()) {
input = br.readLine();
record = input.split("[delimiters");
[code to manipulate each record's elements goes here]
}
****
Next time: Declarations of variables.