Conditional branching if/else...endif?
-
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"
Alsoif 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? -
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.
-
@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... -
@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.
-
@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.
-
@oliof quite so. The obvious alternative for conditional GCode would have been to add endif and endwhile keywords to identify the ends of blocks.
-
@dc42 quite so, and we ran out of bracket types too.
-
@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.