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

#### terabaaphoonmein

Joined Jul 19, 2020
90
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?

Last edited by a moderator:

#### KeithWalker

Joined Jul 10, 2017
2,311
That is a very cryptic piece of code. Give us some clues.
What programming language is it written in? I am a dinosaur so I don't recognize it. Maybe I am not the only one because no one seems to want to answer this post.
Can you add comments to the code so I may understand what the statements are doing?

#### MrSalts

Joined Apr 2, 2020
1,584
@KeithWalker
It's written Python. Sometimes Python is so abbreviated in format that it becomes more difficulty to read instead of easier to read.
@terabaaphoonmein

Code:
n=int(input("Enter a number")). ## any integer (n) can be entered
## (it should be checked and
## error issued if negative
## (not that is not done here.

for num in range(2,n+1):           ## from 2 until (n+1) are processed as the variable "num"
for i in range(2,num):            ## from 2 until "num" values are processed as the variable "i"
if(num%i==0):                   ## when the modulo division result of num/i == 0, end the loop
break
else:
print(num,end="").            ## otherwise, print the current value of num.  the end="" tag means no carriage return for the cursor, just keep printing the consecutive results. You currently have no space between the results so all results would be grouped together.  This "end" function does not seem to work if using Idle as the IDE. But it will work on a console terminal session.

#### djsfantasi

Joined Apr 11, 2010
8,258
That is a very cryptic piece of code. Give us some clues.
What programming language is it written in? I am a dinosaur so I don't recognize it. Maybe I am not the only one because no one seems to want to answer this post.
Can you add comments to the code so I may understand what the statements are doing?
Thanks Keith! I didn’t answer because I didn’t recognize the code. With all the languages I use, this code breaks all common syntax of all of them. So, it makes no sense to me. My response would be, of course it won’t work.

#### MrSalts

Joined Apr 2, 2020
1,584
Thanks Keith! I didn’t answer because I didn’t recognize the code. With all the languages I use, this code breaks all common syntax of all of them. So, it makes no sense to me. My response would be, of course it won’t work.
Python is just weird. Indentation is used instead of brackets. If you want a range, you just type range.

#### MrSalts

Joined Apr 2, 2020
1,584
By the way, range must be one step larger than the last number you want to test. If you want 0 to 2, you must enter range(0,3) - or the n+1 as the OP uses m

#### KeithWalker

Joined Jul 10, 2017
2,311
No wonder I didn't recognize it. I have programmed in very many different languages in the past but think I will stay with the one I hate the most; C++.
I will let you guys figure this one out.