Chapter 6
Download
1 / 19

Chapter 6 - PowerPoint PPT Presentation


  • 95 Views
  • Uploaded on

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.

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

PowerPoint Slideshow about ' Chapter 6' - tave


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

Chapter 6

Loop Structures


Goals and objectives
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 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
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
while loop example

x = 5; // priming the loop

while ( x > 0)

{

x--;

System.out.println(x);

}


Do while loops
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
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
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
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
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
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
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
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
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
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
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
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
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
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


ad