-3

I am going to school to become a programmer, and I am doing some homework and I have a question. It wants me to give an example of why we would use loops in the real world. And I was thinking about smart phones, I have an app on here that tells me the temperature outside, and if the temperature changes, it would tell me. Is that an acceptable answer to this question?

closed as too broad by GrandmasterB, ratchet freak, user40980, GlenH7, gnat Feb 5 '15 at 5:52

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

5

Loops are what make computers interesting machines. Without loops, computers would just be really sophisticated adding machines, and not much else.

The very first computing course I took in high school, we didn't even have a computer... We just had this adding machine that was the size of a suitcase and weighed about 100 pounds. It did what you might expect an adding machine to do: basic arithmetic. And that's all it did.

Until the day I discovered you could write a loop.

I told the machine to multiply 2 by itself, and to repeat that operation in a loop. The machine suddenly came alive, printing ever increasing numbers on a ribbon of paper. Ka-chunk, ka-chunk, ka-chunk. After about 30 loops, it suddenly stopped. The machine had overflowed.

I was hooked.

Almost everything substantial that happens in a computer happens in a loop. Your temperature example could poll some sensor somewhere every 10 seconds to see if the temperature has changed. That's a loop. A report that writes line items on an invoice is a loop: one iteration per line item. The screen you're looking at runs in two loops: one for the horizontal pixels, and one for the vertical ones, refreshing the screen roughly 60 times per second.

In the real world, almost everything is cyclical, and therefore runs in a loop. The vibration of atoms, the rotation of the engine in your car (or the tires), eating food one spoonful at a time, etc.

1

It seems to me that this would be a more clear example:

for student in students:
    grade_exam_of(student)

It loops, the body of each loop is identical, and it is in a domain well understood by your instructor.

0

How about the simple act of taking a walk? You know how to move your left leg. You know how to move your right leg. Now do that repeatedly (via a loop) until you arrive at your destination.

Not the answer you're looking for? Browse other questions tagged or ask your own question.