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)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

June 05, 2017
Tweet

Transcript

  1. CSE110 Principles of Programming with Java Lecture 12: Loops: while

    and do/while statements Javier Gonzalez-Sanchez javiergs@asu.edu 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. while Statement

  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 The

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

    of a while Loop zero or more
  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Example

    1
  7. 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
  8. 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
  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Example

    2
  10. 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
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Example

    3
  12. do-while Statement

  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 The

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

    of a do-while Loop one or more
  15. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Example

    4
  16. 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
  17. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 Example

    5
  18. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Comparing

    while and do-while
  19. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 Test

    yourselves ?
  20. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20 Reference

    Chapter 4
  21. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu 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.