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

In summary, the code provided is used to find prime numbers up to a specified number, with the limitation being that the number must be greater than 2. The issue with not getting a 2 as an output is due to an indentation error in the else statement. The code correctly prints all prime numbers up to the specified number, but also prints some non-prime numbers due to the indentation error.
  • #1
shivajikobardan
674
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
  • #2
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="")
 
  • #3
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".
 

1. How does the nested for loop work?

A nested for loop is a loop within another loop. This means that the inner loop will run completely for every iteration of the outer loop. This allows for multiple levels of iteration and is commonly used for tasks such as iterating through a two-dimensional array.

2. Can you explain how to perform a dry run of a nested for loop?

To perform a dry run of a nested for loop, you can manually go through each iteration of the outer loop and then go through each iteration of the inner loop. Make sure to keep track of the variables and their values at each step to understand how the loop is working.

3. How do the variables in a nested for loop change with each iteration?

The variables in a nested for loop change with each iteration of the inner loop. This means that the outer loop will stay at the same value until the inner loop has completed all of its iterations, and then the outer loop will move on to the next iteration.

4. What happens if there are multiple nested for loops?

If there are multiple nested for loops, the innermost loop will run first, and then the outer loops will run in order. This means that the innermost loop will complete all of its iterations before the next outer loop begins its next iteration.

5. How can I use a nested for loop to solve a problem?

A nested for loop can be used to solve problems that require multiple levels of iteration, such as searching through a two-dimensional array or performing calculations on multiple sets of data. By understanding how the loop works and keeping track of the variables, you can effectively use nested for loops to solve a variety of problems.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
9
Views
704
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
4
Views
996
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
994
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top