I had one idea and to implement it I need your help. Is it possible in the loop, for, while, do while, does not matter, write code that will be executed only once, that is:

boolean variable = false; for (int i;i==10;i++) { System.out.println("OK"); if (i==5) { variable = true; } if (variable==true) { System.out.println(variable); } } 

How to make the if executed only 1 time, and not run more in a loop to get the most optimization.

  • 2
    add break; last line in if - Drawn Raccoon
  • 2
    If the code has to be executed once, then it is worth taking it out of the loop - Regent

2 answers 2

 if (variable==true){ System.out.println(variable); break; } 
  • And after the brake; Can I write commands that will be repeated? :-) - Fussion Bart
  • You exit the loop after break; - guerrk1n Nov.

No additional variables needed.

 if (i==5) { // здесь код } 

It will be executed in a loop once - code i is equal to 5.

  • Thanks, I did just that - Fussion Bart 8:49