• Why do this two do while loops produce different output?

    var dataLen4 = 4;
    var idx = 0;
    do {
          console.log("1. idx:", idx, idx << 2);
          ++idx;
    } while(idx <  dataLen4 );
    /* output
    1. idx: 0 0
    1. idx: 1 4
    1. idx: 2 8
    1. idx: 3 12
    */
    
    idx=0;
    do {
          console.log("2. idx:", idx, idx << 2);
    } while(++idx <  dataLen4 );
    /* output
    2. idx: 0 0
    2. idx: 2 8
    2. idx: 3 12
    */
    

    Code was running on a PICO_R1_3 with 2v06.49.

  • Wow, thanks. No idea how that happened - and it's been a problem since at least 2v00.

    I'll get on to a fix for this now

  • Ok, now fixed (in cutting edge or 2v07)

  • Thanks - As quickly as usual!

  • Neat... I use(d) pre-decrement quite a lot (when walking an array (backwards when direction did not matter)... luckily I used just the while () ...; and not the do ... while() construct and never noticed an issue... ...and there is no issue with it... (the 'real' pre-pre-decrement...) - haha (I usually try NOT to enter a loop unconditionally... it is increased risk for an infinite loop (and it has messed with me). Of course, even with an upfront conditioned loop, an infiniteloop is possible, but much less because 'the break/exit thinking' is done upfront rather than an after thought. If I wanted to run the loop at least once, that would go also into the setup of the upfront condition... and thought. I'm though very aware of the fact that the do ... while() construct can be very useful (and is actually very Espruino-economic): it conveys already by its very architecture that the loop body is executed at least once where as with the upfront condition that fact is masked and only understood by wading the start values and the condition. @MaBe, saw your post the day before. Even though it is fixed now, i wanted to know if I was affected by it.

    // predecrement.js
    
    function predecTest() {
    
      var i=5; // for example: ...=array.legth()
      while(--i >= 0) console.log(i); // walk array
      console.log("----(1)");
    
      var j=4; // for example: ...=array.legth()-1
      while (j >=0) {
        console.log(j);
        j--; }
      console.log("----(2)");
    
      var k=5; // for example: ...=array.legth()
      while (k > 0) { k--; // walk array
        console.log(k); }
      console.log("----(2)");
    
      var l=4; // for example: ...=array.legth()-1
      do { console.log(l);
      } while(--l >= 0); // walk array
      console.log("----(4)");
    
    }
    
    function onInit() {
      predecTest();
    }
    
    setTimeout(onInit,999);
    

    Output:

    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    4
    3
    2
    1
    0
    ----(1)
    4
    3
    2
    1
    0
    ----(2)
    4
    3
    2
    1
    0
    ----(2)
    4
    2
    1
    0
    ----(4)
    > 
    
  • it's been a problem since at least 2v00.

    Says something about how often people use do...while() loops doesn't it?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

[solved] look's like pre increment in while condition is skipping the first increment

Posted by Avatar for MaBe @MaBe

Actions