Closed sts10 closed 1 year ago
I was refactoring a function and found some weirdness with if/elsebranches in afor` loop.
if
branches in a
In the playground, this outputs nothing.
my_arr = [4,5,6,7,8] what_i_found = find_6_or_7(my_arr) print 'Found: {what_i_found}' func find_6_or_7(an_arr): for an_arr each item: if item == 6: return 6 else item == 7: return 7
But if I change the else to a second if, it runs as expected:
else
my_arr = [4,5,6,7,8] what_i_found = find_6_or_7(my_arr) print 'Found: {what_i_found}' func find_6_or_7(an_arr): for an_arr each item: if item == 6: return 6 if item == 7: return 7
Using 2 ifs in this particular example isn't any less efficient than a if and else, but I think there are cases where it would be.
This time, I checked: I use all spaces for indents here.
This should be fixed in 3343587e2ae6a0e9fdfa7e46d3f620222288f7cb.
I was refactoring a function and found some weirdness with
if
/elsebranches in a
for` loop.In the playground, this outputs nothing.
But if I change the
else
to a secondif
, it runs as expected:Using 2
if
s in this particular example isn't any less efficient than aif
andelse
, but I think there are cases where it would be.This time, I checked: I use all spaces for indents here.