简体   繁体   中英

Nested loops with different and dependant steps

I need to make an algorithm to perform a task with 2 while loops. I will explain what it is I want to achieve. I need to make a loop based on length of h value. Well the loop should take big steps with f.ex. 3 or 2 or 4, the item from while or for loop should check a condition or equation. I need to check when or which value of item gives zero value to n in equation. When n value reaches zero, I need to perform a smaller step with 0.1. Because I need to be more precis. There is no need to start the loop with small step in the beginning it slows performance time, and it is just interesting to know exact small value of item which gives zero to n equation. And this is true when n equation closes to zero value.

To illustrate the example: Loop 1 starts and take big steps.

h = 50
st = 10
loop 1 starts
0 ,       3,        6,      9,       12,     15,     18,  21,  24 …………………                            #<--------   steps from h value 
25.0 , 22.0 , 19.0,  16.0, 13.0, 10.0, 7.0, 4.0, 1.0 …………………                                         #<--------  calculate values from n equation
                                                  If n <= 2 or n>= -2:
                                                  |_____ loop 2 starts with: 
                                                          24.0, 23.9 , 23.8, 23.7, 23.6, ……………..     #<--------   small steps iterate with hj = 24+st
                                                          0.4,  0.3 , 0.1, 0.0, -1.0, ……………..        #<--------  calculate values from n equation
                                                          Calculate n values from n equation
                                                          M = 2 + n
                                                          If (n <= 0 or n>= -1) and M==2 :           #<--------   If statement is fullfilled then return some values and quit loop 2 and loop 1.
                                                             Return n, itemstep from loop 2, M
                                                           
                                                          Elif (n <= 0 or n>= -1) and M!=2:
                                                             Quit loop 2 and 1                       #<--------   If statement is not fullfilled then quit loop 2 and loop 1.

The reason for n >= -1 , because we could start step from 50 down to 0 and n value can be negative as well. I have tried to figure it out with code below, but it seems not working, is it a way to generate such code in while or for loop.

M = 2         #<---------- to check conditions
j = 0 
st = 10 
h = 50        #<---------- to iterate over length h
n = h/2 - j   #<---------- to calculate n values (n equation)
result =[]    
while j <=h:    #<----------  loop 1
    n = h/2 - j
    if n <= 3 or n >= -3:  #<---------- if this is satisified then start loop2 
        print(n)
        i=j
        while i < 10+st:       #<----------  loop 2
            n = h/2-i         
            M = 2+n
            if (n <= 0 or n>= -1) and M==2:
                result.append(n, i, M)       #<----------  return values
                i = 70+j                     #<---------- stop loop 2 and loop 1
                j = h+70
            elif  (n <= 0 or n>= -1) and M!=2:  #<---------- if this is not satisified then quit loop2 and loop 1
                i = 70+j                     #<---------- stop loop 2 and loop 1
                j = h+70
            i +=0.1
            j += i
    j += 3

This might help you some. (Not exactly what you wanted but it is a start)

j = 0
h = 50
while j <= h:
    n = h/2 - j
    if -3 <= n <= 3:
        i = j
        while i < j + 10:
            n = h/2 - i
            if n <= 0:
                j = h + 70
                print(n)
                break
            i += 0.1
    j += 3

or you could do it like this.

j = 0
h = 50
while j <= h:
  n = h/2 - j
  if -3 <= n <= 3:
    break
  j += 3

i = j
while i < j + 10:
  n = h/2 - i
  if n <= 0:
      print(n)
      break
  i += 0.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM