How to write this loop better?

Viewed 739

I have a code that does something like this:

while (doorIsLocked()) {
    knockOnDoor();
}
openDoor();

but I want to be polite and always knock on the door before I open it. I can write something like this:

knockOnDoor();
while (doorIsLocked()) {
    knockOnDoor();
}
openDoor();

but I'm just wondering if there's a better idiom that doesn't repeat a statement.

6 Answers
Related