Hello,

I know the break or continue statements for the loops...
But I have a if stetement that I would like to exit from and I don't know how to do

value = -1
    if true:
        print 'hello world'

        # Some unexpected value is set so I would like to leave here
        # but break statement is for loop and not for if
        if value < 0: 
            ????????
        print 'value was >0 so I am still in the if statement'

Any idea on the way to exit the if before the end of the if statement ?
Thank you
Clousot

Just put else branch and put line 9 etc inside the else.

Member Avatar for Enalicho
value = -1
    if true:

true isn't defined. Remember that case matters, it's True, not true.

print 'hello world'

        # Some unexpected value is set so I would like to leave here
        # but break statement is for loop and not for if
        if value < 0: 
            ????????
        print 'value was >0 so I am still in the if statement'

You don't need to break, you just need to use the statement correctly, for example -

value = 2

if True:
    print 'ohai'
    if value > 0:
        print 'This is what I expected'
        do_stuff()
    else:
        print 'This is not what I expected'
        do_nothing()

Then wrap the code you want to run in the appropriate if statement.

Member Avatar for Enalicho
value = -1
    if true:

true isn't defined. Remember that case matters, it's True, not true.

print 'hello world'

        # Some unexpected value is set so I would like to leave here
        # but break statement is for loop and not for if
        if value < 0: 
            ????????
        print 'value was >0 so I am still in the if statement'

You don't need to break, you just need to use the statement correctly, for example -

value = 2

if True:
    print 'ohai'
    if value > 0:
        print 'This is what I expected'
        do_stuff()
    else:
        print 'This is not what I expected'
        do_nothing()

Then wrap the code you want to run in the appropriate if statement.

Thank you for your answers.

In fact I did improvise this small example of code to explain my need, but on the way my real code is written there are already several indentation while I try to keep my code on 80 columns.
This happend in several parts of the code doing every time something else... so I wanted to try to avoid adding a new indentation and also new procedures for each time (yes perhaps you will think that I am complicated ;) ...)

Thank you
Clousot

Member Avatar for Enalicho

It's hard to answer without seeing your code. It's possible that you need to split it up into functions, or that what you're doing isn't a good idea.

I think you need refactoring. Can you express your reasons why you want "avoid adding a new indentation and also new procedures" (learned Pascal as (almost) first (real) prograramming language like me?)

Could you post the real code for this function you are currently testing with test function with some usable input values from your test cases or docstring tests, so we could advice better organization for the code.

I like to use a function as it is simple and the logic obvious

def value_func():
    value = -1
    if true:
        print 'hello world'
     
    # Some unexpected value is set so I would like to leave here
    # but break statement is for loop and not for if
    if value < 0:
        return some_value
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.