0

I am trying to convert a particular JavaScript code into Python code. There is one part I do not understand. This is the JavaScript code:

    do {
        // something 
    } while (a > 0.01);
    b = 10

I tried to replicated it by:

    for i in range(1000):  # or: while True:
        // something 
        if (a > 0.01):
            continue
        else:
            break
    b = 10

But looks like I am wrong.

Can somebody tell me what am I doing wrong in the upper python code? Thank you.

EDIT: The problem is not with "for i in range(1000)" instead of "while True".

marco
  • 899
  • 5
  • 13
  • 21
  • possible duplicate of [Emulate a do-while loop in Python?](http://stackoverflow.com/questions/743164/emulate-a-do-while-loop-in-python) – juanchopanza Sep 08 '15 at 22:10
  • Thank you juanchopanza. I took a look at the topic you attached. Does this mean that my upper python code is correct as one of the ways to replicate JavaScript do-while loop (the issue is not with "while True" instead of "range(1000))? – marco Sep 08 '15 at 22:36

3 Answers3

2

This code:

do {
        // something 
    } while (a > 0.01);
    b = 10

Is equivalent to:

while(True):
    #Do something
    if a <= 0.01:
        break

The do statement always executes its body the first time, and after executing the inner code checks the condition, if it's still true it continues, otherwise it exits.

It can be replicated in Python creating an "infinite loop", then executing the code on the inner cycle and evaluating the negation of your condition, which is a <= 0.01, just to exit if it's true.

This code:

for i in range(1000):  # or: while True:
    // something 
    if (a > 0.01):
        continue
    else:
        break
b = 10

is not equivalent the part of the code you reveal, as it executes 1000 times always checking the condition (look that your do..while code in javascript does not specify 1000 runs anywhere.

Note: Be careful indenting Python code...it can be really painful at the beginning.

avenet
  • 2,894
  • 1
  • 19
  • 26
  • Thank you avenet. The problem is not with "for i in range(1000)" instead of "while True". Also your indentation of "b=10" and mine are not the same. – marco Sep 08 '15 at 22:20
  • I still do not understand you. What you did is replaced the range with while and reversed condition from break to continue. That is exactly what my code does (again the issue is not with the number of iterations, I am getting the same result even with range(10000)). – marco Sep 08 '15 at 22:28
  • Please can you put the entire code? As it's difficult to see what are you doing with the a's... – avenet Sep 08 '15 at 22:35
  • I do not know how to put a code into a comment. Do I need to open a new question? – marco Sep 08 '15 at 22:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89108/discussion-between-marco-and-avenet). – marco Sep 08 '15 at 23:10
1

Instead of using:

for i in range(1000):

You should instead have:

while True:

The do-while loop in Java continues to do something while your condition is true even after more than 1000 iterations (unlike your python version)

NJM
  • 565
  • 3
  • 13
  • Thank you NJM. I always tend to not use "while True" when I can. In this case I know that the loop will likely break in less than 100 iterations (sometimes even 3,4). – marco Sep 08 '15 at 22:16
1

This should do it:

while(True):
    # something
    if (a <= 0.01): break
TextGeek
  • 1,196
  • 11
  • 23
  • Thank you TextGeek. What is the difference between your solution and mine with for in range(1000)? The loop always gets broken before 1000, I just took it for safety reasons, as high enough value. – marco Sep 08 '15 at 22:22
  • I'd consider it clearer; and it will work if you happen to need over 1000 reps. If you're certain 1000 will always be enough, the range-loop will work. The other thing is that range() creates an actual array of 1000 items, which is a waste of time and space -- xrange() would avoid that problem (see http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range) – TextGeek Sep 08 '15 at 22:44