How to force a program to always run the first iteration of a while loop?

I am writing a program to implement the algorithm I found in the literature

while(solution has changed){
    updateSolution();
}

To check whether the while condition is met, I created an object named copy (the same as the solution type) This copy is a copy of the solution until the solution is updated Therefore, if the solution changes, the conditions in the while loop will be met

However, I encountered some problems when I found the best solution to the two object conditions when executing the while loop, because I started with an empty solution (result set) and the copy was also empty at that time (both using constructor calls) This means that when a while loop is executed, both objects are equal, so all statements in the while loop will not be executed

My current solution is to create a virtual variable set to true before the while loop and set it to false I doubt this is the best solution, so I wonder if there is a standard solution to this problem (somehow forcing the program to always run the first iteration of the while loop)?

The code is now:

SolutionSet solution = new SolutionSet();
SolutionSet copy = new SolutionSet();
boolean dummy = true;

while((!solution.equals(copy)) || dummy){
   dummy = false;
   copy = solution.copy();
   solution.update() // here some tests are done and one object may be added to solution
}

Solution

Use do {} while (condition)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>