MacroMill CNC Automation Software

How a FANUC Macro WHILE Loop Works — Tip of the Iceberg

Step-through table in the MacroMill simulator showing a FANUC macro WHILE loop: counters 0 to 3 are true at 0, 90, 180 and 270 degrees, counter 4 is false and exits

A FANUC macro WHILE loop repeats a block of code for as long as its condition is true, then leaves when the test fails. In this lesson we follow a short MacroMill simulator video and walk through the pieces: the counter, the limit, the WHILE test, the body, the update and END. That loop is useful on its own, but it is only the tip of the iceberg of FANUC Custom Macro B. MacroMill’s simulators and training are there to help you master the rest to perfection, safely, before the program reaches a real machine.

What a FANUC macro WHILE loop does

Plain G-code is a fixed list of moves. If you need four keyways around a C-axis, or a set of depth passes, you would otherwise copy the same blocks again and again. A WHILE loop does that repetition for you. The control tests the condition before each pass. If the test is true, it runs the body. If the test is false, it skips the body and continues after the matching END.

The examples below are simulator training fragments, taken from different programs in the lesson video. They are not one complete, machine-ready program. Verify any real machine program before use.

The five parts of a FANUC macro WHILE loop

1. Set the counter

Start the count at zero. In the C-axis source program the counter is #110:

#110=0;

2. Set the limit

#101=4 means four keyways. With a +1 update, the counter will run from 0 to 3 — four passes — and then fail the test at 4.

#101=4 (KEYWAY COUNT);

3. Test before each pass

LT means less than. The line asks: is #110 less than #101? DO1 opens loop number 1. That loop number must match END1.

WHILE[#110 LT #101]DO1;
FANUC program screen in the MacroMill simulator highlighting WHILE[#110 LT #101]DO1, the test that runs before each pass of a FANUC macro WHILE loop
The WHILE test is checked before every pass. LT means less than. DO1 must match END1.

4. Run the body

If the test is true, the control runs the body. In the C-axis example the body first calculates the keyway angle, then executes the moves inside the loop. With four keyways, each pass is 90 degrees apart:

#111=360.0*#110/#101;

5. Update the counter, then END1

Add one to the counter so the next test sees a new value. The increment fragment in the lesson comes from another lathe program, used here to illustrate a +1 update with the C-axis source values:

#110=#110+1;

END1 sends the control back to the WHILE test. True? Run again. False? Leave the loop and continue after END1.

END1;

Step through: four keyways at 0°, 90°, 180° and 270°

With #101=4 and a +1 counter update, the angle #111=360.0*#110/#101 works out like this:

#110Test: #110 LT 4Angle #111
0TRUE0°
1TRUE90°
2TRUE180°
3TRUE270°
4FALSEExit — continue after END1

Four true passes give four keyways, equally spaced around the C-axis. On the fifth test, 4 LT 4 is false. The body is skipped and the program continues after END1.

Step-through table in the MacroMill simulator showing a FANUC macro WHILE loop: counters 0 to 3 are true at 0, 90, 180 and 270 degrees, counter 4 is false and exits
Illustrative walkthrough with #101=4 and a +1 update: four true passes, then exit.

The same repeat idea shows up in other work, not only C-axis indexing. The lesson also shows a different milling program, where each pass of a loop builds more toolpath in depth. The idea is the same: test, run the body, update, test again.

3D GRAPH view in the MacroMill FANUC VMC simulator showing a slot toolpath built up by repeated depth passes, illustrating the same WHILE loop repeat idea
A different program, same repeat idea: each pass adds more toolpath.

The common mistake: a loop that never exits

What if the counter never changes? The test stays the same. With the limit fixed at 4, 0 LT 4 stays true, the body keeps running, and the loop never reaches its exit. Always check that the loop contains an update that will eventually make the condition false — and that DO and END use the same number.

MacroMill simulator warning: if the counter in a FANUC macro WHILE loop never changes, 0 LT 4 stays true and the loop never exits
A missing counter update is a classic infinite-loop mistake. Find the counter, the test, the update and the END before you press Cycle Start.

Other comparison operators, and nesting DO1 to DO3

LT is only one of the comparisons Custom Macro B gives you. The full set is:

  • EQ — equal to
  • NE — not equal to
  • GT — greater than
  • GE — greater than or equal to
  • LT — less than
  • LE — less than or equal to

You can also nest loops. FANUC Custom Macro B allows loop numbers DO1, DO2 and DO3, up to three levels deep. The number on END must match the number on DO. Do not nest a loop with the same number as an outer loop that is still open (DO1 inside DO1 is not allowed). After END1, a later DO1 is fine.

WHILE is just the tip of the iceberg

Once the FANUC macro WHILE loop makes sense, you are looking at only one tool in Custom Macro B. The same language also gives you:

  • Variables — put changing numbers in # variables instead of hard-coding every size.
  • IF / GOTO — let the program choose a path, or raise a #3000 alarm when a value is wrong.
  • Arithmetic — angles, spacings, depths and trig, calculated at the control.
  • Subprogram calls with G65 — pass arguments into a macro, like a function call, and reuse it from any program.
  • Parametric cycles — one proven program for a whole family of parts.
  • Probing logic — measure, compare to nominal, and update offsets or stop on a bad reading.

That is how macros stop you rewriting the same program for every size, which we covered in Unlock CNC Macros Productivity on the Shop Floor — Write Once, Not Again. The same WHILE pattern also drives rotary work, as in our 4th axis CNC simulator post. MacroMill’s FANUC-style simulators and training are built so you can practise all of this — loops, variables, decisions, G65 calls and more — until you have mastered it to perfection, without risking a machine or a workpiece.

Simulator training only. The code shown here is verified fragments from different programs, not one complete machine-ready program. The walkthrough illustrates a +1 update with the C-axis source values; the counter-update fragment comes from another lathe program. The milling depth-pass footage is a different program demonstrating the same repeat idea. Check WHILE / DO / END syntax against your control’s Custom Macro manual, and verify any real machine program before use.

Try it in the MacroMill simulator

Open a program, find the counter, the test, the update and the END, then single-block through the loop and watch the variables change.

MacroMill

Get free CNC macro tips & simulator updates

New tutorials and MacroMill simulator deals, straight to your inbox.

We don’t spam! Read our privacy policy for more info.

Responses

  1. […] quarter-turn repeat is the same kind of loop logic you see in a FANUC macro WHILE loop: test, move, update the angle and Z, then go around […]

  2. […] passes can be generated from geometry, rather than from a long list of hard-coded positions. A FANUC macro WHILE loop is a simple example: four keyways at 0°, 90°, 180° and 270° from a counter and a test, not from […]

  3. […] the trapezoid. The same idea — calculate, loop, update — is the kind of logic you see in a FANUC macro WHILE loop. It is a different problem from CNC thread milling, which interpolates a helix with a milling […]

  4. […] G03 half-circle leads the cutter onto that helix. A WHILE loop then steps the depth: 0.75 mm down every quarter turn, so 39 ÷ 0.75 = 52 steps (13 full turns) to […]

  5. […] For the variable map, IF/GOTO and WHILE rules behind this, see the FANUC Macro B guide and How a FANUC macro WHILE loop works. For a walk-through of writing, testing and taking a macro to the machine, see Write a FANUC macro, […]

  6. […] The test #101 LT 4 is made before each pass, so the body runs for #101 = 0, 1, 2 and 3, and the loop leaves when #101 reaches 4. The line that adds 1 is what moves the counter toward the exit. Leave it out and the condition stays true forever. We walk through that mistake in How a FANUC macro WHILE loop works. […]