CISC 1115
Introduction to Programming Using Java
Lecture #10
Techniques II


With the availability of general loop, many additional, commonly-used techniques can be introduced:

Accumulation

Keeping Count

Searching for a Value

Various Searching Techniques
The two most commonly used techniques:

The Classic Newbie Searching Error

while (datafile.hasNextInt()) {
	if (val == datafile.nextInt()) {
		System.out.print("Found it!");
		System.exit(0);
	}
	else {
		System.out.print("Not there");
		System.exit(1);
	}
}

Finding the Maximum/Minimum of a Sequence of Values

The Classic (Not-So-) Newbie Max-of-a-Squence Error

int max = -1;
while (scanner.hasNextInt()) {
	int num = scanner.nextInt();
	if (num > max) max = num;
}   

break and continue

Somewhat controversial as they allow for exit from the loop other than from the header (break) / unexpected flow of logic from the middle of the loop body back up to the loop header (continue)

Files used in this Lecture