Loop statements are used to repeatedly execute a piece of code. The syntax is as follows: for loop: used to iterate each element in a sequence or range. while loop: used to repeatedly execute code while a condition is true. do-while loop: Similar to a while loop, but the code is executed first and then the condition is checked.
Writing of loops
The loop statement is a statement that controls the program flow in programming, which allows code blocks Repeat a specified number of times or until a specific condition is met. In Python, you can use the following three types of loops:
for loop
for in < Iterable object>
while loop
while
range() function
for in range(, < end>, )
Example:
# for 循环 for item in [1, 2, 3, 4, 5]: print(item) # while 循环 count = 0 while count < 5: print(count) count += 1 # range() 函数 for number in range(1, 10, 2): print(number)
Note:
break
statement within the loop body to exit the loop.continue
statement in the loop body to skip the current iteration and continue with the next iteration.The above is the detailed content of How to write loop. For more information, please follow other related articles on the PHP Chinese website!