Anda di halaman 1dari 24

Looping

while loop

Objectives

• Describe the advantages of looping

• Draw the flowchart for a loop

• Define:

– loop control variable

– loop body

– loop condition

– accumulators

– counters

• Be able to write a loop construct with the Java while statement

Looping

• If making decisions is what makes computers seem intelligent, it’s looping that makes computers worthwhile.

• The beauty of having a computer is that a series of instructions that will be performed over and over again need to be written only once.

performed over and over again need to be written only once . The series of instructions

The series of instructions

be repeated

using a loop .

can

Loop Flowchart

Loop Flowchart • Initialize variable to control loop • Compare variable to a value that stops

• Initialize variable to control loop

• Compare variable to a value that stops the loop

• Alter the variable so it can again be compared for stopping the loop

Example:

Example: • Parts of a loop – Loop control variable - determines whether the loop should

Parts of a loop

Loop control variable -

determines whether the loop should be continued (REP)

Sentinel value - value to compare to stop loop (5)

Loop body - statements inside the loop

public static void main(String args[])

{

int rep = 1;

while (rep < 5)

{

System.out.println(“Warning”); rep = rep + 1;

}

}

The while Loop

• Java provides three different looping structures.

• The while loop has the form:

while(condition)

{

statements;

}

• While the condition is true, the statements will execute repeatedly.

• The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop.

Syntax of while()

while (condition)

{

 

loop body;

}

No semicolon after condition in the while

while condition surrounded by ( )

while NOT While

braces surround loop body

Block Statements in Loops

• Curly braces are required to enclose block statement while loops. (like block if statements)

while (condition)

{

 

statement;

statement;

statement;

}

The while Loop

• Care must be taken to set the condition to false somewhere in the loop so the loop will end.

• Loops that do not end are called infinite loops.

• A while loop executes 0 or more times. If the condition is false, the loop will not execute.

Infinite Loops

• In order for a while loop to end, the condition must become false. The following loop will not end:

int x = 20;

while(x > 0)

{

System.out.println("x is greater than 0");

}

• The variable x never gets decremented so it will always be greater than 0.

• Adding the x-- above fixes the problem.

Infinite Loops

• This version of the loop decrements x during each iteration:

int x = 20;

while(x > 0)

{

System.out.println("x is greater than 0"); x--;

}

Syntax of Structured while Loop

initialize loop control variable; while (condition)

{

loop body; update loop control variable;

}

NOTE: the loop control variable should be updated as the last statement in loop body

Loop Flowchart Example

Loop Flowchart Example

Questions

• On the previous slide identify:

– What this program does

– Loop control variable

• initialization

• update

• testing

– Sentinel value

– Condition

– Loop body

Loop Control Variables

• Initialize – before entering the loop (for input items – prime or lead read)

• Test – condition of the loop

• Update – usually the last statement of the loop body (for input items – end read)

• What happens if the loop control variable is never updated?

Infinite or Endless

loop!

Problems:

• Draw the flowchart to display the numbers 1-10.

• Write the corresponding Java Code

• Identify the:

– loop control variable

• where it is initialized

• where it is updated

– body of the loop

– condition

Java Program Display 1-10

Java Program Display 1-10

Problems

• Draw the flowchart to display even numbers only between 1 and 100. At the end display the sum of the even numbers and an average of the even numbers.

• Write the corresponding Java Code

• Identify the

– loop control variable

• where it is initialized

• where it is updated

– body of the loop

– condition

– accumulator

– counters

• Where do you calculate the average -- inside or outside the loop? Why?

Java Code

Java Code

Accumulators/Counters

Accumulators An accumulator is a numeric variable used for accumulating (adding together) something—such as the total dollar amount of a week’s payroll

They must be initialized to a value (usually 0)

• The accumulator in the previous slide is the variable sum.

Accumulators/Counters

Counters A counter is a numeric variable used for counting something—such as the number of employees paid in a week

They must be initialized to a value (usually

0)

• The counter in the previous example is the variable count. This variable is also the loop control variable.

The while Loop for Input Validation

Input validation is the process of ensuring that user input is valid.

System.out.print("Enter a number in the " + "range of 1 through 100: "); number = keyboard.nextInt(); // Validate the input. while (number < 1 || number > 100)

{

System.out.println("That number is invalid."); System.out.print("Enter a number in the " + "range of 1 through 100: "); number = keyboard.nextInt();

}

• Terminology:

Review

– loop control variable

– loop body

– loop condition

– accumulators

– counters

• Skill:

– Be able to write a loop construct with the Java while statement.