2

in java, how can we execute an instruction only one time in a do while loop

do{
    int param;

    //execute this onty one time (depends of param)
    //other instructions instructions

}while(condition)

thank you

Eddinho
  • 1,279
  • 5
  • 19
  • 29

7 Answers7

6

Putting the statement you want to execute only once is one way of doing it, but, of course, that assumes that the statement comes at the end or beginning of the loop, and doesn't depend on the conditions of what goes on in the loop (either before or after). If you have something like this:

do {
  // do some stuff
  // one time condition
  // do some more stuff
} while(condition);

you won't easily be able to pull that information outside of the loop. If this is your problem, my suggestion would be to place some sort of condition around the one-time statement(s) and update the condition when the statement has run. Something like this:

boolean hasRun = false;
do {
  // do some stuff
  if(!hasRun) {
    // one time condition
    hasRun = true;
  }
  // do some more stuff
} while(condition);
JasCav
  • 34,458
  • 20
  • 113
  • 170
2

How about:

// one-time-code here

do
{
}
while ( condition );
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
  • no i can because i have a for loop before the while loop so same probleme in the for loop and the instruction depends of a parametere exists inside the while loop – Eddinho May 15 '10 at 15:47
  • Please show a larger sample of your code that includes said for-loop. It will allow us to provide more specific answers. :) – Bob Kaufman May 15 '10 at 15:48
  • Unfortunately, I think this is too simplistic of a solution (although, his question doesn't make it clear). See my answer. – JasCav May 15 '10 at 15:50
  • @Jason: Agreed, however based on what was asked, this is the simplest answer. Based on OP's comment, I'm hoping we're going to get a clearer picture of the question shortly. – Bob Kaufman May 15 '10 at 15:51
1

Putting a statement outside of any loops will cause it to only be executed once every time the method is called.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Assuming you want to keep the code inside the loop (other posters have suggested the obvious solution of moving the code outside!), you could consider using a flag to execute it only once:

boolean doneOnce=false;

do{

  if (!doneOnce) {

    \\execute this only one time

    doneOnce=true;
  }

  \\other instructions instructions

} while (condition)

Could be a useful construct if for example you had other instructions in the loop before the once-only code.

mikera
  • 105,238
  • 25
  • 256
  • 415
0

Move the statement you want to execute only once outside the while loop.

0

Add break:

do {
        int param;

        //execute this onty one time (depends of param)
        //other instructions instructions

        break;

    } while(condition);
janusz j
  • 321
  • 3
  • 17
0

Trying to answer exactly what you asked:

bool ranOnce = false;
do{
    int param;

    if (param == SOMECONDITION and !ranOnce){
      doWhatYouWant();
      ranOnce = true;
    }
    //other instructions instructions

}while(condition)

So there you have it, run something only once depending on param.

DSchlingel
  • 15
  • 7