1 / 19

# Chapter 6 - PowerPoint PPT Presentation

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Chapter 6' - tave

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Chapter 6

Loop Structures

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

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

x = 5; // priming the loop

while ( x > 0)

{

x--;

System.out.println(x);

}

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

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

• 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

• 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);

• A variable that is incremented by a varying amount

• Often used for summing

• Should be initialized to 0 when declared

• 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);

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

• 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; }

• 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

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

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

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

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