Python Forum
Why is this code not an infinite loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this code not an infinite loop?
#1
Hi,

Why is following code not infinite loop when I am setting the loop's counter variable i to 0 before it comes to an end?

def f():
	for i in range(3):
		for j in range(3):
			input(str(i)+str(j))    # just for seeing output one by one ...
		i = 0
	return
Please give academic style answers.
Reply
#2
this is for loop, not while loop. There is no condition to check and decide to continue or not the loop, i.e. the value of i is irrelevant if there will be next iteration or not. you are free to use/assign new value to i in the loop body, e.g.
for i in range(3):
    i *= 2
    print(i)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Infinite Loop with my code TheGrado 9 166 Mar-24-2026, 06:16 AM
Last Post: Pedroski55
  How to convert while loop to for loop in my code? tatahuft 4 1,659 Dec-21-2024, 07:59 AM
Last Post: snippsat
Shocked Why this code does not go into an infinite loop? 2367409125 2 2,225 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 2,548 Aug-22-2022, 02:57 AM
Last Post: deanhystad
  Infinite loop problem Zirconyl 5 5,471 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  using 'while loop' output going into infinite loop... amitkb 2 3,402 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Infinite loop not working pmp2 2 3,138 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Appending to list not working and causing a infinite loop eiger23 8 7,758 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Server infinite loop input from user tomislav91 1 5,717 May-23-2019, 02:18 PM
Last Post: heiner55
  Code Change Help: Infinite Loop Error bindulam 2 3,690 Mar-10-2019, 11:15 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020