Why is there no goto statement in Python?
Yes, there is no goto statement in Python. Let’s first understand what goto is in C language. However, the use of goto is also discouraged in C.
The goto statement in C programming provides an unconditional jump from "goto" to a marked statement in the same function. The following is the syntax -
goto label; .. . label: statement;
Example
Now let’s look at the goto C program -
#include <stdio.h> int main () { int a = 10; LOOP:do { if( a == 15) { /* skip the iteration */ a = a + 1; goto LOOP; } printf("a = %d\n", a); a++; }while( a < 20 ); return 0; }
Output
a = 10 a = 11 a = 12 a = 13 a = 14 a = 16 a = 17 a = 18 a = 19
Note - The use of goto statements is also discouraged in C language.
There is no GoTo in Python
In Python, goto is not needed as we can accomplish the same task using if statements and or, and, if-else expressions and loops (including continue and break) using while and for statements.
User-defined exceptions
Use user-defined exceptions as an alternative -
class goto1(Exception): pass class goto2(Exception): pass class goto3(Exception): pass def loop(): print('start') num = input() try: if num<=0: raise goto1 elif num<=2: raise goto2 elif num<=4: raise goto3 elif num<=6: raise goto1 else: print('end') return 0 except goto1 as e: print('goto1') loop() except goto2 as e: print('goto2') loop() except goto3 as e: print('goto3') loop()
Nested method
Example
Use nested methods as another option -
def demo(): print("In the demo() function") def inline(): print("In") inline() demo()
Output
In In the demo() function
goto statement module
It is a function decorator using goto in Python. Tested on Python 2.6 to 3.6 and PyPy. Install it using pip -
Note: Applicable to Python 3.6
pip install goto-statement
Let’s see an example −
# Python 3.6 from goto import with_goto @with_goto def range(start, stop): i = start result = [] label .begin if i == stop: goto .end result.append(i) i += 1 goto .begin label .end return result
The above is the detailed content of Why is there no goto statement in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
