Introduction: 4.1.0

  • A loop (iteration/repetition) is a way to repeat one or more statements
  • control structures can be used to construct any algorithm
    • if and loops
  • while loop executes a loop as long as a boolean condition is true
    • the loop exits once the condition is false
    • like an if statement that runs more than once
  • body uses curly brackets if it has more than one statement
    • same as if
    • curly brackets are always acceptable; it’s good practice to use them
boolean myExpr = false && true;

// just run once
if (myExpr) {
  doSomething();
}

// run every time myExpr evaluates to true
while (myExpr) {
  doSomething();
}

3 Steps to writing a loop: 4.1.1

  • Simplest loops are counter controlled loops
    • the loop control variable is a counter which counts a number of repeats
    • 3 steps to writing a loop using a control variable
  1. initialize the loop variable
  2. test loop variable
  3. change loop variable
int count = 0;  // step 1

while (count <= 10) {  // step 2
  doSomething(count);
  count++;  // step 3
}
  • Java doesn’t require code to be correctly indented to seperate parts of a loop
    • It is standard practice
    • AP graders will use indentation if you forget to open/close brackets

Tracing loops: 4.1.2

  • You can use a tracing table to track the values of variables through a loop
int i = 0;
int j = 0;

while (i <= 5) {
  j = i * i;
  i++;
}

| iteration | i | j | | ——— | — | — | | 0 | 0 | 0 | | 1 | 1 | 1 | | 2 | 2 | 4 | | 3 | 3 | 9 | | 4 | 4 | 16 | | 5 | 5 | 25 |

Errors with loops: 4.1.3

Infinite loops

  • An infinite loop is one which never stops
    • the condition always evaluates to true
  • These can happen if you forget to change the value of the loop variable
int i = 0;

while (i <= 5) {
  System.out.println("This is a loop iteration");
}

the value of i never increases, so this is an infinite loop

Off-by-one error

  • loop runs one too many or too few times
    • usually a problem with the incorrect relational operator
      • using < instead of <=

Input-controlled loops: 4.1.4

  • while loop is usually used when you don’t know how many iterations will happen
    • often an input-controlled loop where user input is used to know when to stop
  • In the magpie chatbot the loop stops when you type “Bye”
    • Stopping value is called the sentinel value
    • A loop can also stop using a return statement
      • Exits the method
      • you can also use break which just exits the loop
  • there are standard algorithms which use loops
    • sum, min/max, average, frequency
    • identify which numbers are evenly divisible
    • identify individual digits of an integer

Summary: 4.1.6

  • iteration statements change the flow of control by repeating a set of statements until a condition is met
  • loop control variables can be used in a boolean condition to loop a certain number of times
  • 3 steps to writing a loop
    • initialize loop variable
    • test loop variable
    • change loop variable
  • the boolean expression is evaluated before every iteration of the loop body
    • the body is executed if the loop is true
    • when the expression evaluates false, the loop exits
  • an infinite loop is one in which the boolean expression always evaluates true, so the loop never exits
  • off by one errors occur when a loop iterates one too many/too few
  • input-controlled loops are controlled by user input
    • use sentinel values as the condition to stop the loop
      • an input like “bye”
  • there are many standard algorithms for sums/averages/etc.