190 likes | 309 Views
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.
E N D
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. • Employ techniques such as using a debugger, adding extra output statements, or hand-tracing code.
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
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(); }
while loop example x = 5; // priming the loop while ( x > 0) { x--; System.out.println(x); }
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.
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.
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
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);
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);
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);
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.
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; }
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
Variable Trace int num1 = 0;int num2 = 0;while (num1 < 10) { if (num1 % 3 == 0) { num2 += num1;System.out.print(num2 + “ "); } num1 += 1;}
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;}
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;}
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.
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