How to make loop move on to another loop once the first loop is done

Viewed 122

I just want to know the logic within this problem. I just want my program to execute another loops once the first loop was done.

for i in range(1,5):
    for j in range(1,5):
        print(i*j,end="\t")
    print("\n")

After the code above was done I want my program to perform another loop which is this one:

for i in range(5,1,-1):
    for j in range(5,1,-1):
        print(i*j,end="\t")
    print("\n")

1 Answers

Just order them how you want them executed (in this case, one after the other). Python, among many other languages will just execute code sequentially unless you specifically introduce control structures like loops:

for i in range(1,5):
    for j in range(1,5):
        print(i*j,end="\t")
    print("\n")
for i in range(5,1,-1):
    for j in range(5,1,-1):
        print(i*j,end="\t")
    print("\n")

Just be aware that the second loop isn't a perfect reversal of the first. Since ranges are inclusive at the start and exclusive at the end, the tow complementary loops would be:

for i in range(1,5):        # 1..4 inclusive
for i in range(4,0,-1):     # 4..1 inclusive
Related