-1

[tmp.py]

#is_skip = False             # Alt. approach
for i in range(5):
    for j in range(3):
        if j == 2:
#            is_skip = True  # Alt. approach
            break
        print('I, J => ', i, j)
#    if is_skip:             # Alt. approach
#        break               # Alt. approach

[Expected]

I, J =>  0 0
I, J =>  0 1

[Current]

I, J =>  0 0
I, J =>  0 1
I, J =>  1 0
I, J =>  1 1
I, J =>  2 0
I, J =>  2 1
I, J =>  3 0
I, J =>  3 1
I, J =>  4 0
I, J =>  4 1

Any other best approach is available, except the above commented one.

Thanks,

Jai K
  • 375
  • 1
  • 4
  • 12

1 Answers1

0

make a function and then return from that when condition matches:

def loopBreakExample():
    for i in range(5):
        for j in range(3):
            if j == 2:

                return
            print('I, J => ', i, j)
loopBreakExample()
SM Abu Taher Asif
  • 2,221
  • 1
  • 12
  • 14