<<

Lesson 9 Part 2: Loops

A loop is a control structure that causes a statement or group of statements to repeat.

 We will discuss three (possibly four) looping control structures. They differ in how they control the repetition.

The

 General Form: while (BooleanExpression) Statement or Block  First, the BooleanExpression is tested  If it is true, the Statement or Block is executed  After the Statement or Block is done executing, the BooleanExpression is tested again  If it is still true, the Statement or Block is executed again o This continues until the test of the BooleanExpression results in false.  Note: the programming style rules that apply to decision statements also apply.

while Loop Example public class WhileLoop { public static void main(String[] args) { int number = 1; //Must have a starting value for While Loop

while (number <= 5) { System.out.println("Hello!"); number++; } }

} Flowchart for the above loop

 Here, number is called a loop control variable.  A loop control variable determines how many times a loop repeats.  Each repetition of a loop is called an iteration.  The while loop is known as a pretest loop, because it tests the boolean expression before it executes the statements in its body.  Note: This implies that if the boolean expression is not initially true, the body is never executed.

Infinite Loops

 In all but rare cases, loops must contain a way to terminate within themselves.  In the previous example number was incremented so that eventually number <= 5 would be false.  If a loop does not have a way of terminating it’s iteration, it is said to be an , because it will iterate indefinitely.  This is a bad logic error!  If we removed number++ from the previous example, it would be an infinite loop.  Can also be created by putting a semicolon after the loop header or not using brackets properly.

Infinite Loop Example: public class WhileLoop { public static void main(String[] args) { int number = 1;

while (number <= 5);// semicolon causes infinite loop { System.out.println("Hello!"); number++; } }

} Example 2 public class WhileLoop { public static void main(String[] args) { int number = 1;

while (number <= 5) { System.out.println("Hello!"); // Number never reaches 5 //causing infinite loop

} }

}

The do-while Loop

while loops are considered pretest, but Java also provides a posttest loop called the do-while loop: do Statement or Block while (BooleanExpression);  Here, the Statement or Block is executed first  Next, the BooleanExpression is tested  If true, the Statement or Block is executed  Then the BooleanExpression is tested  This continues until the BooleanExpression is false.  Again, this is a posttest loop, meaning the BooleanExpression is tested at the end.  Note that this means the Statement or Block will ALWAYS be executed at least once.  Also not the semicolon at the end of the last line.

In a , the statements are excuted 1 time before the Boolean expression has been checked.

Example Do-While Loop public class DoWhileLoop { public static void main(String[] args) { int number = 1;

do { System.out.println("Hello!"); number++; } while(number <= 5); }

}

/*Always make sure that your Boolean Expression will become False sometime during the execution of your program.*/