• Tags
  • Documentation
  • Order
  • Register
  • Login
Duet3D Logo Duet3D
  • Tags
  • Documentation
  • Order
  • Register
  • Login

Conditional branching if/else...endif?

Scheduled Pinned Locked Moved
Gcode meta commands
4
8
294
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.
  • undefined
    soare0
    last edited by soare0 27 Jan 2024, 19:20

    Re: A couple of questions regarding global variables
    For reasons I do not know, trying to work with variables, was a little... convoluted.

    First, I tried to define a global variable in a trigger#.g file. It did not work, I received an error, like "unknown variable". Still, defining it (same line of code, cut and paste) at the end of config.g, worked like a charm.

    Second, not directly related, but still: when testing that variable in a file (it was in pause.g, where I needed to branch to different situations), I had problems with syntax.
    For example:

    if variable =0
    gcode
    gcode
    gcode
    else
    gcode
    gcode
    gcode

    did not worked. The message was "else is not following if"
    Also

    if variable =0
    P98.....
    else
    gcode
    gcode
    gcode

    did not worked.

    But, fortunately:

    if variable =0
    P98 etc1
    else
    P98 etc2

    worked.
    So it seems that for many (but 1) lines of code, after a if or else, syntax asks maybe for some brackets, or such, but I could not find any reference.
    I also have not encountered documented end if or endif.
    At least I found a way, but maybe somebody can shed some light on this?

    undefined 1 Reply Last reply 27 Jan 2024, 19:53 Reply Quote 0
    • undefined
      infiniteloop @soare0
      last edited by 27 Jan 2024, 19:53

      @soare0

      The message was "else is not following if"

      Programming (or better: writing scripts) requires rules. For example, all of your examples are missing indentation. Please study this document: GCode meta commands carefully - although its title suggests otherwise, it serves as kind of a programmer language manual. In addition, you should be familiar with the GCode dictionary.

      undefined 1 Reply Last reply 30 Jan 2024, 23:47 Reply Quote 1
      • undefined
        soare0 @infiniteloop
        last edited by 30 Jan 2024, 23:47

        @infiniteloop
        I tried indentation, of course, the sample above was ... just for reference. I tried it like a ... magic charm, and now I found that it is needed functional, and not only for code clarity.
        Indeed, it is true that I did not studied word for word the chapter mentioned by you. After some years of firmware programming, as an electronics engineer, I may have lost my patience with code (being ... promoted from programming, if I can say so). I just do not remember that any of the C (or even Basic) compilers I used, to be structured like this, so eh... old habits.
        Thanks for clarifications, anyway...

        undefined undefined 2 Replies Last reply 31 Jan 2024, 10:06 Reply Quote 0
        • undefined
          dc42 administrators @soare0
          last edited by 31 Jan 2024, 10:06

          @soare0 said in Conditional branching if/else...endif?:

          I just do not remember that any of the C (or even Basic) compilers I used, to be structured like this, so eh... old habits.

          Older programming languages generally use braces or keywords to indicate where blocks start and end. In those languages, indentation is typically used to make the blocks more visible to human readers, but the compiler itself ignores indentation. This led to language designers suggesting that if indentation is what human readers use to identify the block structure, why not get the compiler to identify the block structure the same way, and dispense with the braces? And this is exactly what some more recent languages such as Haskell and Python do.

          See https://en.wikipedia.org/wiki/Off-side_rule for more details.

          Duet WiFi hardware designer and firmware engineer
          Please do not ask me for Duet support via PM or email, use the forum
          http://www.escher3d.com, https://miscsolutions.wordpress.com

          1 Reply Last reply Reply Quote 0
          • undefined
            oliof @soare0
            last edited by 31 Jan 2024, 10:06

            @soare0 indentation for code logic is a huge point of contention. Some languages need it, others don't. If you started out with languages that do need it, it can be infuriating to switch those that don't, and the other way round.

            <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

            undefined 1 Reply Last reply 31 Jan 2024, 10:19 Reply Quote 0
            • undefined
              dc42 administrators @oliof
              last edited by dc42 31 Jan 2024, 10:19

              @oliof quite so. The obvious alternative for conditional GCode would have been to add endif and endwhile keywords to identify the ends of blocks.

              Duet WiFi hardware designer and firmware engineer
              Please do not ask me for Duet support via PM or email, use the forum
              http://www.escher3d.com, https://miscsolutions.wordpress.com

              undefined 1 Reply Last reply 31 Jan 2024, 10:20 Reply Quote 0
              • undefined
                oliof @dc42
                last edited by 31 Jan 2024, 10:20

                @dc42 quite so, and we ran out of bracket types too.

                <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

                undefined 1 Reply Last reply 31 Jan 2024, 13:30 Reply Quote 0
                • undefined
                  soare0 @oliof
                  last edited by 31 Jan 2024, 13:30

                  @oliof Indeed, I noticed 3d printing and CNC machining need more brakets, haha...!
                  It was just unexpected, as Python is a foreign language to me (it always was).
                  No problem adapting though. If I survived to... some 3d printers, for sure I would survive missing some brackets...
                  Thanks again for your help.

                  1 Reply Last reply Reply Quote 1
                  2 out of 8
                  • First post
                    2/8
                    Last post
                  Unless otherwise noted, all forum content is licensed under CC-BY-SA