1 / 12

The do Statement

This article explains the syntax and usage of the do statement in Java. It includes examples and a flowchart of a do loop.

ahagan
Download Presentation

The do Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The do Statement • A do statement has the following syntax: do { statement; } while ( condition ); • The statement is executed once initially, and then the condition is evaluated • The statement is executed repeatedly until the condition becomes false

  2. statement true condition evaluated false Flowchart of a do Loop

  3. The do Statement • An example of a do loop: int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2); Don’t forget this semicolon! • The body of a do loop executes at least once

  4. The while Loop The do Loop condition evaluated statement true true false condition evaluated statement false Comparing while and do

  5. animation Trace do Loop Initialize count int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  6. animation Trace do Loop, cont. Print Welcome to Java int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  7. animation Trace do Loop, cont. Increase count by 1 count is 1 now int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  8. animation Trace do Loop, cont. (count < 2) is true int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  9. animation Trace do Loop, cont. Print Welcome to Java int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  10. animation Trace do Loop, cont. Increase count by 1 count is 2 now int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  11. animation Trace do Loop, cont. (count < 2) is false since count is 2 now int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

  12. animation Trace do Loop The loop exits. Execute the next statement after the loop. int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 2);

More Related