1 / 19

More Control Structures

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.

zonta
Download Presentation

More Control Structures

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. More Control Structures Loops

  2. The while Statment response = 1; while (response == 1) { System.out.print("Enter 1 or 0:");response = input.nextInt(); }

  3. 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.

  4. 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.)

  5. 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?

  6. The do…while Statement do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } while (response == 1);

  7. 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.

  8. Danger, Will Robinson! Danger!

  9. 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?

  10. You Do: • Pg 133: Review: Prompter

  11. 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?

  12. 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.

  13. 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?

  14. 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);

  15. You Do: • P134: Review: NumbersSum • P 134: Review: PercentPassing

  16. 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.

  17. 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.

  18. 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.

  19. You Do: • P136: Review: Factorial • If you finish this, then do OddSum on the same page.

More Related