1

Similar to suggestions listed here, is it possible to do the same with the continue statement?

Something like this:

for x in range(10):
   continue if x<5

Thanks for the help

yonatan
  • 113
  • 9
  • 1
    You can do `if x < 5: continue` on one line. – Samwise Dec 21 '21 at 16:17
  • 3
    No, Python does not have Perl-style statement modifiers. The question you link to talks about conditional expressions. – chepner Dec 21 '21 at 16:18
  • Are you more interested in the *syntax* in the body of the loop, or a solution to avoiding the values of `x` that match the condition in the first place? If the latter, you can use `filter` and `itertools.filterfalse` directly on the iterable to avoid entering the loop at all for "bad" values of `x`. – chepner Dec 21 '21 at 16:21
  • What *problem do you hope to solve* this way? If you simply want to know what is syntactically possible, that is what the documentation is for. – Karl Knechtel Dec 21 '21 at 16:25

2 Answers2

1

No, this is not possible in python you will have to revert to:

for x in range(10):
   if x<5:
     continue

However, like the comments pointed out you can make a one line if out of that:

if x < 5: continue

I would not recommend using if statements like that tho since it makes the code harder to read and you do not really gain anything from it.

Josip Domazet
  • 2,246
  • 2
  • 14
  • 37
1

You can use Python's one line if statements:

for x in range(10):
   if x < 5: continue
   print(x)

as explained here

Tuor
  • 875
  • 1
  • 8
  • 32