Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume...

89
Loops

Transcript of Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume...

Page 1: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Loops

Page 2: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Program Structure: Code Blocks

Defined by opening and closing curly bracket (e.g., { & })

Can be nested innermost opening curly bracket matches innermost closing curly bracket

can nest conditionals, loops

/** * Our first program */public class ExampleClass {

public static void main(String[] args) {

// Your code goes here!

}

}

Page 3: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

scope defines the region of code where a variable can be used

Scope is defined by code blocks variables declared inside a code block are local to that block

variables anywhere inside that block (even nested blocks!)

variables cannot be used outside that block

Scope and Variables

Page 4: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Last week, how to make decisions about whether or not to execute code

This week, how to make decisions about whether to execute code again

Example previously saw how we could use conditionals to calculate a single person’s age

loops will allow us to repeat that same code for multiple people

Control Flow in Programs

Page 5: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Often want to repeat code zero or more times

Two options copy and paste code multiple times

use a loop

What problems arise with the first option?

Why Loops?

Page 6: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Loops allow us to repeat one or more statements while some boolean condition is true, and stop when the condition is false

Loops

conditionalstatements

true

false

loops

true

false

Page 7: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

do-whileloops

Types of Loops

whileloops

true

false

true

false

true

false

forloops

Page 8: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

do-whileloops

Types of Loops

whileloops

true

false

true

false

true

false

forloops

Page 9: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

while (<boolean expression>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

Page 10: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

5

>is this true?

Page 11: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

5

>

Page 12: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

5>

Page 13: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

5

5

>

Page 14: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

6

5

>

Page 15: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

6

5

>

Page 16: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

6

>is this true?

5

Page 17: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

6

>

5

true

false

Page 18: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

6

5

true

false

>

Page 19: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

6

56

true

false

>

Page 20: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

7

56

true

false

>

Page 21: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

7

56

>

Page 22: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

7

>is this true?

56

Page 23: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

7

>

56

true

false

Page 24: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

7

56

true

false

>

Page 25: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

7

567

true

false

>

Page 26: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

8

567

true

false

>

Page 27: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

8

567

>

Page 28: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

8

>is this true?

567

Page 29: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

While the condition is true, execute the statements inside the loop

While Loops

true

false

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

memory

value (int)

8

567

>

Page 30: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Conditionals decide whether or not to execute a block of code once

Loops decide whether or not to execute a block of code multiple times

Loops vs Conditionals

conditionalstatements

true

false

loops

true

false

Page 31: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Parts of a Loop

int value = 5;

while (value < 8) {

System.out.println(value); value++;

}

//code to execute after while loop

Every loop has four parts

Page 32: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Parts of a Loop

while

}

//code to execute after while loop

Every loop has four parts initialization

set up a variable that will control the loop

int value = 5;

Page 33: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Parts of a Loop

int

while

}

//code to execute after while loop

Every loop has four parts initialization

set up a variable that will control the loop

condition a boolean expression to control when the loop stops

value < 8

Page 34: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Parts of a Loop

int

while

}

//code to execute after while loop

Every loop has four parts initialization

set up a variable that will control the loop

condition a boolean expression to control when the loop stops

work the code the loop will repeat

System.out.println(value);

Page 35: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Parts of a Loop

int

while

}

//code to execute after while loop

Every loop has four parts initialization

set up a variable that will control the loop

condition a boolean expression to control when the loop stops

work the code the loop will repeat

progress how the loop moves closer to termination

value++;

Page 36: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications

will always be true for this class!

finite loops are those that stop

infinite loops are those that repeat forever will require you to manually terminate your program

Finite vs Infinite Loops

Page 37: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Types of Loops

whileloops

true

false

true

false

forloops

do-whileloops

true

false

Page 38: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

true

false

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

Page 39: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

0 only done once!

true

false

Page 40: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

1 is this true?

true

false

Page 41: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

2

true

false

Page 42: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

3

true

false

Page 43: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

