Repe$$on CSC 121 Fall 2016 Howard Rosenthal

Lesson Goals —  Learn the following three repetition methods, their

similarities and differences, and how to avoid common errors when using them: —  while —  do-while —  for

—  Learn nested looping —  Learn how to use break statements to exit a loop —  Begin to learn how to deal with situations where

faulty or out of range input is supplied

2

Introduc$on to Loops —  Many of the tasks performed by computers require repetition, often massive

repetition

—  Using repetitive syntax can make your code more concise and easier to write

and debug

—  Java has three types of repetitive constructs (also known as loops): while, do-

while or for —  Loops are built out of several fundamental statements because there are three things (in each of the three types of loops) that must be done correctly: —  The loop must be initialized correctly. —  The ending condition must be tested correctly. —  The body of the loop or the loop statement must change the condition that is

tested.

—  Overlooking one of these aspects results in a defective loop and a sometimes

difficult to find program bug! Usually each of these three considerations is located in a different spot. No wonder that loops often go wrong! —  You have to plan out the logic of your program in order to effectively use loops and conditionals

3

Types of Loops

type

description

counting loop

Uses a loop control variable to count upwards or downwards (usually by an integer increment.)

sentinel-controlled loop

Loop keeps going until a special value is encountered in the data.

result-controlled loop

Loop keeps going until a test determines that the desired result has been achieved.

4

The while Statement Here is a program with a loop. It contains a while statement, followed by a block of code. Remember, a block is a group of statements enclosed in braces. // Example of a while loop public class LoopExample { public static void main (String[] args ) { // start count out at one int count = 1; while ( count = 0 ) // count is tested { System.out.println( "count is:" + count ); count = count - 1; // count is changed by -1 } System.out.println( "Done counting down." ); } } 8

The do-while Statement Here is a program with a loop. It contains a do statement, followed by a block of code, followed by a while statement. // Example of a do-while loop that executes only once public class DoWhileExample { public static void main (String[] args ) { int count = 1000; // initialize do { System.out.println( count ); count++ ; } while ( count < 10 ); // test System.out.println( "Done with the loop" ); } } 9

The Basic Syntax of the do-while Statement do { statement1; statement2; o 0 0 statementn; } while (condition);

10

Basic Terminology of the do-while Statement —  The condition is a Boolean expression: something that

evaluates to true or false. —  The condition can be complicated, using many relational operators and logical operators. —  A single statement is allowed —  Without braces to create a block only one statement is

executed inside the loop

—  The block or single executable statement is sometimes

called the loop body. —  The do-while is always executed at least once, if the do statement is executed —  There is a semi-colon after the while statement in a dowhile

11

The for Statement Here is a program with a loop. It contains a for statement, followed by a block of code. A block is a group of statements enclosed in braces. // Example of a for loop public class ForExample { public static void main (String[] args ) { int sum=0; for ( int count = 0; count