Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

This I really should know!

CthulhuCthulhu Member Posts: 39
I know I should know this, and I think I do .. but .. aa #" it .. here is my question :
Could someone in simple terms explain the difference in using do-while rather than a while structure in a program. I know I had some problems with that when making large programs handling database events, and recordsets.

Comments

  • : I know I should know this, and I think I do .. but .. aa #" it .. here is my question :
    : Could someone in simple terms explain the difference in using do-while rather than a while structure in a program. I know I had some problems with that when making large programs handling database events, and recordsets.
    :

    do {
    System.out.println("You will see.");
    } while (false);

    gives output because the condition is processed AFTER the first entry into the while loop.

    ---

    while (false) {
    System.out.println("You'll never see me.");
    }

    does not give any output, because the condition is processed BEFORE any entry into the while loop!

    ---

    I never had a strong feeling that I want to use a do ... while structure, so I can not give an example when it would be of any great use ... :-(

    tron.
  • DariusDarius Member Posts: 1,666
    : : I know I should know this, and I think I do .. but .. aa #" it .. here is my question :
    : : Could someone in simple terms explain the difference in using do-while rather than a while structure in a program. I know I had some problems with that when making large programs handling database events, and recordsets.
    : :
    :
    : do {
    : System.out.println("You will see.");
    : } while (false);
    :
    : gives output because the condition is processed AFTER the first entry into the while loop.
    :
    : ---
    :
    : while (false) {
    : System.out.println("You'll never see me.");
    : }
    :
    : does not give any output, because the condition is processed BEFORE any entry into the while loop!
    :
    : ---
    :
    : I never had a strong feeling that I want to use a do ... while structure, so I can not give an example when it would be of any great use ... :-(
    :
    : tron.
    :

    You'd use a do-while loop anytime you want the body to execute at least once. A good example would be getting input.

    System.out.println("[1] Deposit");
    System.out.println("[2] Withdraw");
    System.out.println("[3] Quit");
    do{
    System.out.println("Enter choice: ");
    int choice=getInput()
    handleInput(choice);
    }while(choice!=3);

    However, I also don't use them often. They look ugly (not from a code perspective, just from an aesthetic one). Also, most of the time you either want the loop to execute while the condition is true, or not to execute at all. Of course, use them when they are appropriate.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • chamsterchamster Member Posts: 662
    : Also, most of the time you either want the loop to execute while the condition is true, or not to execute at all.

    Except for the cases where the condition is not determined until you executed the code in the loop. I always use do-while for this:

    [code]
    do
    {
    ans = JOptionPane.showInputDialog (null, "Enter max 5 chars");
    }
    while (ans.length () > 5);
    [/code]


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • Andre YoungAndre Young USAMember Posts: 0

    _ / http://forcoder.org / free video tutorials and ebooks about < Java MATLAB Scratch C C# Ruby Visual Basic .NET Perl Objective-C C++ Delphi PL/SQL R Assembly Visual Basic JavaScript Swift PHP Go Python Alice VBScript Lisp Prolog Bash ABAP F# Fortran SAS Dart FoxPro Scala Scheme Awk D Erlang Ada Lua Crystal Kotlin ML COBOL Rust Hack Julia Apex Transact-SQL Logo LabVIEW Clojure /> ____________

Sign In or Register to comment.