34

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
Accay Hassan
  • 367
  • 1
  • 3
  • 6
  • 5
    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**. – Soner from The Ottoman Empire Nov 24 '18 at 13:47

1 Answers1

90
REPEAT
    ...
UNTIL cond

Is equivalent to

while True:
    ...
    if cond:
        break
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 1
    `if cond` is wrong. It has to be `if not cond` for Python or `if !(cond)` for Java, C, C++ – Soner from The Ottoman Empire Nov 24 '18 at 13:54
  • 14
    @snr, both loops exit when `cond` is true – John La Rooy Nov 25 '18 at 05:21
  • 1
    The previous two comments seem contradictory and the first one has an upvote. Which is correct? Perhaps and example would help to clarify? – Robin Andrews Oct 27 '19 at 13:23
  • 11
    @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`. – Grismar Nov 25 '19 at 04:48
  • 3
    @Robin: first comment is indeed wrong. – Michael Ekoka Dec 05 '19 at 16:42
  • The answer is correct. Doing while True and then breaking on 'condition' is the same as repeat until 'condition'. Adding a not is not needed here! – Walter Schreppers Sep 13 '21 at 11:43