HISE Logo Forum
    • Categories
    • Register
    • Login

    scope of loop variables

    Scheduled Pinned Locked Moved Scripting
    3 Posts 2 Posters 591 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      CherryAnt
      last edited by

      What is better:
      for (var i = 0; i < 200; i++){
      or
      for (i = 0; i < 200; i++){
      and why i cant use:
      for (local i = 0; i < 200; i++){

      What is the scope of "i" on that bucles?
      Thanks

      1 Reply Last reply Reply Quote 0
      • Christoph HartC
        Christoph Hart
        last edited by

        I think I am following standard Javascript behaviour here which makes the scope global. There‘s no difference between for(i=0 and for(var i=0), in fact this is the only case where I allowed the parser to accept unqualified assignments of variables.

        My recommendation is to define one reg variable called i in the onInit callback and reuse it (that‘s what I am doing when I need to iterate using this loop). Bear in mind that the other for loop we discussed yesterday is much more faster and should be preferred if possible.

        C 1 Reply Last reply Reply Quote 1
        • C
          CherryAnt @Christoph Hart
          last edited by

          @christoph-hart thanks a lot

          1 Reply Last reply Reply Quote 0
          • First post
            Last post

          39

          Online

          1.7k

          Users

          11.9k

          Topics

          103.5k

          Posts