473,805 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Good python equivalent to C goto

Hello,

Any suggestions on a good python equivalent for the following C code:

while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}

Thanks
Kurien
Aug 16 '08
22 7927

-----Original Message-----
From: py************* *************** ****@python.org [mailto:python-
li************* ************@py thon.org] On Behalf Of Kurien Mathew
Sent: Saturday, August 16, 2008 5:21 PM
To: py*********@pyt hon.org
Subject: Good python equivalent to C goto

Hello,

Any suggestions on a good python equivalent for the following C code:

while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}

=============== ============
Use a flag and a one loop for loop
while loopCondition:
flag = False
for i in range(1):
if condition1:
break
if condition2:
break
if condition3:
break
stmt1
stmt2
flag = True
if not flag:
stmt3
stmt4
=============== ============
Or just a straight flag

while ( loopCondition ):
flag = False

if (condition1)
flag = True # goto next;
if (not flag and condition2)
flag = True # goto next;
if (not flag and condition3)
flag = True # goto next;

if not flag:
stmt1
stmt2
else
stmt3
stmt4

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622
Aug 18 '08 #21
On Aug 17, 1:09*pm, Matthew Fitzgibbons <eles...@nienna .orgwrote:
Kurien Mathew wrote:
Hello,
Any suggestions on a good python equivalent for the following C code:
while (loopCondition)
{
* * if (condition1)
* * * * goto next;
* * if (condition2)
* * * * goto next;
* * if (condition3)
* * * * goto next;
* * stmt1;
* * stmt2;
next:
* * stmt3;
* * stmt4;
*}
Thanks
Kurien
--
http://mail.python.org/mailman/listinfo/python-list

I would not be too happy if I saw C code like that in my repository.
This is equivalent:

while (loopCondition) {
* * *if (!(condition1 || condition2 || condition3)) {
* * * * *stmt1;
* * * * *stmt2;
* * *}
* * *stmt3;
* * *stmt4;

}

In Python:

while (loopCondition) :
* * *if not (condition1 or condition2 or condition3):
* * * * *stmt1
* * * * *stmt2
* * *stmt3
* * *stmt4

If stmt3 and stmt4 are error cleanup code, I would use try/finally.

while loopCondition:
* * *try:
* * * * *if condition1:
* * * * * * *raise Error1()
* * * * *if condition2:
* * * * * * *raise Error2()
* * * * *if condition3:
* * * * * * *raise Error3()
* * * * *stmt1
* * * * *stmt2
* * *finally:
* * * * *stmt3
* * * * *stmt4

This will also bail out of the loop on and exception and the exception
will get to the next level. If you don't want that to happen, put an
appropriate except block before the finally.

-Matt- Hide quoted text -

- Show quoted text -
Close, but there is no reason for the conditions to raise anything,
they can just use the continue statement:

i = 20
while i 0:
try:
if i % 2:
continue
if i % 3:
continue
print i, "is even and a multiple of 3"
finally:
i -= 1

Prints:
18 is even and a multiple of 3
12 is even and a multiple of 3
6 is even and a multiple of 3

I think this is closest to the OP's stated requirements.

-- Paul
Aug 18 '08 #22
Paul McGuire wrote:
On Aug 17, 1:09 pm, Matthew Fitzgibbons <eles...@nienna .orgwrote:
>Kurien Mathew wrote:
>>Hello,
Any suggestions on a good python equivalent for the following C code:
while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}
Thanks
Kurien
--
http://mail.python.org/mailman/listinfo/python-list
I would not be too happy if I saw C code like that in my repository.
This is equivalent:

while (loopCondition) {
if (!(condition1 || condition2 || condition3)) {
stmt1;
stmt2;
}
stmt3;
stmt4;

}

In Python:

while (loopCondition) :
if not (condition1 or condition2 or condition3):
stmt1
stmt2
stmt3
stmt4

If stmt3 and stmt4 are error cleanup code, I would use try/finally.

while loopCondition:
try:
if condition1:
raise Error1()
if condition2:
raise Error2()
if condition3:
raise Error3()
stmt1
stmt2
finally:
stmt3
stmt4

This will also bail out of the loop on and exception and the exception
will get to the next level. If you don't want that to happen, put an
appropriate except block before the finally.

-Matt- Hide quoted text -

- Show quoted text -

Close, but there is no reason for the conditions to raise anything,
they can just use the continue statement:

i = 20
while i 0:
try:
if i % 2:
continue
if i % 3:
continue
print i, "is even and a multiple of 3"
finally:
i -= 1

Prints:
18 is even and a multiple of 3
12 is even and a multiple of 3
6 is even and a multiple of 3

I think this is closest to the OP's stated requirements.

-- Paul
--
http://mail.python.org/mailman/listinfo/python-list
I'm aware my example where I raised exceptions was functionally
different ("This will bail out of the loop on an exception and the
exception will get to the next level.") Just thinking in terms of
telling the caller something went wrong. The other examples were
functionally equivalent, however.

Still, this is the best way so far. :) I never thought of using continue
with finally. It gets added to my Python tricks file for future use.

-Matt
Aug 18 '08 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
12736
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
20
3865
by: BJ MacNevin | last post by:
Hi all, I teach middle school and am currently trying to bring some computer science to the students. Our district has a wonderfully linked network throughout all our schools... done via MS Windows Network. In order to protect the network, our district's IT department does not want things installed on the system (or at least makes it VERY difficult to get it done). SO, I am using MSW Logo installed onto a CD-ROM... we just stick in the...
28
3312
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
41
3564
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
2
4457
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
150
6590
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and see if you agree with my opinion or not. Tony Marston http://www.tonymarston.net
13
2164
by: Steven Bethard | last post by:
Jean-Paul Calderone <exarkun@divmod.comwrote: Interesting. Could you give a few illustrations of this? (I didn't run into the same problem at all, so I'm curious.) Steve
206
8392
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
0
947
by: Jean-Paul Calderone | last post by:
On Sat, 16 Aug 2008 23:20:52 +0200, Kurien Mathew <kmathew@envivio.frwrote: Goto isn't providing much value over `if´ in this example. Python has a pretty serviceable `if´ too: while loopCondition: if not (condition1 or condition2 or condition3): stmt1 stmt2 stmt3
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.