Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 12: Loops: while and do/while statements Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 Topics class global variables methods statements instructions local variables conditional Statements if-else switch ?: loop Statement while do-while for

Slide 3

Slide 3 text

while Statement

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 The while Statement • The while statement has the following syntax:

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Logic of a while Loop zero or more

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Example 1

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 The while Statement • Note that if the condition of a while statement is false initially, the statement is never executed • Therefore, the body of a while loop will execute zero or more times

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Infinite Loops • The body of a while loop eventually must make the condition false • If not, it is an infinite loop, which will execute until the user interrupts the program

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Example 2

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Nested Loops • Similar to nested if statements, loops can be nested as well. That is, the body of a loop can contain another loop • Each time through the outer loop, the inner loop goes through its full set of iterations

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Example 3

Slide 12

Slide 12 text

do-while Statement

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 The do-while Statement • The do-while statement has the following syntax:

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Logic of a do-while Loop one or more

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Example 4

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 The do-while Statement • A do loop is similar to a while loop, except that the condition is evaluated after the body of the loop is executed • Therefore the body of a do loop will execute at least once

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Example 5

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Comparing while and do-while

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 Test yourselves ?

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20 Reference Chapter 4

Slide 21

Slide 21 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.