i++
of our for
loops.
int total = 0; // initialize running sum … while (condition) { // or for (i.e.., some sort of loop) … total += …; // add to the running sum … } …
public class Program_10_1 { public static void main(String [] args) throws Exception { int total = 0; for (int i = 1; i <= 20; i++) total += i; System.out.println("The total is: " + total); } }
import java.util.*; public class Program_10_2 { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(System.in); int total = 0; while (total < 100) { System.out.print("Enter a number: "); int number = scanner.nextInt(); total += number; } System.out.println("total: " + total); } }
1
as we keep track of the item(s)
we are counting.
import java.io.*; import java.util.*; public class Program_10_3 { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("numbers_2.text")); int count = 0; while ( scanner.hasNextInt()) { int number = scanner.nextInt(); count++; // Note we're not doing anything with the number } System.out.println(count + " numbers were read in"); } }
import java.io.*; import java.util.*; public class Program_10_4a { public static void main(String [] args) throws Exception { Scanner keyboard = new Scanner(System.in); Scanner datafile = new Scanner(new File("numbers_4.text")); System.out.print("What number are you looking for? "); int val = keyboard.nextInt(); while (datafile.hasNextInt()) { if (val == datafile.nextInt()) { System.out.print("Found it!"); System.exit(0); } } System.out.print("Not there"); System.exit(1); } }
import java.io.*; import java.util.*; public class Program_10_4b { public static void main(String [] args) throws Exception { Scanner keyboard = new Scanner(System.in); Scanner datafile = new Scanner(new File("numbers_4.text")); System.out.print("What number are you looking for? "); int val = keyboard.nextInt(); boolean found = false; while (datafile.hasNextInt()) if (val == datafile.nextInt()) found = true; if (found) System.out.println("Found it!"); else System.out.println("Not there"); } }
break
logic
import java.io.*; import java.util.*; public class Program_10_4c { public static void main(String [] args) throws Exception { Scanner keyboard = new Scanner(System.in); Scanner datafile = new Scanner(new File("numbers_4.text")); System.out.print("What number are you looking for? "); int val = keyboard.nextInt(); boolean found = false; while (datafile.hasNextInt()) if (val == datafile.nextInt()) { found = true; break; } if (found) System.out.println("Found it!"); else System.out.println("Not there"); } }
import java.io.*; import java.util.*; public class Program_10_4d { public static void main(String [] args) throws Exception { Scanner keyboard = new Scanner(System.in); Scanner datafile = new Scanner(new File("numbers_4.text")); System.out.print("What number are you looking for? "); int val = keyboard.nextInt(); boolean found = false; while (!found && datafile.hasNextInt()) if (val == datafile.nextInt()) found = true; if (found) System.out.println("Found it!"); else System.out.println("Not there"); } }
break
while (datafile.hasNextInt()) {
if (val == datafile.nextInt()) {
System.out.print("Found it!");
System.exit(0);
}
else {
System.out.print("Not there");
System.exit(1);
}
}
import java.io.*; import java.util.*; public class Program_10_5 { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("values.text")); int max = scanner.nextInt(); while (scanner.hasNextInt()) { int num = scanner.nextInt(); if (num > max) max = num; } System.out.println("The maximum number read in is " + max); } }
int max = -1; while (scanner.hasNextInt()) { int num = scanner.nextInt(); if (num > max) max = num; }
break
and continue
break
causes an immediate exit from the loop
continue
cause execution flow to proceed to the next iteration, bypassing all remaining loop body logic
break
) /
unexpected flow of logic from the middle of the loop body back up to the loop header (continue
)