How to exit an if clause

前端 未结 13 1722
故里飘歌
故里飘歌 2020-12-04 08:45

What sorts of methods exist for prematurely exiting an if clause?

There are times when I\'m writing code and want to put a break statement

相关标签:
13条回答
  • 2020-12-04 09:07

    For what was actually asked, my approach is to put those ifs inside a one-looped loop

    while (True):
        if (some_condition):
            ...
            if (condition_a):
                # do something
                # and then exit the outer if block
                break
            ...
            if (condition_b):
                # do something
                # and then exit the outer if block
                break
            # more code here
        # make sure it is looped once
        break
    

    Test it:

    conditions = [True,False]
    some_condition = True
    
    for condition_a in conditions:
        for condition_b in conditions:
            print("\n")
            print("with condition_a", condition_a)
            print("with condition_b", condition_b)
            while (True):
                if (some_condition):
                    print("checkpoint 1")
                    if (condition_a):
                        # do something
                        # and then exit the outer if block
                        print("checkpoint 2")
                        break
                    print ("checkpoint 3")
                    if (condition_b):
                        # do something
                        # and then exit the outer if block
                        print("checkpoint 4")
                        break
                    print ("checkpoint 5")
                    # more code here
                # make sure it is looped once
                break
    
    0 讨论(0)
  • 2020-12-04 09:09

    The only thing that would apply this without additional methods is elif as the following example

    a = ['yearly', 'monthly', 'quartly', 'semiannual', 'monthly', 'quartly', 'semiannual', 'yearly']
    # start the condition
    if 'monthly' in b: 
        print('monthly') 
    elif 'quartly' in b: 
        print('quartly') 
    elif 'semiannual' in b: 
        print('semiannual') 
    elif 'yearly' in b: 
        print('yearly') 
    else: 
        print('final') 
    
    0 讨论(0)
  • 2020-12-04 09:11

    may be this?

    if some_condition and condition_a:
           # do something
    elif some_condition and condition_b:
               # do something
               # and then exit the outer if block
    elif some_condition and not condition_b:
               # more code here
    else:
         #blah
    if
    
    0 讨论(0)
  • 2020-12-04 09:11

    Generally speaking, don't. If you are nesting "ifs" and breaking from them, you are doing it wrong.

    However, if you must:

    if condition_a:
       def condition_a_fun():
           do_stuff()
           if we_wanna_escape:
               return
       condition_a_fun()
    if condition_b:
       def condition_b_fun():
           do_more_stuff()
           if we_wanna_get_out_again:
               return
       condition_b_fun()
    

    Note, the functions don't HAVE to be declared in the if statement, they can be declared in advance ;) This would be a better choice, since it will avoid needing to refactor out an ugly if/then later on.

    0 讨论(0)
  • 2020-12-04 09:12
    while some_condition:
       ...
       if condition_a:
           # do something
           break
       ...
       if condition_b:
           # do something
           break
       # more code here
       break
    
    0 讨论(0)
  • 2020-12-04 09:13

    So here i understand you're trying to break out of the outer if code block

    if some_condition:
        ...
        if condition_a:
           # do something
           # and then exit the outer if block
           ...
        if condition_b:
           # do something
           # and then exit the outer if block
    # more code here
    

    One way out of this is that you can test for for a false condition in the outer if block, which will then implicitly exit out of the code block, you then use an else block to nest the other ifs to do something

    if test_for_false:
        # Exit the code(which is the outer if code)
    
    else:
        if condition_a:
            # Do something
    
        if condition_b:
            # Do something
    
    0 讨论(0)
提交回复
热议问题