Creative Communities of the World Forums

The peer to peer support community for media production professionals.

  • Posted by Lord Scales on May 2, 2007 at 8:30 pm

    Hi there! Looking to some examples here and in MotionScript.com, I finally got waht the “for” does.

    But I need your help to understand better the difference between for and while. Could you give a simple (but really simple :]) example to show the utility of for and while and their differences?

    Thanks for helping and sorry by any disturbing.

    Lord Scales

    Lord Scales replied 19 years ago 2 Members · 2 Replies
  • 2 Replies
  • Darby Edelen

    May 3, 2007 at 4:23 am

    The statement:

    for(x = 1; x < 3; x++){ //do things here }

    is equivalent to the following:

    x = 1;
    while(x < 3){ //do things here x++; }

    In the for() loop the first statement sets a variable equal to some starting value, when the second statement evaluates to false (or 0) the loop will stop, the last statement is run after each loop. So in a standard for loop you would start with a value, compare that value to some other value, then change that value after looping through some code.

    The example above sets the variable x to 1 to start, then checks if x is less than 3, since x is equal to 1 and is less than 3 the loop goes into its first iteration. After this first iteration the value of x is increased to 2 (x++ is the same thing as x = x + 1). Then if x is still less than 3 (which it is) it loops again.

    A while() loop is a little more straightforward. While the statement in the parenthesis (in this case x < 3) is true (or equal to some non-zero value) the loop will continue to loop indefinitely. So if we set x equal to 1 before the loop begins, then check to see if x is less than 3 to begin the loop, then increment x at the end of every loop it is effectively the same thing as our previous for() loop. I hope that wasn't too confusing, if you have any other questions or points to clarify let me know. Also, its been a little while since I've taken a programming course so if I left anything out... shame on me, and please correct me =)

  • Lord Scales

    May 3, 2007 at 9:12 pm

    Thanks very much! I got that and I am going to test it here.

    Thanks again!

    Lord Scales

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy