Loop Structures and Strings Chapter 6 Review The while/do-while Statement • The while statement and do-while statement are loop structures that iterate a set of statements repeatedly based on a condition. • The difference between the loops is when the condition is evaluated. • The while statement evaluates the condition before any iterations are performed. • The do-while does not evaluate the condition until after the first iteration. while/do-while syntax while (<condition>) { <statement>; } do { <statement>; } while (<condition>); while examples int num = 0; while (num < 5) { num += 1>; } do-while examples do { System.out.print("Enter a # less than 4:"); playerNum = input.nextInt(); } while (playerNum >= 4); Infinite Loops • Some syntax errors and logic errors can lead to an infinite loop, which executes forever causing the application to just hang. • A logic error can also cause an overflow, which occurs when there are not enough bits to store a number. Infinite Loop example int num = -1; while (num < 0) { num = -1>; } Counters & Accumulators • Counters are used for keeping track of loop iterations and are used in applications that keep track of the number of guesses or the number of values entered. • An accumulator is increased by varying amounts. • Accumulators are often used to sum values. • Both counters and accumulators should be initialized when they are declared. flag, sentinel • A flag, also called a sentinel, is used to signify that a loop should stop iterating. • Flags are usually a constant that is declared in the application,but may also be determined by prompting the user for a value. The for Statement • The for statement is a loop structure that executes a set of statements a fixed # of times. • A loop control variable is used to determine loop iterations and can be declared in the for statement itself. for (<initialization>; <condition>; increment>) { <statements>; } The for Statement example for (int i=1; i <=10; i++) { System.out.println(i); } • i is the loop control variable • i<10 is the condition • i++ is the increment statement scope/programming style • Note that the counter is declared in the initialization of the for statement (int i = 1). • With a declaration in this location, the scope of the counter is from the initialization to the closing curly brace of the for statement. • The application will not recognize the variable outside of that statement. • Declaring variables so that their scope is limited to where they are needed is good programming style because it produces cleaner code and helps eliminate the possibility of errors. Debugging Techniques • Debugging is the process of getting an application to work correctly. • Debugging techniques include using a debugger, often included with a compiler, and a variable trace, which is a manual technique. • Debuggers have the advantage of being able to display the actual value of a variable as it changes. • Other techniques include adding println statements before and after variable assignment. • Commenting out statements can also locate an error through process of elimination. The String Class • The String class is used to declare string variables. • The string class is included in the java.lang package • It contains methods for determining the length of a string, converting a string to lower/uppercase, extracting substrings, & comparing. • A string is immutable, which means it cannot be changed from its original value. However, a String object can be assigned a new string in memory. • The characters of a string have an index value, with the first character at index 0. Useful String Class Methods Method Description length() returns an integer corresponding to the number of characters in the string. substring(int start, int end) returns a substring of the string, which starts at start position and ends one character before the end position. substring(int start) returns a substring of the string, which starts at start position and extends to the end of the string. toLowerCase() returns a copy of the string with all lowercase letters. toUpperCase() returns a copy of the string with all uppercase letters. trim() returns a copy of the string with all leading and trailing spaces removed. Useful String Class Methods Cont’d Method Description replaceFirst(String str, String str2) returns a string with the first occurrence of str replaced by str2. replaceAll(String str, String str2) returns a string with all occurrences of str replaced by str2. Comparing Strings • Strings are compared when determining equality or alphabetical order. • Some of the String class methods for comparing strings include: Method Description equals(String str) returns true when the string is the same as str. Returns false otherwise. equalsIgnoreCase(String str) same as equals() except that uppercase and lowercase differences between the strings are ignored. Comparing Strings Cont’d Method Description compareTo(String str) returns 0 when str is the same as the string, a negative integer is returned when str comes alphabetically after the string, and a positive integer is returned when str comes alphabetically before the string. Note that uppercase and lowercase letters are considered different. compareToIgnoreCase( String str) same as compareTo() except that uppercase and lowercase differences between the strings are ignored. indexOf(String str) returns the integer corresponding to the location of the first occurrence of str in the string. Otherwise –1 is returned. lastIndexOf(String str) returns the integer corresponding to the location of the last occurrence of str in the string. Otherwise –1 is returned. startsWith(String str) returns true when the string begins with str. Returns false otherwise. endsWith(String str) returns true when the string ends with str. Returns false otherwise.