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

    Meta commands

    Scheduled Pinned Locked Moved
    Gcode meta commands
    4
    31
    2.0k
    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.
    • alankilianundefined
      alankilian @jens55
      last edited by

      @jens55 You don't need to add a 'continue' statement at the end of a loop.

      jens55undefined 1 Reply Last reply Reply Quote 0
      • alankilianundefined
        alankilian @jens55
        last edited by

        @jens55 said in Meta commands:

        The commented out line seems to dump the 'continue' statement outside of the loop and seems to me to be a bug in interpretation.

        You need to be very particular with the left-side whitespace to keep things working.

        The interpreter is going to look at how many tab (or space)?) characters you have at the beginning of every line to know where the end of the loops are.

        Your commented-out line starts with a semicolon in column one which tells the interperter that's the end of your while loop.

        So, the 'continue' statement is not outside of the loop.

        1 Reply Last reply Reply Quote 0
        • jens55undefined
          jens55 @alankilian
          last edited by

          @alankilian, I realize that .... but what I am getting at is that something is fishy about how this is interpreted. While I don't need the 'continue', there should be nothing barring me from using it. It isn't inherently wrong.

          alankilianundefined 1 Reply Last reply Reply Quote 0
          • alankilianundefined
            alankilian @jens55
            last edited by alankilian

            @jens55 That semicolon in column-1 causes the interpeter to end the while loop.

            This works:

            while iterations < 10
              m118 s{"hello world " ^ iterations}
              ;  m118 s{"hello world " ^ iterations}
              g4 s2
              continue
            
            jens55undefined 1 Reply Last reply Reply Quote 0
            • jens55undefined
              jens55 @alankilian
              last edited by

              @alankilian, I will test that but the semicolon is there to comment out the line and should not cause this reaction.

              alankilianundefined 2 Replies Last reply Reply Quote 0
              • alankilianundefined
                alankilian @jens55
                last edited by

                @jens55 This works to keep the whitespace:

                while iterations < 10
                  m118 s{"hello world " ^ iterations}
                  ;  m118 s{"hello world " ^ iterations}
                  g4 s2
                  continue
                
                jens55undefined 1 Reply Last reply Reply Quote 0
                • jens55undefined
                  jens55 @alankilian
                  last edited by

                  @alankilian
                  @dc42
                  Ahhhh, I need the spaces before the comment .... ok but that s a bug in the interpreter

                  alankilianundefined 1 Reply Last reply Reply Quote 0
                  • alankilianundefined
                    alankilian @jens55
                    last edited by

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • alankilianundefined
                      alankilian @jens55
                      last edited by

                      @jens55 said in Meta commands:

                      Ahhhh, I need the spaces before the comment .... ok but that s a bug in the interpreter

                      From the documentation:

                      The body ends just before the first line that is not indented.

                      So, it could be argued that the interpreter is working fine.

                      It's not up to me to decide.

                      I'm happy you're getting going on your project again.

                      And I learned some things from this discussion, so thank you for that also.

                      1 Reply Last reply Reply Quote 0
                      • jens55undefined
                        jens55
                        last edited by jens55

                        @dc42
                        Here is another example of strange behaviour:

                        while true
                          if iterations < 10
                            m118 s"hello world"
                            g4 s2
                            continue
                        break
                        

                        This executes but leaves the printer in a 'busy' state
                        Without the 'break' command it does the same
                        If I move the 'break' command to the right by two spaces, everything finishes ok - it breaks out of the loop
                        Point is, there is no such thing as an 'endif' to clearly identify where the 'if' statement ends. IMHO there should be (optional maybe) an 'endif' for readability but in any case, the 'break' should break out of the loop no matter what.

                        Edit: On re-reading this, there is a flaw in my thinking here, and unless the 'break' is indented two spaces over, the 'while' loop has no exit. Interesting note here is that the iteration variable apparently isn't reset until after exiting the 'while' loop because it only prints 'hello world' for one 'if' loop.
                        That to me seems rather odd ....

                        OwenDundefined 1 Reply Last reply Reply Quote 0
                        • OwenDundefined
                          OwenD @jens55
                          last edited by OwenD

                          @jens55
                          Nothing strange about it.
                          Using "while true" creates an infinite loop
                          This is a dangerous practice for the uninitiated as you must ensure that at some point you have a conditional that triggers a break command.
                          You have not done so. Therefore the loop continues.
                          Once it gets to iteration 10 it just stops sending the echo.

                          You should have put

                          'if iterations < 10
                               Echo "blah blah"
                          else
                                break
                          

                          Your continue command is useless as it's not part of the while construct.
                          The point of continue is to skip an iteration.

                          'if iterations < 10
                               if iterations = 5
                                      continue 
                               Echo "blah blah" ; won't happen on #5
                          else
                                break  ;  required to end loop
                          

                          It's safer to use
                          while iterations < 10
                          blah blah

                          EDIT
                          Sorry. Doing this on my phone so I could not see your code
                          Your BREK is outside the whole loop, but the comments stand

                          jens55undefined 1 Reply Last reply Reply Quote 1
                          • jens55undefined
                            jens55
                            last edited by

                            Here is another thing that strikes me as odd:

                            while true
                              if iterations < 10
                                m118 s"hello world"
                                g4 s2
                                continue
                              if iterations < 11
                                m118 s"hello north america"
                            	g4 s2
                            	continue
                              break
                            

                            This code will execute the first 'if' for 10 times but will not reset 'iterations' as I would have expected and because of that will only execute the second 'if' loop once.

                            alankilianundefined 2 Replies Last reply Reply Quote 0
                            • jens55undefined
                              jens55 @OwenD
                              last edited by

                              @owend said in Meta commands:

                              Your continue command is useless as it's not part of the while construct.
                              The point of continue is to skip an iteration.

                              To skip an iteration ???? Where did you see this in the documentation ???
                              I look at 'continue' as a command that strictly exists as a means to improve readability of the code since the command itself seems to do diddly squat and can be left out.

                              alankilianundefined DanS79undefined 2 Replies Last reply Reply Quote 0
                              • alankilianundefined
                                alankilian @jens55
                                last edited by

                                @jens55 said in Meta commands:

                                @owend said in Meta commands:

                                Your continue command is useless as it's not part of the while construct.
                                The point of continue is to skip an iteration.

                                To skip an iteration ???? Where did you see this in the documentation ???

                                'continue' jumps back to the BEGINNING of a loop.

                                So if you have the following story: (COMPLETELY useless, but trying to illuminate how 'continue' is used)

                                while iterations < 10
                                  m118 s{"This is iteration " ^ iterations}
                                  if iterations > 5
                                    continue  ; Don't do this stuff after the first 5 iterations
                                  m118 s{"Perform this action on " ^ iterations}
                                
                                This is iteration 0
                                Perform this action on 0
                                This is iteration 1
                                Perform this action on 1
                                This is iteration 2
                                Perform this action on 2
                                This is iteration 3
                                Perform this action on 3
                                This is iteration 4
                                Perform this action on 4
                                This is iteration 5
                                Perform this action on 5
                                This is iteration 6
                                This is iteration 7
                                This is iteration 8
                                This is iteration 9
                                
                                1 Reply Last reply Reply Quote 0
                                • alankilianundefined
                                  alankilian @jens55
                                  last edited by

                                  @jens55 said in Meta commands:

                                  Here is another thing that strikes me as odd:

                                  while true
                                    if iterations < 10
                                      m118 s"hello world"
                                      g4 s2
                                      continue
                                    if iterations < 11
                                      m118 s"hello north america"
                                  	g4 s2
                                  	continue
                                    break
                                  

                                  This code will execute the first 'if' for 10 times but will not reset 'iterations' as I would have expected and because of that will only execute the second 'if' loop once.

                                  Why do you think 'iterations' will get reset to zero?
                                  'iterations' counts the number of times through the loop.

                                  Your 'if' statements will not affect the iteration count at all.

                                  1 Reply Last reply Reply Quote 0
                                  • DanS79undefined
                                    DanS79 @jens55
                                    last edited by

                                    @jens55 said in Meta commands:

                                    To skip an iteration ???? Where did you see this in the documentation ???
                                    I look at 'continue' as a command that strictly exists as a means to improve readability of the code since the command itself seems to do diddly squat and can be left out.

                                    To be clear it doesn't skip an iteration, it skips everything after the continue statement in the current iteration. It goes back to the top of the looping structure and starts the next iteration. Every programming language I can think of works this way.

                                    1 Reply Last reply Reply Quote 1
                                    • alankilianundefined
                                      alankilian @jens55
                                      last edited by

                                      @jens55 I'm trying to help you understand how this works, but your responses seem to be accusatory (the interpreter is broken) and inflammatory, so if you don't change your attitude, I'll check out of this conversation.

                                      I'm really trying to help you understand how to write programs in a clear understandable way.

                                      Ref: I'm a retired embedded-systems software and hardware engineer who has been programming since about 1978, so I've got the experience to be able to help and in addition, you can help me learn new things also if we both work together on it.

                                      jens55undefined 1 Reply Last reply Reply Quote 0
                                      • jens55undefined
                                        jens55 @alankilian
                                        last edited by jens55

                                        @alankilian, I REALLY appreciate any and all help and I apologize if you felt in any way slighted whatsoever. It was most certainly not my intent !!!
                                        I got confused by the sentence "The point of continue is to skip an iteration."
                                        DanS79 cleared it up and confirmed my interpretation by saying "To be clear it doesn't skip an iteration, it skips everything after the continue statement in the current iteration." IE it doesn't skip an iteration but goes back to the beginning of the loop.
                                        Your example (thanks) did however clarify another point on the continue command that I was not aware of and hence my earlier confusion about sequential 'if' statements. The iteration happens over the 'while' loop and not as I had assumed over the 'if' loop. A very important bit of learning for me!
                                        So to repeat, I apologize profusely and hope we are back on the same wavelength !

                                        alankilianundefined 1 Reply Last reply Reply Quote 3
                                        • alankilianundefined
                                          alankilian @jens55
                                          last edited by

                                          @jens55 Awesome!

                                          AND, you got me to try some new stuff using meta-commands, so everyone wins.

                                          1 Reply Last reply Reply Quote 0
                                          • OwenDundefined
                                            OwenD
                                            last edited by

                                            @jens55
                                            Poor choice of words on my part. , but the code should have been clear enough
                                            Apologies for confusing you.
                                            I'm Australian, so English isn't my first language 😂

                                            jens55undefined 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Unless otherwise noted, all forum content is licensed under CC-BY-SA