1 / 19

Chapter 6

Chapter 6. Loop Structures. Goals and Objectives. Identify loop structures. Implement an algorithm using iteration. Explain how infinite loops can occur. Differentiate between counters and accumulators.

tave
Download Presentation

Chapter 6

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. Chapter 6 Loop Structures

  2. Goals and Objectives • Identify loop structures. • Implement an algorithm using iteration. • Explain how infinite loops can occur. • Differentiate between counters and accumulators. • Employ techniques such as using a debugger, adding extra output statements, or hand-tracing code.

  3. while Statements • while statement – is a loop structure. • Iteration – each time a loop repeats is call an iteration. • 3 parts of a loop • Priming the loop • Termination condition • Increment/decrement • Pre-Test loop – test the Boolean expression first before executing the loop. • Variable loop – loops varies on how many times it executes

  4. 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(); }

  5. while loop example x = 5; // priming the loop while ( x > 0) { x--; System.out.println(x); }

  6. do-while loops • Post test loop – the Boolean expression is evaluated at the end of the loop. • Variable loop – loop don’t always execute the same amount of time each time it runs.

  7. The do-while Statement • Alternative form of the while statement • Executes at least once • The statementdo { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } while (response == 1);iterates once and continues to iterate until response is not 1.

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

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

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

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

  12. for loop statement • Pre-test loop – the Boolean expression is evaluated at the beginning of the loop. • Fixed loop – loop always execute the same amount of time each time it runs.

  13. 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; }

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

  15. Variable Trace int num1 = 0;int num2 = 0;while (num1 < 10) { if (num1 % 3 == 0) { num2 += num1;System.out.print(num2 + “ "); } num1 += 1;}

  16. Using println() to Debug int num1 = 0;int num2 = 0;System.out.println("num1 before while: " + num1); //debugwhile (num1 < 10) {System.out.println("num1 in while: " + num1); //debug if (num1 % 3 == 0) { num2 += num1;System.out.println("num2: " + num2); //debugSystem.out.print(num2 + " "); } num1 += 1;}

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

  18. Pre & Post Increment/Decrement • variableName++; //Post increment • ++variableName; //Pre increment • variableName--; //Post decrement • --variableName; //Pre decrement • The difference between pre and post is when the variable gets incremented or decremented.

  19. Example Pre & Post x = 0; while(x<5) System.out.println(x++); • Output  0,1,2,3,4 • Example 2 x = 0; while(x < 5) System.out.println(++x) • Output  1,2,3,4,5

More Related