1 is this true?

true

false

Page 44: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

2

true

false

Page 45: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

3

true

false

Page 46: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while loop

1 is this true?

true

false

Page 47: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Explicitly recognizes the four parts of a loop in a single structure

For Loops

for (<var init>; <boolean expr>; <progress>) {

//code to execute if boolean expression is true

}

//code to execute after while looptrue

false

Page 48: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

true

false

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)>

Page 49: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

true

false

for

}

//code to execute after while loop

memory

i (int)

5

> int i = 5

Page 50: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

true

false

for

}

//code to execute after while loop

memory

i (int)

5

>is this true?

i < 8

Page 51: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

5

>is this true?

i < 8

true

false

Page 52: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

>

true

false

5

Page 53: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

true

false

5

> 5

Page 54: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

true

false

5

> i++

5

Page 55: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

5

> i++

6

true

false

Page 56: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

true

false

for

}

//code to execute after while loop

memory

i (int)

6

>is this true?

i < 8

5

Page 57: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

6

>is this true?

i < 8

true

false

5

Page 58: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

>

true

false

6

5

Page 59: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

true

false

56

> 6

Page 60: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

true

false

56

> i++

6

Page 61: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

56

> i++

7

true

false

5

Page 62: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

true

false

for

}

//code to execute after while loop

memory

i (int)

7

>is this true?

i < 8

56

Page 63: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

7

>is this true?

i < 8

true

false

56

Page 64: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

>

true

false

7

56

Page 65: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

true

false

567

> 7

Page 66: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

true

false

567

> i++

7

Page 67: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for

}

//code to execute after while loop

memory

i (int)

567

> i++

8

true

false

5

Page 68: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

true

false

for

}

//code to execute after while loop

memory

i (int)

8

>is this true?

i < 8

567

Page 69: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

“For i = 5, print the value of i while i is less than 8…”

For Loops

for (int i = 5; i < 8; i++) {

System.out.println(i);

}

//code to execute after while loop

memory

i (int)

567

8

>true

false

Page 70: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

For loop variables are one of the few places where you can get away with single letter variable names

but, if you can come up with a sensible name, use it!

Why use the for loop? for loops ensure you have all four parts of the loop there

easier to miss one or more with the while loop

For Loop Notes

Page 71: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

For loops are used when we know how many times we want the loop to execute

While loops are used when we aren’t sure how many times we want the loop to execute

In reality, can use for or while loops interchangeably …although it is often more natural to use one over the other in most cases

should be able to understand how both work!

For Loop vs While Loop

Page 72: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

do whileloops

Types of Loops

whileloops

true

false

true

false

true

false

forloops

Page 73: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

do {

//code to execute if boolean expression is true

} while (<boolean expr>);

//code to execute after while loop

remember me!

Page 74: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

5

>

Page 75: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

5>

Page 76: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

5>

5

Page 77: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

6

5

>is this true?

Page 78: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

6

5

>

Page 79: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

6

5

>

Page 80: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

6>

5

Page 81: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

6>

56

Page 82: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

7

56

>is this true?

Page 83: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

7

56

>

Page 84: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

7

56

>

Page 85: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

7>

56

Page 86: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

7>

567

Page 87: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

8

567

>is this true?

Page 88: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

Similar to a while loop, but checks the condition last

Do While Loops

true

false

int value = 5;

do {

System.out.println(value); value++;

} while (value < 8);

//code to execute after while loop

memory

value (int)

8

567

>

Page 89: Loops - University of Wisconsin–La Crosse...Usually, we want loops to stop at some point, resume with code after loop isn’t true for all applications will always be true for this

When we want to guarantee that our work is performed at least once

Primary use is for checking validity of user input we always want to ask for input once

only if the input is invalid do we want to ask again

Why Do While Loops?

int input;

do {

System.out.print("Enter a " + "number [1-10] "); input = scan.nextInt();

} while (input < 1 || input > 10);

//code to execute after while loop