I'm learning C as my first programming language. I'm confused on how do loops work in real-life. In programming every function has a reason.

example - if/else statements are used if we have conditions. Nesting if/else statements are used if we have conditions inside a condition(more condition).

I am going to try my best answering this using a real-life problem. I tend to understand better using Facebook as an example.

Let's say a user is signing up for Facebook. We don't want users signing up of age less than 13 years old. If users age is less than 13, it should terminate the sign up page and give a reason saying "Sorry, you're too young". Do we use if/else statement behind situation? Please correct me if I'm wrong.

Now, Loops are used for shortening up the program/application. What's the real-life analogy behind this? Using Facebook as an example.

Can 'News Feed' be a strong example of using Loops?

  • 1
    Choose anything you want to do several times, e.g. to several things with perhaps minor variations. Voila, you got a loop. – user7043 Apr 25 '11 at 12:20
  • 6
    Is this homework? Tag it accordingly. – JeffO Apr 25 '11 at 12:45
  • 3
    Why the requirement that Facebook be used as an example? There are plenty of other good examples not related to Facebook. – FrustratedWithFormsDesigner Apr 25 '11 at 13:25
  • Facebook is something that we use now in our daily lives. I tend to understand better using these examples. – Dylan Cole Apr 25 '11 at 13:28
  • 1
    Notice that a program without any repetitive construct (loops, or perhaps recursions) will stop very quickly (e.g. in less than a second, assuming a billion machine instructions in memory). So programs without loops are boring and are not doing much. – Basile Starynkevitch Apr 25 '15 at 17:58

Using Facebook as an example. Click to view your friends list. Everybody has a variable number of friends, so you need code to iterate over each friend to display their photo, name, and profile link. A novice might try to achieve this through copy/paste programming:

// and so on, and on, and on

Or you could use a for loop:

foreach(var $friend in $user->friends) {
| improve this answer | |
  • Thats the type of answer I was looking for! Is there an analogy behind using do_while loops? in What situations can it be used? (Using facebook example). – Dylan Cole Apr 25 '11 at 12:32
  • 8
    @Dylan while (awake) { check_facebook(); } – CPerkins Apr 25 '11 at 17:45

Please read this very carefully.


"In order to meet these objections, we introduce a repetitive connective which, again for historical reasons, is written in two parts "while...do". Using this connective we can write the algorithm:"

What follows is a real-life loop. Explained in detail.

| improve this answer | |

Take for example tagging your friend when you upload photos. What you are doing there is iterating over a collection of photos and doing something on each one of them.

| improve this answer | |

Can 'News Feed' be a strong example of using Loops?

I would hope not. If it is pulling data from a database, then it should be using set-based logic not loops.

| improve this answer | |

I'm learning C as my first programming language.

I'm so sorry. C is a great language for a lot of applications, but teaching isn't one of them.

As far as how loops would be used for a Facebook-style application, it would mainly be for things like displaying a sequence of entries, such as your list of friends or people you may know. You'd retrieve the list of friends from the database into some sort of list or array structure, and then use a loop to walk through the list and generate the HTML for display, something like

Friend *friendList = getFriends(); 
size_t i;

for (i = 0; friendList[i] != NULL; i++)

or alternately

i = 0;
while (friendList[i] != NULL)

Not that the above code looks anything like real Facebook code. I'm pretty sure Facebook isn't written in C.

Logically speaking, you'd use a for loop to iterate over a finite sequence of elements (such as in an array), whereas you'd use a while loop to iterate while a condition was true (and a do-while loop to execute the sequence at least once and continue to execute while the condition was true). C kind of blurred the line between for and while loop structures, and there are places like this where you could use either one equally well. We know the list is finite, but we don't know how long it is (you can't tell from a pointer alone how many elements are in the sequence), so we use a protocol where the last element in the list must be set to NULL.

| improve this answer | |

Typically, your do/loop would be used in instances where you don't necessarily have a count. Say, for instance, you have a collection that you move through, but that collection can be added to (or removed from) at any time.

Possible real life scenario - some sort of auto-reading application that reads all new messages out loud, moving to the next message once one is read.

In this case, a for/next on an index of the original count would not get any new messages that are added. Once the original count is hit, it ends. Also, if the ability was provided to remove a message that was previously sent out, the for/next would fail, because you'd be trying to iterate to a message that no longer exists (index out of bounds).

A Do/Loop, however, can walk over each of these, and gracefully leave when there's nothing left to display.

do while there are more messages read message loop end

Sorry, I can't think of a better example right now.

| improve this answer | |

Consider any sequence of events in your life that are repeated and you'll see an example of a loop. For example, buses in transit systems tend to be an example of a loop where the bus goes around and around. Another example would be to consider an ATM where while you are performing transactions, the software of the machine is in a loop to process transaction after transaction until you acknowledge that you have no more to do.

Another example would be to consider cooking a dozen hot dogs where there will likely be some tasks in this that are repeated if you document the steps and then simplify the algorithm if you wanted to teach this to another person.

To give a Facebook specific example, consider how Facebook updates your Friend requests/Messages/Notifications at the top of the page. This would generally use a loop as you wouldn't want to be requesting an update constantly yet you do want the browser to check periodically for an update.

| improve this answer | |

An if condition can be thought of as a degenerate while loop. To illustrate this and other looping fundamentals, I offer this awesome video: Prime Factors Kata (Annotated). It's not Facebook, but I'm sure you will be able to understand what's going on.

| improve this answer | |

There are, including this one, 10 answers on this page as I post. So how would you display them?

if (answerCount >= 1) displayAnswer (1);
if (answerCount >= 2) displayAnswer (2);
if (answerCount >= 3) displayAnswer (3);
if (answerCount >= 4) displayAnswer (4);
if (answerCount >= 5) displayAnswer (5);
if (answerCount >= 6) displayAnswer (6);
if (answerCount >= 7) displayAnswer (7);
if (answerCount >= 8) displayAnswer (8);
if (answerCount >= 9) displayAnswer (9);
if (answerCount >= 10) displayAnswer (10);
if (answerCount >= 11) displayAnswer (11);
if (answerCount >= 12) displayAnswer (12);

So how would you display a page with 13 answers? How would you display a page with 100 answers? That's where you use a loop.

| improve this answer | |

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