Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 12

CSE110 Lecture 12

Principles of Programming with Java
Loops: while and do/while statements
(202005)

Transcript

21 slides
  1. 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
  2. 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
  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 The

    while Statement • The while statement has the following syntax:
  4. 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
  5. 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
  6. 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
  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 The

    do-while Statement • The do-while statement has the following syntax:
  8. 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
  9. 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming