Guest Posted May 12, 2017 Share Posted May 12, 2017 The tutorial I am following says that: "Post-test loops will execute at least ONE TIME - This is useful for sanitizing input" I have no idea what this means. Help please! Quote Link to comment Share on other sites More sharing options...
Explv Posted May 12, 2017 Share Posted May 12, 2017 (edited) 7 minutes ago, Noidlox said: The tutorial I am following says that: "Post-test loops will execute at least ONE TIME - This is useful for sanitizing input" I have no idea what this means. Help please! An example of a post-test loop would be a do while loop. For example: do { // some stuff } while(some condition is true); // check this after The contents of this loop will execute at least once, because the condition is checked after the loop completes NOT before. This is different from a while loop where the condition is checked first: while (some condition is true) { // Check this first // some stuff } An example use of this is sanitizing input because you may want to get some input from the user THEN check it, and only exit the loop if some condition is matched. Consider this trivial example: Scanner scanner = new Scanner(System.in); String input; do { input = scanner.nextLine(); // Read a line of input } while (!input.equals("Hello")); // Only exit the loop if the input matches "Hello" Edited May 12, 2017 by Explv 4 Quote Link to comment Share on other sites More sharing options...
Guest Posted May 12, 2017 Share Posted May 12, 2017 4 minutes ago, Explv said: An example of a post-test loop would be a do while loop. For example: do { // some stuff } while(some condition is true); // check this after The contents of this loop will execute at least once, because the condition is checked after the loop completes NOT before. This is different from a while loop where the condition is checked first: while (some condition is true) { // Check this first // some stuff } An example use of this is sanitizing input because you may want to get some input from the user THEN check it, and only exit the loop if some condition is matched. Consider this trivial example: Scanner scanner = new Scanner(System.in); String input; do { input = scanner.nextLine(); // Read a line of input } while (!input.equals("Hello")); // Only exit the loop if the input matches "Hello" Makes sense. Thank you! Quote Link to comment Share on other sites More sharing options...