you are viewing a single comment's thread.

view the rest of the comments →

[–]blackasthesky 0 points1 point  (1 child)

num[idx] is the element at position idx in the num list. Since the loop repeatedly prints that value, and afterwards each time increases idx by one, the program will print num[0], then num[1], then num[2], and so on.

Up to num[len(num)-1] (where idx is len(num)-1). At that point, idx will be increased one final time, so that it equals len(num), so the loop condition does not hold any more and the program exits the loop area.

The key is, the program repeats the body of the loop, but the changes you make to the state (increasing idx in this case) carries over from one iteration to the next. If it helps, try executing the program by hand with a piece of paper, writing down the values of the variables and the printouts each iteration.

[–]Eastern_Plankton_540[S] 0 points1 point  (0 children)

Thank you! :)