Do While loop Example

  1. /*
  2.   Do While loop Example
  3.   This Java Example shows how to use do while loop to iterate in Java program.
  4. */
  5.  
  6. public class DoWhileExample {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. /*
  11.   * Do while loop executes statment until certain condition become false.
  12.   * Syntax of do while loop is
  13.   *
  14.   * do
  15.   * <loop body>
  16.   * while(<condition>);
  17.   *
  18.   * where <condition> is a boolean expression.
  19.   *
  20.   * Please not that the condition is evaluated after executing the loop body.
  21.   * So loop will be executed at least once even if the condition is false.
  22.   */
  23.  
  24. int i =0;
  25.  
  26. do
  27. {
  28. System.out.println("i is : " + i);
  29. i++;
  30.  
  31. }while(i < 5);
  32.  
  33. }
  34. }
  35.  
  36. /*
  37. Output would be
  38. i is : 0
  39. i is : 1
  40. i is : 2
  41. i is : 3
  42. i is : 4
  43. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine

Related Java Examples





    Suggested Reading

    Oracle Magazine
    Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
    Cost: FREE

    View/Subscribe
    NASA Tech Briefs
    Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
    Cost: FREE

    View/Subscribe
    FierceBiotech IT
    Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
    Cost: FREE

    View/Subscribe
    Simply SQL
    Simply SQL is a practical step-by-step guide to writing SQL.
    Cost: FREE

    View/Subscribe
    Simply JavaScript
    Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
    Cost: FREE

    View/Subscribe
    PCMag.com's What's New Now
    Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
    Cost: FREE

    View/Subscribe

    Import data

    wat if ud like to get the same statement but instead ask the user to key in the data how would the program be??

    You'd have to import

    You'd have to import scanner, and set i to the scanner.

    An easy way

    An easy way would be to use InputStreamReader class and wrap it with BufferedReader class. Then parse the inputted value and set the variable with that value.

    hi..

    is there any more complicate do while loops example...
    that has a command of yes/no to terminate the pr0gram...


    Could not find what you are looking for? Search Java Examples




    Feel Tired? Read Jokes & Inspirational Stories, Play Games!