Home > Software design >  Controling the flow of for loop with nested if-elif statements in python3
Controling the flow of for loop with nested if-elif statements in python3

Time:08-31

I have the following code snippet.

1     for k in range(m):
2         
3         # step 1
4         step_1() # this is a function that updates some values
5     
6         # step 2
7         if len(R) == 0:
8             print("R is empty - construction completed!")
9             break 
10        elif len(R) == 1:
11            if f[i] <= b[k]:
12                # do something and terminate the program
13                break
14            else:
15                # do something and go back to step 1
16                continue
17        else: 
18            # function that assings some values to i and j
19            i,j = select_two_closest_nodes()
20    
21            if f[i] > b[k] and f[j] <= b[k]:
22                # step 2a
23                if b[k]-f[j] <= A:
24                    # do something and go back to step 1
25                    continue
26                else:
27                    # do something and go back to step 2

If I want to go from row 15 back to step 1 I can just add continue at the end of the else block, as k then enters the next iteration and step_1() runs again. Same thing if I want to go from row 24 to step 1. However, If I'm at 27 and want to skip step 1 altogether and go straight to step 2 without running step_1() I can't just add a continue. How can I go from row 27 to row 6? I.e without entering the next iteration in the for loop.

CodePudding user response:

I'd handle the situation by making new function called step_2 that returns two values "TERMINATE" and "CONTINUE". Inside this function I'll have while True: loop that is repeated as many times as step 2 is necessary.

def step_2(R, f, b, k):
    while True:

        # step 2
        if len(R) == 0:
            print("R is empty - construction completed!")
            return "TERMINATE"
        elif len(R) == 1:
            if f[i] <= b[k]:
                # do something and terminate the program
                return "TERMINATE"
            else:
                # do something and go back to step 1
                return "CONTINUE"
        else:
            # function that assings some values to i and j
            i, j = select_two_closest_nodes()

            if f[i] > b[k] and f[j] <= b[k]:
                # step 2a
                if b[k] - f[j] <= A:
                    # do something and go back to step 1
                    return "CONTINUE"
                else:
                    # do something and go back to step 2
                    continue


for k in range(m):

    # step 1
    step_1()  # this is a function that updates some values

    r = step_2(R, f, b, k):
    if r == 'TERMINATE':
        break
    elif r == 'CONTINUE':
        continue
    else:
        raise Exception('Unknown return value')

NOTE: Of course, better option is using Enum as return values instead "TERMINATE"/"CONTINUE", using pattern matching etc. This is just a concept.

  • Related