120 likes | 213 Views
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).
E N D
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) 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.
Example int K = 0; while (K < 4) { System.out.print(K + ““); K++; } output: 0 1 2 3
More Examples int i = 0; int sum = 0; while (i < 4) { sum = sum + i; i++; } System.out.println(sum);
More Examples int i = 2; int p = 1; int total = 1; while (p < 4) { total = i * total; p++; } System.out.println(total);
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; }
Hand-Tracing Loops int n = 1729; int sum = 0; while (n > 0) { int digit = n % 10; sum = sum + digit; n = n / 10; }
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
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
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)
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++; }
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)