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
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
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
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
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
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
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.