hosting image

Looping In Odoo 14

Repeating code over and over again known as a loop. And is also a fairly common scenario in programming. Python provide couple of ways to doing that.

 

Why we use loop in programming?

There may be some situation where we need to execute a block of code again and again or we want to execute some lines of code repeatedly. A loop in programming languages allows us to execute a statement or block of statement multiple times.

Like other programming languages, python provide following types of loops to handle such type of scenario where we need to execute code or block of code repeatedly.

  1. While Loop
  2. For Loop

 

 

In Python  we using forloop for looping the code,

 

FOR LOOP IN ODOO 14

For loop in python is little bit different like other programming languages (JavaScriptC++ and Java). These types of language have the concept of index variable that counts the number of iteration in for loop.

So for example in Python the syntax of for loop is looks like this.

def main():
    x = 0
    # define a for loop
    for x in range(4,10):
        print("Value of x: ",x)
if __name__ == "__main__": main()

Note:Python for loops are called iterator.In this case if I want have x to loop over the
range of numbers than I need to use python built in range() function. So I have a range 
going to 4 to 10 in for loop and inside the loop I have a print statement that will 
prints the value of x.



Output will be like this:
Python - Loops
For loops not only operates on number it also operates on sets of things such as collections, objects and lists. Here collections mean a set of or a list of objects for example a list of or a collection of day’s name.

For example a list of day’s, list of months and list of data which we need to loop through.

Days = [“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”]

Use a for loop over a collection/list


#!/usr/bin/python3 def main(): # use a for loop over a collection/list list_of_days = [“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”] for day in list_of_days:

    print(“Day Name: “,day)
if __name__ == “__main__”: main()

In the above code I declared a variable name list_of_days and I am initializing this to a list of days using opening and closing square bracket. And inside of the bracket I have my day name. Now I am going to loop through the list and printing out the day. Here we are not using any number instead we are iterating the for loop for each member of the list item. And the print statement will prints out the day name.

 



Loop control statements

Python loop control statement will change the execution of a loop. For example on some condition we want to stop or continue the loop iteration. In python we have “break” and “continue” key word to stop/break or continue the loop execution.

Break statement

The break statement is used to break the execution of a loop if a condition is met.

 def main():

# use of break loop control statement

for x in range(5,10):

if x == 7: break

print(“x: “,x)

if __name__ == “__main__”: main()

 

Note:

n above code If x is equal to 7 than break, the break statement is here will cause to terminate the for loop and fall through the next block of code. When we run this program this will print out only 5 and 6 because when the condition is met the break statement will kicks out the loop.

Output will be like this

Continue statement

The continue statement skips the rest of the statement when it is encounter.

 
def main():

# use of continue loop control statement

for x in range(5,10):

if (x % 2 == 0): continue

print(“x: “,x)

if __name__ == “__main__”: main()

 

Note: In above code our condition is if x modulus 2 is equal to 0 or in other word take x and divide it by 0 if the value left over is 0 than continue. Its mean if the condition is met don’t go to the next line (in our case the next line is print statement) just go back up to the start of the loop. In simple word if I come across in the even number doesn’t print out that number. The continue statement will skip the next line if the condition will met. This will print out 5,6 and 9.

The Output Will Be Like This:

 

To know more about us


Leave a Reply

Your email address will not be published.