Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ FOR loops in C, what this all mean.

FOR loops in C, what this all mean.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Robert Powell   Australia  (367 posts)  Bio
Date Wed 19 Jan 2005 09:33 AM (UTC)
Message
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.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Wed 19 Jan 2005 09:44 AM (UTC)
Message
Well, for starters, it'd help if you posted the whole loop. :P

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #2 on Wed 19 Jan 2005 10:53 AM (UTC)
Message
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

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Wed 19 Jan 2005 11:16 AM (UTC)
Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #4 on Wed 19 Jan 2005 08:08 PM (UTC)
Message
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++)

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #5 on Wed 19 Jan 2005 08:23 PM (UTC)
Message
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... :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #6 on Thu 20 Jan 2005 03:14 AM (UTC)
Message
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.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


18,465 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.