Professor: Kevin Molloy (adapted from slides originally developed by - - PowerPoint PPT Presentation

professor kevin molloy adapted from slides originally
SMART_READER_LITE
LIVE PREVIEW

Professor: Kevin Molloy (adapted from slides originally developed by - - PowerPoint PPT Presentation

Professor: Kevin Molloy (adapted from slides originally developed by Alvin Chao) Reminder: if-statements if (BooleanExpression) { true Boolean Statement(s) Expression Statement(s) } false While Loops while (BooleanExpression) {


slide-1
SLIDE 1

Professor: Kevin Molloy (adapted from slides originally developed by Alvin Chao)

slide-2
SLIDE 2

Reminder: if-statements

Boolean Expression Statement(s)

true false

if (BooleanExpression) { Statement(s) }

slide-3
SLIDE 3

While Loops

Boolean Expression Statement(s)

true false

while (BooleanExpression) { Statement(s) }

slide-4
SLIDE 4

While Loops

Boolean Expression Statement(s)

true false

int a = 0; while (a < 5) { System.out.println(“Hello.”); }

Is there a problem?

slide-5
SLIDE 5

While Loops

Boolean Expression Statement(s)

true false

int a = 0; while (a < 5) { System.out.println(“Hello.”); a++; }

The body of every while loop should contain instruction(s) that can change the truth value of the logical expression

slide-6
SLIDE 6

l We can use an if-statement to make sure that the

user enters valid data:

l Problem: user only gets one shot.

If for Input Validation

System.out.print("Withdrawl amount: "); amount = input.nextDouble(); if (amount < 1.0 || amount > 300.0) { System.out.println("Bad withdrawal amount!"); System.exit(0); // Exits the application. } System.out.printf("Here are your %.2f dollars.", amount);

slide-7
SLIDE 7

While Loop for Input Validation

System.out.print("Withdrawl amount: "); amount = input.nextDouble(); while (amount < 1.0 || amount > 300.0) { System.out.println("Amount must be $1.00 - $300.00."); System.out.print("Withdrawl amount: "); amount = input.nextDouble(); } System.out.printf("Here are your %.2f dollars.", amount);

l Use a while loop to keep asking while the user

still hasn't entered a valid number:

slide-8
SLIDE 8

While Loop for Input Validation

System.out.print("Withdrawl amount: "); amount = input.nextDouble(); while (amount < 1.0 || amount > 300.0) { System.out.println("Amount must be $1.00 - $300.00."); System.out.print("Withdrawl amount: "); amount = input.nextDouble(); } System.out.printf("Here are your %.2f dollars.", amount);

l Use a while loop to keep asking while the user

still hasn't entered a valid number:

Ugly that we repeat this code

slide-9
SLIDE 9

Do-While Loops

Boolean Expression Statement(s)

true false

do { Statement(s) } while (BooleanExpression);

l Referred to as a post-test

loop, because the test is performed after they loop body

slide-10
SLIDE 10

l No more code repetition:

Do-While Loop for Input Validation

do { System.out.println("Amount must be $1.0 - $300.00"); System.out.print("Withdrawl amount: "); amount = input.nextDouble(); } while (amount < 1.0 || amount > 300.0); System.out.printf("Here are your %.2f dollars.", amount);

slide-11
SLIDE 11

Counting Loops

l Common to write loops that execute some fixed

number of times:

int frame = 1; while (frame <= 10) { // Get bowling scores for this frame. // Do some fancy calculations. // Show a turkey animation if needed... frame++; }

slide-12
SLIDE 12

Counting Loops

l Common to write loops that execute some fixed

number of times:

int frame = 1; while (frame <= 10) { // Get bowling scores for this frame. // Do some fancy calculations. // Show a turkey animation if needed... frame++; } We need to look in three different places to figure

  • ut what this loop

is doing.

slide-13
SLIDE 13

For Loops

int frame = 1; while (frame <= 10) { // Get bowling scores for th // Do some fancy calculati // Show a turkey anim... frame++; }

for (int frame = 1; frame <= 10; frame++) { // Get the latest scores. // Do some fancy calculations. // Show a turkey animation if needed... }

l For loops provide more concise syntax for the same

logic:

slide-14
SLIDE 14
  • Acknowledgements

Parts of this activity are based on materials developed by Chris Mayfield and Nathan Sprague. </end>