In Python I can implement a loop with step counter and a stop condition as a classical case of for loop :
for i in range(50):
result = fun(i)
print(i, result)
if result == 0:
break
where fun(x)
is some arbitrary function from integers to integers.
I always in doubts if that is the best way to code it (Pythonically, and in terms of readability and efficiency) or is it better to run it as a while loop:
i = 0
result = 1
while result != 0 and i < 50:
result = fun(i)
print(i, result)
i += 1
which approach is better? In particular - I'm concerned about the usage of break
statement which doesn't feel right.