Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does the inner loop run first?

Hi I am trying to understand the order of nested loops. The first loop through the outer loop returns 13 16 19 113 116 119
It looks like it starts with the outer loop first then fully go through the inner loop iterations and back to outer loop until iterations is that correct?
Is this the case for most nested loops?
Thanks in advanced!

i1 = 1
    while i1 < 19:
        i2 = 3
        while i2 <= 9:
            print(f'{i1}{i2}', end=' ')
            i2 = i2 + 3
    i1 = i1 + 10

When I went through the 2nd iteration of the outer loop i2 overwrites back to 3 (instead of 6) which is what is confusing me and is incorrect

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you are correct about the order of execution in nested loops. The outer loop will iterate first, and for each iteration of the outer loop, the inner loop will complete all its iterations before moving to the next iteration of the outer loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading