nested loop
The format of applying the while loop format in a while loop is as follows:
while conditional expression 1:
while conditional expression 2:
loop body 2
loop body 1
Apply the for loop format in a for loop:
for iteration variable in object 1:
for iterates variable 2 in object 2:
loop body 2
loop body 1
Apply the for loop format in the while loop:
while conditional expression:
for iterates the variable in object:
loop body 2
loop body 1
Apply the while loop format in a for loop:
for iterates the variable in object:
while conditional expression:
loop body 2
loop body 1
Use the break statement format in a while statement:
while conditional expression 1:
execute code
if conditional expression 2:
break
Use the continue statement format in a while statement:
while conditional expression1;
execute code
if conditional expression 2:
continue
Use the break statement to completely terminate the loop:
Use the continue statement to jump directly to the next iteration of the loop, the continue statement will only skip the remaining statements in the innermost loop.
for i in range(1,10): #输出9行
for j in range(1,i+1): #输出与行数相等的列
print(str(j) + 'x' + str(i) + '=' + str(i * j) + '\t', end='')
print('')
Two for loops are used to print out 9 columns, i+1 is for example 1*9=9, 2*9=18, 3*9=27 plus one in turn. The second loop can control the value of the number of columns in the multiplication table to be greater than the number of columns in the same row. It can be seen that the second loop is based on the first loop.