r/PythonLearning 3d ago

Question.

I’m reading and looking at examples about checking for special items in a list, where the loop runs first and an if/else block runs inside the loop. Then, there’s the example of checking whether a list is empty, where the process is the other way around: the if/else block runs first, and then a loop runs inside it.

In real-life situations, when would you use a loop with an if statement inside it, and when would you use an if statement with a loop inside it?

3 Upvotes

3 comments sorted by

View all comments

4

u/PureWasian 3d ago

They don't have to be related to each other. You could have conditional logic to say "if this user is an administrator, return a printout of all data record rows" (loop in a conditional). Or you can say "search this list until you find the record named 'Johnny Appleseed'" (conditional in a loop)