[Python] Basic Grammar---5, Loop Statement

1.5 Loop statement

Loop mainly solves the problem of repetitive code

Divided into two categories:

  • Known times: 10 steps forward

  • Known conditions: walk forward until you hit the wall

Four elements of the cycle:

  • Initialization of the loop

  • Continuation condition of the loop

  • Loop body

  • Cycle step-period-spacing

for loop

In Python, it revises the for loop, which is mainly used when the number of loops is known

After the for loop is a known sequence

# Python
for i in range(1,11): # [1,11)
    print(i)

// C/Java
for (int i = 1; i <= 10; i++) {
    print(i);
}

while loop

循环的初始化
while 循环的继续条件:
    循环的循环体
    循环的步长
while True:
    循环体

 

Guess you like

Origin blog.csdn.net/trichloromethane/article/details/108267350