How does nested for loop works, can you explain with dry run?

  • Context: MHB 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Explain Loop Works
Click For Summary
SUMMARY

The discussion focuses on the implementation of a nested for loop in Python to identify prime numbers up to a user-defined limit. The original code fails to print the number 2 due to incorrect indentation of the else statement associated with the inner loop. The corrected version ensures that the else statement aligns properly with the if statement, allowing for accurate identification of prime numbers. The expected output for n=5 is 2, 3, 5, while the flawed code produces incorrect results.

PREREQUISITES
  • Understanding of Python syntax and indentation rules
  • Familiarity with control flow statements (for loops, if statements)
  • Basic knowledge of prime number identification algorithms
  • Experience with Python's input function and range function
NEXT STEPS
  • Study Python's control flow and indentation best practices
  • Learn about prime number algorithms, including the Sieve of Eratosthenes
  • Explore debugging techniques for Python code to identify logical errors
  • Investigate the use of functions to modularize code for prime number generation
USEFUL FOR

Python developers, educators teaching programming concepts, and anyone interested in understanding nested loops and prime number algorithms.

shivajikobardan
Messages
637
Reaction score
54
Here is the code that I am talking about-:
Code:
n=int(input("Enter a number"))
for num in range(2,n+1):
    for i in range(2,num):
        if(num%i==0):
            break
    else:
        print(num,end="")

If I give n=5 output should be 2,3,5.

Here is my dry run. Everything is fine except for 2 where I am not getting 2 as output. What has gone wrong here? I don't understand what is gone wrong here?
1-min.jpg
 
Technology news on Phys.org
Your else statement is not properly indented to align with the if statement. I believe it should be like this.

Code:
n=int(input("Enter a number"))
for num in range(2,n+1):
    for i in range(2,num):
        if(num%i==0):
            break
        else:
            print(num,end="")
 
If n= 5 then the "outer loop" runs for num= 2 to num= 6.

For num= 2, the "inner loop" runs for i= 2 to i= 2 (in other words, i is only 2). 2 divided by 2 is 1 with no remainder so 2%2= 0 so "break" and go on to

For num= 3, the "inner loop runs for i= 2 to i= 3. 3 divided by 2 is 1 with remainder 1 so 3%2= 1 so print 3. 3 divided by 3 is 1 with no remainder so 3%3=0 so "break" and go on to

For num= 4, the inner loop runs for i= 2 to 4. 4 divided by 2 is 2 with no remainder so 2%2= 0 "break". 4 divided by 3 is 1 with remainder 1 so print 4. 4 divided by 4 is 1 with remainder 0 so 4%4= 0 "break"

For num= 5, the inner loop runs for i= 2 to 5. 5 divided by 2 is 2 with remainder 1 so 5%2= 1 so print 5. 5 divided by 3 is 1 with remainder 2 so 5%3= 2 so print 5. 5 divided by 4 is 1 with remainder 1 so 5%4= 1 so print 5. 5 divided by 5 is 1 with remainder 0 so break.

For num= 6, the inner loop runs for i= 2 to 6. 6 divided by 2 is 3 with no remainder so 6%2= 0 so break. 6 divided by 3 is 2 with no remainder so 6%3= 0 so break. 6 divided by 4 is 1 with remainder 2 so 6%4= 2 so print 6. 6 divided by 5 is 1 with remainder 1 so 6%5= 1 so print 6. 6 divided by 6 is 1 with no remainder so 6%6= 0 so break.

The result will be to print "3 4 5 5 5 6 6".
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K