Docsity

While and Do-While Loops, Exams of Law

Partial preview of the text

Download While and Do-While Loops and more Exams Law in PDF only on Docsity!

While and Do-While Loops

15-110 Summer 2010

Margaret Reid-Miller

Loops

  • Within a method, we can alter the flow of control

using either conditionals or loops.

  • The loop statements while, do-while, and for

allow us execute a statement(s) over and over.

  • Like a conditional, a loop is controlled by a boolean

expression that determines how many times the

statement is executed.

E.g., You may want to calculate the interest paid on a mortgage for each year of the loan term.

The if Flowchart

boolean_expression statement (body of loop) true false

The while Flowchart

boolean_expression statement (body of loop) true false

The Loop Control Variable

  • The variable i (known as the loop control variable )

is used in three ways: it is initialized, tested , and

updated.!

! int i = 0;! ! while (i < n) {! !! System.out.print(“”);! !!* i++;! ! }! ! System.out.println();!

  • All three things must be coordinated in order for

the loop to work correctly!

// initialize // test // update

Off-by-1 Errors

int i = 0;! while (i < n) {!! System.out.print(“”);! !* i++;! }! System.out.println();! For n = 5 the output is ***** (5 asterisks) ! int i = 1 ;! ! while (i < n) {! !! System.out.print (“”);! !!* i++;! ! }! ! System.out.println();! Output?

Warning!

! What is the output if n = 5?

int i = 0;! while (i < n) {! !! System.out.print(“”);! !* i--;! }! System.out.println();!

Infinite Loops

Do you know which

company has this address?

1 Infinite Loop

Cupertino, CA 95014

Apple Computer

int i = 0;! while (i < n) {! !! System.out.print(“”);! !* i--;! }! System.out.println();!

Exercise

  • Write a method with a while loop to prints 1 through

n in square brackets. For example, if n = 6 print

! ![1] [2] [3] [4] [5] [6]!

Exercise: Cumulative Sum

  • Write a method with a while loop that computes the

sum of first n positive integers:

sum = 1 + 2 + 3 + … + n

Examples:

n = 5 sum = 15

n = 19 sum = 190

The do Statement

  • The form of the do statement is

do!

!! while (< boolean_expression> );

  • First, statement is executed.
  • Then, the boolean_expression is evaluated. If it

evaluates to true, statement is executed again.

  • This repetition continues until the

boolean_expression evaluates to false.

The do Flowchart

boolean_expression statement false true

User Input

Scanner keyboard = new Scanner(System.in);! System.out.print(! !! “Please enter the month [1-12]: ”);! int month = keyboard.nextInt();!

What if the user enters a month outside the range?

User Input (cont’d)

  • Use a do-while loop to test whether a user has entered

data of the correct form and, if not, ask repeatedly until

the data entered is correct.

Scanner keyboard = new Scanner(System.in);! int month;! do {! ! System.out.print(! !! “Please enter the month [1-12]: ”);! ! month = keyboard.nextInt();! } while ( month < 1 || month > 12 );! Must be declared outside the loop Outside the scope of the loop