WHILE Loops: Changing the incremental step

Hi there,

I am new to Python and I have been getting a bit confused with WHILE loops. I am wondering if the incremental step in WHILE loops, i +=1 (if temporary variable = i) can be changed to, say, i += 2. Also, under what circumstances might we want to completely leave out the incremental step and simply have a WHILE loop execute ‘forever’, can anyone give an example?

For some reason, I always forget to type out the incremental step, assuming that my condition will naturally stop executing when False. :slight_smile: I guess I need to step up my game and get more practice!

Thanks everyone!


1 Like

Yes you can change the increment to whatever you like, you can do away with the increment (there’s no explicit requirement for an incrementing value) and continuous loops are as simple as while True: (e.g. expecting user input or in combination with some kind of timer).

The loop will finish when the condition evaluates to False though. It should be noted that the condition isn’t repeatedly checked whilst running, only on every new iteration. You could look into using break to escape the current loop if necessary but consider if it affects how easy it is to read and understand your code.

Thanks very much for the prompt! :slight_smile: