190 likes | 295 Views
More Control Structures. Loops. The while Statment. response = 1; while (response == 1) { System.out.print("Enter 1 or 0:"); response = input.nextInt(); }. The while Statement. Loop structure that repeatedly executes a set of statements as long as a condition is true.
E N D
More Control Structures Loops
The while Statment response = 1; while (response == 1) { System.out.print("Enter 1 or 0:");response = input.nextInt(); }
The while Statement • Loop structure that repeatedly executes a set of statements as long as a condition is true. • Each execution is called an iteration. • The condition is a Boolean expression. • Will never execute if the condition is initially false.
The while Statement & Parentheses • Parentheses (‘{‘ and ‘}’) are used the same way with a while loop as they are with an if statement. • If you have more than one statement to execute when the condition is true those statements must be grouped together using parentheses. (See example on slide 2.)
response = 1; while (response == 1) { System.out.print("Enter 1 or 0:");response = input.nextInt(); } • The loop above iterates initially because the condition is true and then continues to iterate until response is not 1. • What would occur if response was initially set to 0?
The do…while Statement do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } while (response == 1);
The do…while Statement • Alternative form of the while statement • Will always execute at least once do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } while (response == 1); • It continues to execute until the value of response changes to something other than 1.
Infinite Loops • It is possible (easy!) to write infinite loops - loops that will never stop executing. while (num < 0) System.out.print("Enter a value: "); num = input.nextInt(); • Can you explain why the code fragment above is an example of an infinite loop?
You Do: • Pg 133: Review: Prompter
Counters • Counters are an important tool that often get used with loops. • Often used for counting loop iterations. • 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); • Remember our GradeAvg program? How could we have used a counter there?
Accumulators • A variable that is incremented by a varying amount. • (cf. a counter which is always incremented by the same amount.) • We can use them for summing up a value or for “saving it up”. • They should be initialized to zero when declared.
Counters and Accumulators do {System.out.print("Enter grade:");grade = input.nextInt();sumOfGrades += grade; gradeCount += 1; } while (grade != 999); System.out.println(“Average: “ + sumOfGrades/gradeCount); • What could we write instead ofgradeCount += 1?
Flags & Sentinels • We can set and use a flag to signal when our program should stop looping. • Declaring them as constants can make maintaining our programs easier. • final int STOP = 999; do { System.out.print("Enter grade:"); grade = input.nextInt(); sumOfGrades += grade; } while (grade != STOP);
You Do: • P134: Review: NumbersSum • P 134: Review: PercentPassing
The for Statement. • This is another kind of looping control structure. • The key difference is that the for loop always executes a fixed number of times. for (i = 0; i <= 10; i++) {sum += i; } System.out.println(i); • i is known as the loop control variable (LCV) because it controls how the loop iterates.
The for Statement - LCV for (i = 0; i <= 10; i++) {sum += i; } System.out.println(i); • i = 0;means that the starting value of the LCV is 0. • i <= 10;means that the loop keeps iterating (looping) as long as the LCV is less than or equal to 10. • i++ means that the LCV increments (increases) by 1 each time the for loop runs.
The LCV • The LCV can be any variable in your program. It does not have to be called i. • You can actually declare the variable right in the for loop. for (int i = 0; i <= 10; i++) • This is the traditional way to do it in java. • N.B. If so, then the variable only has scope (is known) within the for loop. Referring to it again outside the for loop without having declared it elsewhere is a syntax error.
You Do: • P136: Review: Factorial • If you finish this, then do OddSum on the same page.