Viewing a single comment thread. View all comments

Sloloem t1_iyeh3if wrote

Controlling flow from inside loops is done with break and continue.

Something like this:

for (i=1;i<=10;i++) {
  if (i==5) SOMETHING
  print i;
}

If you use break for SOMETHING your output is: 1 2 3 4

If you use continue your output is: 1 2 3 4 6 7 8 9 10

Some languages let you control multiple levels of looping using something like break 2 to break not only this loop, but the loop it's inside. Javascript lets you do this with labels. Not all languages give you this level of control so you either figure out how to design the process without nested loops or come up with other ways around the issue like GOTO's or flag variables that direct outer loops to break when set from inner loops but those situations have no single right answer.

2

OldHellaGnarGnar2 t1_iyehcc0 wrote

Do these work similarly for while loops?

2

Sloloem t1_iyehp37 wrote

Oh yeah. Totally.

2

OldHellaGnarGnar2 t1_iyf5swx wrote

I'm not sure if I missed your link about breaks earlier, or if it was edited in, but I just now saw it, and it's super helpful. Our robot programs technically use "JMP LBL", not GOTO, but I basically took them as the same thing in terms of function. So it already has labels for each section if I were to restructure it to use nested loops and whatnot.

All of your comments have been super helpful. I've been wanting to learn more programming for a while, but wasn't sure what concepts or practices I'm not even aware of, and this gives me a lot to think about. I recently got the "Automate the Boring Stuff with Python" book, to use as a starting point, but am still kinda learning the syntax and python-equivalent commands of what I already know in Matlab of Fanuc TP, and haven't really gotten to stuff about code structures or paradigms, etc

2

Sloloem t1_iyf986x wrote

Sweet deal, glad I could help and good luck breaking in.

Actually I just scoped out some of your comment history and see you're working in CNC engineering and pop into physics and CAD subreddits a lot...I have an idea for a hand-cranked guitar pickup winder I'd like to design for 3d printing but for some reason I'm having a hell of a time getting my head around how to design the gearbox to multiply RPMs in a reasonable size. A guitar pickup involves upwards of 10,000 winds of a copper filament around the magnetic pole pieces. Motorized pickup winders tend to have speed controls from 600 to 2000 RPMs but for a hand-cranked machine 500RPM seems downright reasonable. We're talking <2oz balanced load. Is that something that might be in your wheelhouse that you could point me at some good resources or fundamentals about? Because I'd love to learn it.

2