'goto' in micropython

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

'goto' in micropython

Post by VladVons » Wed Feb 26, 2020 6:01 am

I know all horrow stories about GOTO, but nevertheless....

under python for PC there is a 6k library "goto-statement"
It is not small for ESP8266

how to exit from nested loop?

Code: Select all

def LabeledNestedLoop(aMax):
	for A in range(10):
		#LabelA
		for B in range(10):
			for C in range(10):
				print(A, B, C)
				if (C == aMax):
					#goto LabelEnd
					# or 
					#goto LabelA
	#LabelEnd
	print('The end')
	

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: 'goto' in micropython

Post by pythoncoder » Wed Feb 26, 2020 6:04 am

Use exceptions.

Code: Select all

class MyError(RuntimeError):
    pass

try:
    while True:
        while True:
            while True:
                raise MyError
except MyError:
    print('We got out!')
Peter Hinch
Index to my micropython libraries.

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: 'goto' in micropython

Post by VladVons » Wed Feb 26, 2020 7:54 am

Thatks Peter
with LabelEnd you are right

any idea about LabelA?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: 'goto' in micropython

Post by jimmo » Wed Feb 26, 2020 9:27 am

Use functions

Code: Select all

def LabeledNestedLoop(aMax):
      def inner(A):
             for B in range(10):
			for C in range(10):
				print(A, B, C)
				if (C == aMax):
					return True # LabelA
					# or
					return False # LabelEnd
	
	for A in range(10):
	        # LabelA
	        if not inner(A):
	            break

        #LabelEnd
	

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: 'goto' in micropython

Post by pythoncoder » Thu Feb 27, 2020 8:10 pm

Or use multiple exceptions:

Code: Select all

class MyError(RuntimeError):
    pass
class MyInnerError(MyError):
    pass
try:
    while True:
        try:
            while True:
                while True:
                    raise MyError # or MyInnerError
        except MyInnerError:
            print('inner')
except MyError:
    print('We got out!')
Exceptions don't necessarily correspond to errors: they can be used to control program flow.
Peter Hinch
Index to my micropython libraries.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: 'goto' in micropython

Post by stijn » Fri Feb 28, 2020 12:12 am

StopIteration being the prime example..

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: 'goto' in micropython

Post by VladVons » Fri Feb 28, 2020 6:36 am

thanks everyone for ideas

Code: Select all

def LabeledNestedLoop(aMax):
    print('begin')
    try:
        for A in range(aMax):
            for B in range(aMax):
                for C in range(aMax):
                    print(A, B, C)
                    if (C > 3):
                        raise StopIteration
    except StopIteration:
        #LabelEnd
        print('end')

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: 'goto' in micropython

Post by pythoncoder » Fri Feb 28, 2020 7:04 am

This use of StopIteration is unorthodox and likely to confuse anyone reading your code. It is intended for use in generators. My preference is for custom exception classes as in my examples. Although my choice of names could have been better, perhaps QuitInnerLoop rather than MyInnerError.

In my code there is usually there is an application specific name which comes to mind like MessageComplete.
Peter Hinch
Index to my micropython libraries.

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: 'goto' in micropython

Post by VladVons » Fri Feb 28, 2020 4:34 pm

Thanks Peter for your posts here.
Your GitHub is also a good reading book :)

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: 'goto' in micropython

Post by OutoftheBOTS_ » Fri Feb 28, 2020 10:44 pm

The technique that I use is instead of using "while True" I have a flag that I can switch on/off

Code: Select all

loop_true = True

while loop_true
	
	#code here
	
	if (want_to_exit): loop_true = False

Post Reply