Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.

Similar presentations


Presentation on theme: "© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The."— Presentation transcript:

1 © 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The condition is a Boolean expression  Will never execute if the condition is initially false  The loop below iterates once because the condition is true and then continues to iterate until response is not 1: response = 1; while (response == 1) { System.out.print("Enter 1 or 0:"); response = input.nextInt(); }

2 © 2007 Lawrenceville Press Slide 2 Chapter 6 The do-while Statement  Alternative form of the while statement  Executes at least once  The statement do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } while (response == 1); iterates once and continues to iterate until response is not 1.

3 © 2007 Lawrenceville Press Slide 3 Chapter 6 Infinite Loops  A loop that continues executing forever  Can be caused by syntax or logic errors. For example: while (num < 0)//error--no braces System.out.print("Enter a vale: "); num = input.nextInt();  Some errors can result in an overflow

4 © 2007 Lawrenceville Press Slide 4 Chapter 6 Counters  A variable that is incremented by a constant value  Often used for counting loop iterations  Should be initialized to 0 when declared  The counter in the loop counts the number of responses: do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); numResponses += 1; } while (response == 1);

5 © 2007 Lawrenceville Press Slide 5 Chapter 6 Accumulators  A variable that is incremented by a varying amount  Often used for summing  Should be initialized to 0 when declared  The accumulator in the loop sums values: do { System.out.print("Enter grade:"); grade = input.nextInt(); sumOfGrades += grade; } while (grade != 999);

6 © 2007 Lawrenceville Press Slide 6 Chapter 6 Using Flags  A flag, or sentinel, indicates when a loop should stop iterating  Often a constant  Code is easier to modify when sentinels are constants declared at the beginning of an application  A flag is used in the condition of the loop: final int STOP = 999; do { System.out.print("Enter grade:"); grade = input.nextInt(); sumOfGrades += grade; } while (grade != STOP);

7 © 2007 Lawrenceville Press Slide 7 Chapter 6 The for Statement  Loop structure that executes a set of statements a fixed number of times  Uses a loop control variable (lcv)  The increment ( ++ ) or decrement ( -- ) operators are used to change the value of the loop control variable  The loop below executes until i is greater than 10: for (int i = 0; i <= 10; i++) { sum += i; }

8 © 2007 Lawrenceville Press Slide 8 Chapter 6 Debugging Techniques  The debugger included with many compilers  Variable trace, which is a manual technique of list values of variables at the points of assignment  Additional println() statements for displaying variable values at points of assignment  "Commenting out" code to detect bugs through a process of elimination

9 © 2007 Lawrenceville Press Slide 9 Chapter 6 Variable Trace int num1 = 0; int num2 = 0; while (num1 < 10) { if (num1 % 3 == 0) { num2 += num1; System.out.print(num2 + " "); } num1 += 1; }

10 © 2007 Lawrenceville Press Slide 10 Chapter 6 Using println() to Debug int num1 = 0; int num2 = 0; System.out.println("num1 before while: " + num1);//debug while (num1 < 10) { System.out.println("num1 in while: " + num1);//debug if (num1 % 3 == 0) { num2 += num1; System.out.println("num2: " + num2);//debug System.out.print(num2 + " "); } num1 += 1; }

11 © 2007 Lawrenceville Press Slide 11 Chapter 6 Using Comments to Debug int num1 = 0; int num2 = 0; while (num1 < 10) { //if (num1 % 3 == 0) { //num2 += num1; //System.out.print(num2 + " "); //} num1 += 1; }

12 © 2007 Lawrenceville Press Slide 12 Chapter 6 The String Class  Part of the java.lang package  A String object is comprised of a sequence of characters with the first character at index position 0  String methods include: length() isEmpty() substring() toLowerCase() toUpperCase() trim() replaceFirst() replaceAll()

13 © 2007 Lawrenceville Press Slide 13 Chapter 6 String Data and the Scanner Class The next() method should be used for reading string data after numeric data has been read. If the nextLine() method is used, the end-of-line character left by the numeric entry is read and any text typed is ignored. System.out.print("Enter age: "); age = input.nextInt(); System.out.print("Enter first name: "); firstName = input.next();

14 © 2007 Lawrenceville Press Slide 14 Chapter 6 String Data and the Scanner Class (con't) Alternatively, a statement can be added to read the end-of-line character left by the numeric entry, so that the nextLine() method can be used to read a string that may contain white space: System.out.print("Enter age: "); age = input.nextInt(); input.nextLine();//remove end-of-line System.out.print("Enter full name: "); fullName = input.nextLine();

15 © 2007 Lawrenceville Press Slide 15 Chapter 6 String Assignment Strings are immutable. Assigning a new string to a String object simply changes the object reference to point to the new string in memory: String text; text = "heLlO"; text = text.toLowerCase(); text heLlO hello

16 © 2007 Lawrenceville Press Slide 16 Chapter 6 Comparing Strings The String class includes several methods for comparing two strings: equals() equalsIgnoreCase() compareTo() compareToIgnoreCase() indexOf() lastIndexOf() startsWith() endsWith()


Download ppt "© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The."

Similar presentations


Ads by Google