Results 1 to 4 of 4

Thread: For loops: when to use 0 and subtract 1

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Semisalis
    Join Date
    Sep 2011
    Location
    New Zealand
    Posts
    412

    Default For loops: when to use 0 and subtract 1

    Very basic question. I see a lot of Lua script fragments on the forum have for loops with 0 to x-1 ranges. Is there a rule on when to use the and when to use 1 to x

    My guess would be 0 to x-1 for any table generated with a CA supplied function (ie from C+) and 1 to x if i make my own table in lua or any tables defined by CA in lua rather than C+

    I am just starting modding Rome 2. I have done some basic fiddling of values with Lua in another game (Dawn of war 2) but I am self-taught and not a programmer so please forgive basic questions
    ~ Too soon old, too late smart ~

  2. #2
    Karnil Vark Khaitan's Avatar Praepositus
    Join Date
    Jan 2010
    Location
    DaneMark
    Posts
    5,031

    Default Re: For loops: when to use 0 and subtract 1

    If you have made the loop's "rule" that it will stop "looping" then the counter reach Zero, it will quit the loop and return the "return Value"

    Think of it as a rule, the system is ordered to stop then it reach zero, and will then continue with the rest of the program, but not that loop because it is done with it.

    this is java:

    while (x > 0)
    {
    x - 1;
    System.out.println ("player " + x + "'left");
    }
    System.out.println ("done");

    havne't tested the code, but hope it crates a better understanding of it all.

    Im the Knight in Sour Armor http://tvtropes.org/pmwiki/pmwiki.ph...ghtInSourArmor
    Rainbow Darling rainbows Darling. Darling Rainbows!!!!!
    but on the same time modder with my first mod for Rome 2!http://steamcommunity.com/sharedfile.../?id=286218945
    Hey Sparkle Sparkle Sparkle!
    https://www.youtube.com/watch?v=LDULtV9U2kA
    Quote Originally Posted by riskymonk View Post
    yea but mods are created by fans of the series. Games are created by university students who might not necessarily know or play the games/series they're working on

  3. #3
    Semisalis
    Join Date
    Sep 2011
    Location
    New Zealand
    Posts
    412

    Default Re: For loops: when to use 0 and subtract 1

    Thanks, I try and avoid while loops because of the risk of an infinite loop and my brain doesn't like them.

    I was more thinking of a for, do loop through a table or vector. I think in C+ the first variable in a table has the key zero whereas in Lua the first variable has the key 1. Maybe i have raised a non-issue

    Cheers
    KK
    ~ Too soon old, too late smart ~

  4. #4

    Default Re: For loops: when to use 0 and subtract 1

    Most time, using 0 to x-1 is convenient, the number of elements in array usually begin with 0 not 1, e.g when you use "military_force_list:item_at(i)" to do a series of operations, "military_force_list:item_at(1)" is not the first

    one to be handled.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •