Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 The while Statement

Similar presentations


Presentation on theme: "Chapter 6 The while Statement"— Presentation transcript:

1 Chapter 6 The while Statement
11/13/2018 7:10 PM 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(); } Refer to page 131 in the text. © 2007 Lawrenceville Press

2 Chapter 6 The do-while Statement
11/13/2018 7:10 PM 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. Refer to pages 131 and 132 in the text. © 2007 Lawrenceville Press

3 Chapter 6 Infinite Loops
11/13/2018 7:10 PM 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 Refer to page 132 in the text. An infinite loop can cause an application to stop responding or just "hang". Closing the application window (with JCreator Pro) will end the application so that the source of the infinite loop can be located and corrected. Errors that result in an overflow may generate unexpected results rather than "hanging" the application. © 2007 Lawrenceville Press

4 A variable that is incremented by a constant value
11/13/2018 7:10 PM 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); Refer to page 133 in the text. © 2007 Lawrenceville Press

5 A variable that is incremented by a varying amount
11/13/2018 7:10 PM 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); Refer to page 133 in the text. © 2007 Lawrenceville Press

6 A flag, or sentinel, indicates when a loop should stop iterating
11/13/2018 7:10 PM 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); Refer to page 133 in the text. © 2007 Lawrenceville Press

7 Chapter 6 The for Statement
11/13/2018 7:10 PM 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; } Refer to page 135 in the text. © 2007 Lawrenceville Press

8 Chapter 6 Debugging Techniques
11/13/2018 7:10 PM 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 Refer to pages 136 and 137 in the text. © 2007 Lawrenceville Press

9 Chapter 6 Variable Trace
11/13/2018 7:10 PM 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; } Refer to page 136 in the text. © 2007 Lawrenceville Press

10 Chapter 6 Using println() to Debug
11/13/2018 7:10 PM 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; } Refer to page 137 in the text. When using println() statements to debug a segment of code, it is usually most effective to place statements just after assignment changes the value of a variable. © 2007 Lawrenceville Press

11 Chapter 6 Using Comments to Debug
11/13/2018 7:10 PM 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; } Refer to page 137 in the text. Comments can be used to comment out segments of code to determine through a process of elimination the location of any bugs. © 2007 Lawrenceville Press

12 Chapter 6 The String Class
11/13/2018 7:10 PM 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() Refer to page 138 in the text. The String class is used for representing text. The first character of a string is at index position 0. The last character is at position length()-1. © 2007 Lawrenceville Press

13 Chapter 6 String Data and the Scanner Class
11/13/2018 7:10 PM 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(); Refer to page 138 in the text. You may also want to refer students back to page 82. The nextInt() and nextDouble() methods read in data up to an end-of-line character. The nextLine() method reads data up to and including the end-of-line character. Because of this, calling a nextLine() method after a nextInt() or nextDouble() reads the end-of-line left by those methods and the string entry just made is lost. © 2007 Lawrenceville Press

14 Chapter 6 String Data and the Scanner Class (con't)
11/13/2018 7:10 PM 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(); Refer to page 138 in the text. You may also want to refer students back to page 82. Note that an input.nextLine() statement can be added to simply remove the end-of-line left by the numeric entry. © 2007 Lawrenceville Press

15 Chapter 6 String Assignment
11/13/2018 7:10 PM 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 Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains three (3) animations. Refer to page 139 in the text. The object instantiation, String text, associates the object name text with a memory location <press space bar> The first assignment statement points to the location in memory that stores the mixed-case string <press space bar> The second assignment statement, moves the pointer to the location in memory that stores the lowercase string <press space bar> © 2007 Lawrenceville Press

16 Chapter 6 Comparing Strings
11/13/2018 7:10 PM Chapter 6 Comparing Strings The String class includes several methods for comparing two strings: equals() equalsIgnoreCase() compareTo() compareToIgnoreCase() indexOf() lastIndexOf() startsWith() endsWith() Refer to page 140 in the text. © 2007 Lawrenceville Press


Download ppt "Chapter 6 The while Statement"

Similar presentations


Ads by Google