37

I am a beginner in Python programming. I am trying to work on this algorithm that finds convex hull using Graham's scan method. However, in the pseudocode, there is a repeat ... until loop, which I could not figure out a way to write it in Python.

How do I write a repeat ... until loop in Python?

3
  • 6
    as a side-note, google redirects to here when asking repeat-until equivalent. So, in other languages, if exists, do-while is repeat-until except that condition is logically inverted. Commented Nov 24, 2018 at 13:47
  • 1
    There is another difference. In repeat-until, the condition is evaluated at the end of the loop. So at least one iteration of the loop will always be executed before the end is reached and condition is evaluated. Commented Jan 7, 2024 at 15:40
  • Yes, @AssafR points out the main point of using the repeat..until clause: The while-loop's body will never execute at all if the condition fails in the first iteration, the repeat...until loop's body will always do at least one iteration, no matter how the condition evaluates. Having a specific language construct to point this out sometimes helps explain your algorithm (at least I feel the lack of it when the language doesn't have repeat...until) Commented May 8, 2024 at 17:52

1 Answer 1

95
REPEAT
    ...
UNTIL cond

Is equivalent to

while True:
    ...
    if cond:
        break
Sign up to request clarification or add additional context in comments.

6 Comments

if cond is wrong. It has to be if not cond for Python or if !(cond) for Java, C, C++
@snr, both loops exit when cond is true
The previous two comments seem contradictory and the first one has an upvote. Which is correct? Perhaps and example would help to clarify?
@Robin the confusion is a good illustration of the common confusion between while cond do {} and repeat {} until cond loops. A while loop continues as long as its condition is True, a repeat loop continues until its condition is True. @snr is wrong, but it's a common mistake. Compare: while must_continue: command and while True: command; if not must_continue: break. You could do the opposite with repeat, but you never see those examples, because languages with repeat typically also have while.
@Robin: first comment is indeed wrong.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.