Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    If and while always evaluate the whole expression

    Beginners
    if-then-else
    4
    7
    391
    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.
    • spikey
      spikey F last edited by spikey

      Yes, excuse me, yet another if post. Consider it as a note, and correct my observation if you know better, please.

      a = []
      if (len(a) > 0) and (a[0] == 1) then
          // do something 
      endif
      

      I wanted to use the while loop like this, because I need to check something inside the array to decide if the loop should go on

      a = []
      i = 0
      while (i < len(a)) and (a[i] == "test") loop
          // do something 
         i += 1
      repeat
      

      workaround:

      a = []
      i = 0
      while (i < len(a)) loop
         if (a[i] != "test") then
            break
         endif
          // do something 
         i += 1
      endif
      

      Appendix 03/26/2020:
      The first two code samples throw an out of bound exception. Thank you @PB____ for linking to the first post about this, I searched for if-then-else posts, so I was not able to find it. So, @Spacemario came to the same workaround.

      1 Reply Last reply Reply Quote 1
      • M
        MikeDX last edited by

        This definitely shouldn't happen and is a bug, Ill report it to the team 👍

        1 Reply Last reply Reply Quote 1
        • PB____
          PB____ last edited by

          A similar report was also mentioned in: https://fuzearena.com/forum/topic/644/logical-and-or-do-not-short-circuit

          Also and and or cast the result to a boolean value, for example (2 and 3) == 1. I would expect (2 and 3) == 3 and (2 or 3) == 2, but I do think beginning programmers might expect and and or to behave the way they are implemented now.

          Maybe it's an idea to add additional operators: && and || to implement short circuiting, without casting the type?

          Anyway, that's my opinion on it, do with it as you please...

          1 Reply Last reply Reply Quote 1
          • Martin
            Martin Fuze Team last edited by

            I don’t even understand what your report is saying. But it’s ok because Mike seems to so maybe I missed something.

            It’s already been stated before that Fuze does not perform short circuiting like some languages do.

            As far as I can see, none of your loops will iterate at all. Or is that the point? That the last one does maybe?

            1 Reply Last reply Reply Quote 0
            • PB____
              PB____ last edited by

              They way I read it, the reasoning is that Fuze will throw an error because it does not perform short circuiting:

              even though a is an empty array here, the expression (i < len(a)) and (a[i] == "test") will try to evaluate both i < len(a) and a[i] == "test". So the first check does not 'protect' the second one. It will try to evaluate a[0] == "test" anyway, even though a does not contain any elements.

              That's why I linked to the earlier short circuiting post.

              1 Reply Last reply Reply Quote 1
              • Martin
                Martin Fuze Team last edited by

                I've not seen the other thread before so I can't say why I know that Fuze doesn't short-circuit, but it's not news to me. So yeah, it doesn't surprise me that a[i] == "test" errors, because Fuze doesn't short-circuit. I wouldn't have know that it evaluates right to left though so that's useful if true.

                1 Reply Last reply Reply Quote 0
                • PB____
                  PB____ last edited by

                  Just checked, it does operate right to left (for both or and and), but it will always evaluate both sides regardless.

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