FOR loops in C, what this all mean.

Posted by Robert Powell on Wed 19 Jan 2005 09:33 AM — 7 posts, 24,722 views.

Australia #0
Hi all, been a while since i have posted but i have a question about a section of code that i dont realy get. i was wondering if anyone can help me make sence of this for loop i dont realy get whats going on here,

for (x=1; x<=mnum; x++)
        {
          material = get_obj_carry (ch, arg1 );
          if (material == NULL)
            {
              send_to_char( "You didn't realize it when you started, but you haven't enough material\n\r", ch);
              return;
            }


Im sort of lost, any explaination would be great.
USA #1
Well, for starters, it'd help if you posted the whole loop. :P
Australia #2
Sorry thought that i posted all that was relevent, its from a snippet do_tailor makes clothes and stuff,

for (x=1; x<=mnum; x++)
        {
          material = get_obj_carry (ch, arg1 );
          if (material == NULL)
            {
              send_to_char( "You didn't realize it when you started, but you haven't enough material\n\r", ch);
              return;
            }
          extract_obj( material );
        }
      obj_to_char( clothing, ch );
      send_to_char("You snip and sew, creating a new piece of clothing.\n\r", ch);
      act( AT_ACTION, "$n snips and sews creating a new piece of clothing.", ch, NULL, NULL, TO_ROOM );

      xnum = number_range(0, 100);
      if (xnum < 20) /*did the sewkit break?*/
        {
          extract_obj( sewkit );
          act( AT_ACTION, "$n pulls too hard on the needle and it breaks!", ch, NULL, NULL, TO_ROOM);
          send_to_char( "You pull too hard on the needle and it breaks!\n\r", ch );
        }
      learn_from_failure( ch, gsn_tailor );
      return;
    }


The stuff below the first post i understand, if there is more to a for loop than i have posted then i will post the whole snippet
USA #3
What exactly are you not understanding? For loops do a certain thing a specific number of times. More precisely:

for ( start-instruction; condition; end-of-loop-instruction )
{ ... do something ... }

So, in your code, everything inside the for loop will be executed 'mnum' times.

As for what it actually does/is used for, that depends entirely on the context of the for loop. Seems to me that it's trying to create an object, mnum times, but that's just total guessing; I'd need more information about the whole thing and its intended purpose to know for sure.
Australia #4
mnum is the number of corpses that are needed to craft a particular item, somewhere above that material is
given the value of vnum 10 for corpses, so i sort of get it now, it loops through untill either mnum = x and you have enought material or it exits because mnum dont = x as you dont have enought material.

Not comming from a C background the for loop just plain looked funny the way its written
for (x=1; x<=mnum; x++)
USA #5
Quote:
for (x=1; x<=mnum; x++) { ... }
That means:
set x to 1
while x <= mnum
{
     ...
     x = x + 1
}

Does that clear things up?

It exits when x <= mnum, or, if you ever don't have a material.

What it seems to be doing is to consume all the materials, and then to actually create the clothing. It's a kind of mean implementation, since if you don't have enough, it still consumes as many as it can... :)
Australia #6
Yes thats exactly how it works trashes everything you have even if you dont have enought to do the job, now that i better understand the way the for loop works i can go aboout changing this to work the way i would like it to.