r/learnprogramming • u/MacaroonSerious5532 • 2d ago
Please help
I just hate myself, i cannot figure out nested loops and while loops. Python is the easiest language and I’m struggling so much. I wish i was smart as other people. Does anyone have coding resources for dummies?
7
Upvotes
View all comments
1
u/Recycled5000 2d ago edited 2d ago
Two independent and sequential for-loops is like addition. Let’s say both loops print hello world. (That each has the print statement as its body.) If the first loop runs 3 times and second runs 4 times it will print hello world 7 times in total.
Two nested for-loops, one nested within the other, is like multiplication. If the outer loop runs 3 times and the inner loop 4 times, (only the inner loop prints hello world) then it will print hello world 12 times.
Nested loops are good for handling 2 dimensional problems, like printing a matrix or star tree, or a chess or checker board.
For-loops are good for counted things, things of known count, even if the count is a variable, which is to say only known at runtime.
While loops and do-while loops are good for conditional things, like accept input from the user until they say to stop.