1 / 12

COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu. Used to implement looping. Two key elements test condition body of loop. Test Condition. false. true. While loop. Execute Statements (body of loop).

jaxon
Download Presentation

COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

  2. Used to implement looping • Two key elements • test condition • body of loop Test Condition false true While loop Execute Statements (body of loop) while (condition) { Statement1; Statement2; ::: } // end while body of loop As long as the condition is true, you execute all statements in the body of the loop. When condition is false, you exit the loop. Key Point: Some statement(s) in the body of the loop must eventually make the condition false.

  3. Example int K = 0; while (K < 4) { System.out.print(K + ““); K++; } output: 0 1 2 3

  4. More Examples int i = 0; int sum = 0; while (i < 4) { sum = sum + i; i++; } System.out.println(sum);

  5. More Examples int i = 2; int p = 1; int total = 1; while (p < 4) { total = i * total; p++; } System.out.println(total);

  6. More Examples i = 0; sum = 0; while (sum < 10) { i++; sum = sum + i; Print i and sum; } i = 0; sum = 0; while (sum < 10) { i++; sum = sum - i; Print i and sum; } i = 0; sum = 0; while (sum >= 10) { i++; sum = sum + i; Print i and sum; } i = 0; sum = 0; while (sum < 0) { i++; sum = sum + i; Print i and sum; }

  7. Hand-Tracing Loops int n = 1729; int sum = 0; while (n > 0) { int digit = n % 10; sum = sum + digit; n = n / 10; }

  8. Common Error 1: Infinite Loop Forgetting to update the variable that eventually makes the loop condition false int year = 1; while (year <= 20) { double interest = balance * rate /100.0; balance = balance + interest; } // end while

  9. Common Error 1: Infinite Loop Forgetting to update the variable that eventually makes the loop condition false int year = 1; while (year > 0) { double interest = balance * rate /100.0; balance = balance + interest; year++; } // end while

  10. Common Error 2: Off-by-One Error or Boundary Condition Error int year = 0; while (balance < TARGET) { year++; double interest = balance * rate /100.0; } // end while System.out.println(“the interest doubled after ” + year + “years.”); • Questions: • Should year start at 0 or 1? • Should you test for (balance <TARGET ) or (balance <= TARGET)

  11. Exercise Write a Java program that prompts the user for a positive integers value and outputs the numbers (1 2 3 4 …) up to the number that is inputted. Scanner in = new Scanner(System.in); System.out.println(“Enter in a positive number: “); int lastNumber = in.nextInt(); int cntr = 1; System.out.println(““); while (cntr <= lastNumber) { System.out.print(cntr + ““); cntr++; }

  12. Write Loops that computes • the sum of all integers between 2 and 100 (inclusive) • the sum of all EVEN integers between 2 and 100 (exclusive) • the sum of all SQUARES of integers between -300 and +300 (inclusive) • the average of all ODD integers between 1 and 100 (inclusive)

More Related