Navigation

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

    What does ‘else’ actually do?

    Beginners
    if-then-else conditional
    4
    6
    443
    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.
    • PickleCatStars
      PickleCatStars F last edited by

      If something then
      Do a thing
      Endif

      Very simple and straightforward. You can see if things are more than, less than, equal to, not equal to, the same as (like for strings), see if things are not the same or not more or not less etc. I do loads of stuff using those. Sometimes I do long chains of ‘if something and if not something and...’ sometimes I put more ifs inside other ifs and it all works out. I’ve never had to use an ‘else’. So what does it do and how would I use it? :S

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

        @toxibunny

        The expression in an if statement (something in your example) can only have one of two outcomes:

        1. it's true, the block within the if statement is executed
        2. it's false, the block within the if statement is not executed

        Sometimes you also want to do something if the expression results into false (scenario 2), but it's something different from if the expression is true (scenario 1).

        You could write this as follows:

        var c = controls(0)
        if c.a and c.b then
            // do scenario 1
        endif
        if !(c.a and c.b) then
            // do scenario 2
        endif
        

        This would work, but it's more laborious to maintain and to understand. You might even forget why it's written this way later, and update one if statement, but forget to update the other.

        By using the else keyword, you make this more compact, and you only need to maintain the if condition in one place:

        var c = controls(0)
        if c.a and c.b then
            // do scenario 1
        else
            // do scenario 2
        endif
        

        Edit:
        Besides this point, if you have a complex expression that you use in many places, it could also be a good idea to move that to a separate function, so in this example that would lead to:

        function doABarrelRoll()
            var c = controls(0)
        return c.a and c.b
        
        if doABarrelRoll() then
            // do scenario 1
        else
            // do scenario 2
        endif
        

        By doing this, the function-name reminds you what the combination of checks actually stand for, the next time you look at it. And on top of it, if you want to change the controls for doABarrelRoll in this example, you only need to change one line of code.

        1 Reply Last reply Reply Quote 3
        • PickleCatStars
          PickleCatStars F last edited by

          Thanks, that’s much clearer now. I like what you did here:

          function doABarrelRoll()
          var c = controls(0)
          return c.a and c.b

          The last line returns a 1 if they’re both 1, but 0 otherwise, is that right? I never thought of doing it that way. It’s nice and neat.

          waldron PB____ 2 Replies Last reply Reply Quote 1
          • waldron
            waldron F @PickleCatStars last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • PB____
              PB____ @PickleCatStars last edited by

              @toxibunny said in What does ‘else’ actually do?:

              The last line returns a 1 if they’re both 1, but 0 otherwise, is that right?

              Yes, you are correct. For accuracy, I feel like I need to say more, but you can ignore this:

              The more correct phrasing would be:

              The last line returns a true if they’re both 1, but false otherwise

              In some other languages, the expression 2 and 3 would result in 3, but in Fuze 2 and 3 results in 1. This means that 1 in this context is treated as a boolean, not an int. So even though 1 and true are technically the same data in Fuze, by calling it true it's more clear that you don't need to expect the function to return a different numerical value.

              1 Reply Last reply Reply Quote 1
              • T
                tony7059 last edited by

                @PB____ , nice refactor and explanation of if-else : )

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