Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
Choose anything you want to do several times, e.g. to several things with perhaps minor variations. Voila, you got a loop. –  delnan Apr 25 '11 at 12:20
5  
Is this homework? Tag it accordingly. –  JeffO Apr 25 '11 at 12:45
2  
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

9 Answers 9

up vote 11 down vote accepted

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:

show_friend($user->friends[0]);
show_friend($user->friends[1]);
show_friend($user->friends[2]);
// and so on, and on, and on

Or you could use a for loop:

foreach(var $friend in $user->friends) {
  show_friend($friend);
}
share|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
5  
@Dylan while (awake) { check_facebook(); } –  CPerkins Apr 25 '11 at 17:45

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.

I don't understand what you are asking exactly. The Facebook example you used wouldn't be a case for a loop. This question seems like homework.

A loop would apply to a situation where you have to do something multiple times for a specfic amount of time while a condition is true and/or false.

share|improve this answer
1  
Homework indeed! –  Deckard Apr 25 '11 at 12:20
1  
That example is based on if/else statement. –  Dylan Cole Apr 25 '11 at 12:22
    
I sort of got it was an example. Why you added "Do we use if/else statement behind situation? Please correct me if I'm wrong." was what confused me. –  Ramhound Apr 25 '11 at 12:28

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.

share|improve this answer

Please read this very carefully.

http://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD316.2.html

"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.

share|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.

share|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++)
{
  generateHtmlFor(friendList[i]);
}

or alternately

i = 0;
while (friendList[i] != NULL)
{
  generateHtmlFor(friendList[i++]);
}

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.

share|improve this answer
    
Perfect! –  Dylan Cole Apr 25 '11 at 17:54

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.

share|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.

share|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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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