Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Does Fibaro Lua support goto continue syntax?


Question

Posted

Hello guys,
I never used it in fobaro, and i dont know the syntax

Does Fibaro Lua support goto continue ?
Any example please?

6 answers to this question

Recommended Posts

  • 0
Posted (edited)

Syntax is:

goto label

...

::label::

I am wondering where this is justified to be used.

A case that comes to my mind is multi-nested loop exit; I hope this is one of rare cases when goto simplifies code.

Please login or register to see this code.

goto is used in this example instead of packing loop into a function or have a flag and double break.

You cannot jump to the different scope of variables, o.e. inside for...end loop, do...while with local definition.

But this is probably obvious.

In turn you can jump out of such blocks.

 

In general - it has negative impact of code clarity. As a kid I used it with Atari Basic.

PS. Maybe this is more general: You cannot use goto to jump into a block in a way that skips the initialization of local variables.

Edited by Łukasz997
  • 0
Posted

Without goto - is it really less clear? Maybe...

Please login or register to see this code.

Or maybe someone knows better use cases?

  • 0
Posted (edited)

The only reason "may" be to break out from nested loops like your example but in general it should be avoided. Reason beyond hard-to-follow code, it usually makes it hard for the compiler to optimize the code flow and do register allocations as normal block entry/exits are invalidated.

 

 

Please login or register to see this code.

Please login or register to see this code.



Alt2. factor out a find function that you can return from.
 

Please login or register to see this code.

Please login or register to see this code.

 

Edited by jgab
  • 0
  • Inquirer
  • Posted

    thanks, yes  as you wrote..
    I would need it to be used in very rare cases..mosty in nested loops, and in loops when inner part of the loop is long and hard to see what is where (Fibaros editor is not the best, although its better than it was 2 years ago)
    Thanks anyway for both of you

    • 0
    Posted
    5 minut temu, Neo Andersson napisał:

    mosty in nested loops, and in loops when inner part of the loop is long and hard to see what is where

    I don’t know your specific case, but I would advise against goto.
    It’s somewhat of an atavistic leftover from old programming languages.
    By wrapping the code in loop to a local function, you can significantly shorten such block.

    • Thanks 1
    • 0
    Posted (edited)
    9 hours ago, Łukasz997 said:

    I don’t know your specific case, but I would advise against goto.
    It’s somewhat of an atavistic leftover from old programming languages.
    By wrapping the code in loop to a local function, you can significantly shorten such block.

     

    Yes, and you can get some interesting errors - like if you introduce a local variable "within" the jump

    Please login or register to see this code.

    Please login or register to see this code.

     

    Edited by jgab
    • Thanks 1

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...