Announcement

Collapse
No announcement yet.

Problem with "for" statement - current understanding

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with "for" statement - current understanding

    I am rather new to EFS code, but have a long background in developing and debugging applications in various languages (Incl. VB, Pascal, COBOL, dBASE).

    In developing some indicators recently using Version 7.9 (Build 719) I came across a problem with the execution of the "for" statement.

    I tried everything to get code to work, but statements after "for" did not seem to execute (despite syntax check being ok).

    I then replaced the "for" construct with an equivalent "while" construct, and the code executed perfectly!

    I have just completed the coding of another indicator (a morphed CCI calculation) and had exactly the same problem.

    If this is universal, IT IS VERY SERIOUS!!!

    If anyone can confirm, or has found why this problem occurs, I would be most grateful.

  • #2
    griesha30246
    FWIW I use the "for" statement regularly without any problems.
    You may want to post [a representative example of] the code that is not working for you
    Alex

    Comment


    • #3
      Possible "for" problem - sample code attached

      The following code is an extract of the test code I used to demonstrate (to me) that the "while" construct worked, but the "for" construct did not.

      If there is something I have missed, please advise.

      function Fn_InitVars() {
      // Note: vCalcLength is a global variable - value 50.
      // aPrevPrice & aPrevDevn are global Arrays.
      // Other variables are not particularly relevant
      var i;
      var vCurrClose = close();

      // This works ok - get debug lines output.
      i = 0;
      while (i <= (vCalcLength - 1)) {
      aPrevPrice[i] = vCurrClose;
      aPrevDevn[i] = 0;
      debugPrint("WHILE: i = " + i + " Price = " + vCurrClose + "\n");
      i++;
      }

      // This fails to execute - no debug text is output.
      for (i = 0; i == (vCalcLength - 1); i++) {
      aPrevPrice[i] = vCurrClose;
      aPrevDevn[i] = 0;
      debugPrint("FOR: i = " + i + " Price = " + vCurrClose + "\n");
      }
      vPrevPriceTot = (vCurrClose * vCalcLength);
      debugPrint("Price Total = " + vPrevPriceTot + "\n");
      vPrevDevnTot = 0;

      return(0);
      }

      Comment


      • #4
        griesha30246
        You may want to try for (i = 0; i < vCalcLength; i++) instead of for (i = 0; i == (vCalcLength - 1); i++)
        Alex

        Comment


        • #5
          Problem with &quot;for&quot; statement - current understanding

          Thank you for the suggestion. It worked fine.

          My conclusions now are as follows:

          Example 1: for (i = 0; i > vCalcLength; i++) {
          Example 2: for (i = 0; i >= vCalcLength; i++) {
          Example 3: for (i = 0; i == vCalcLength; i++) {
          Example 4: for (i = 0; i = vCalcLength; i++) {

          Examples 1 and 2 work fine.

          Example 3 passes syntax check, but following statements will not execute. I understand that the second part of the "for" statement is a condition, hence this valid condition should work, but does not.

          Example 4 is incorrect as it has a single "=" where "==" should be used. This one is nasty! Trying a syntax check on this causes the cp to thrash. Only way out is to terminate eSig.

          Conclusion. Syntax check needs improving to a) allow all valid conditions in parameter 2, and b) report obvious errors in conditions ("=" instead of "==") especially as these are the types of simple errors programmers tend to make.

          My opinion only, of course. Hope this helps to improve the (already very good) product.

          Comment


          • #6
            griesha30246
            FWIW I am running 7.9 build 719 and the condition in your example 4 does not appear to cause any thrashing (or other abnormal behavior).
            Alex

            Comment


            • #7
              griesha,

              All four are valid Javascript syntax. #3 will only execute if vCalcLength == 0.

              And #4 is a classic programmer gotcha, but is completely valid Javascript for a programming point of view.

              Comment


              • #8
                &quot;for&quot; statement enlightenment

                Thank you Dion. I now realise you are absolutely correct.

                For what it's worth, I feel the documentation is misleading (and I certainly read it incorrectly).

                The docn states:
                "The for loop repeats until a specified condition evaluates to false"

                In other procedural languages (e.g. VB) the "for" statement is effectively a "for - until" construct, TERMINATING when a condition evaluates to true. In EFS code, it is a "for - while" construct which RUNS when condition is true.

                This is a key difference and I'm sure will catch out a lot of us "old" programmers. Still, the documentation does use the word "UNTIL" which is why the misinterpretation is so easy to make.

                Hope this is useful. Thanks again.

                Comment

                Working...
                X