MacroMill CNC Automation Software

WHILE vs IF in FANUC macros: a lesson on repeating and deciding

WHILE and IF in a FANUC macro on the MacroMill simulator: O9011 shows WHILE[#154LT#110]DO2, an IF clamp and END2

This FANUC macro lesson is about the difference between WHILE and IF. Both let a program decide something, and beginners often mix them up. By the end you will know which one repeats, which one decides once, how to write each, and how the two work together in one real MacroMill macro.

You can step through every example in the MacroMill FANUC VMC simulator, the FANUC VMC Advanced Control Suite, and watch the variables change line by line. Yes, programs and macros you write and test in a MacroMill simulator can be saved or exported and run on a real CNC machine, after a normal prove-out with single block, reduced rapids and a dry run.

The difference between WHILE and IF in one minute

IF is a one-time decision. The control reaches the IF, tests the condition once, and either runs what follows or skips it. Then it carries on down the program. It never goes back.

WHILE is a repeat. The control tests the condition before every pass. While it is true, it runs every block between WHILE and the matching END, jumps back to the WHILE and tests again. When the test is false, it jumps to the block after END and leaves the loop.

So the short answer is: use IF to choose, and use WHILE to repeat. If you want a block to run once or not at all, that is IF. If you want it to run again and again until a number reaches a limit, that is WHILE.

How to write IF

FANUC Macro B has two forms of IF. IF THEN runs one statement when the condition is true. IF GOTO jumps to a sequence number when the condition is true.

IF [#101 GT 0] THEN #102 = 1
IF [#101 LE 0] GOTO 100

In the first line, #102 becomes 1 only when #101 is greater than 0. If it is not, the control ignores the statement and moves to the next block. In the second line, a true condition sends the program to the block with N100, skipping everything in between. A false condition does nothing and the next block runs. The code above shows the pattern only; the real examples come later.

How to write WHILE

WHILE needs a condition, a DO number and a matching END with the same number.

#101 = 0
WHILE [#101 LT 4] DO1
(blocks that repeat)
#101 = #101 + 1
END1

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.

WHILE loop step-through in a FANUC macro on the MacroMill simulator: the test is true for counters 0 to 3 and false at 4
The MacroMill simulator steps a WHILE loop: counters 0 to 3 are true, counter 4 is false and the loop exits. Illustrative walkthrough with #101 = 4.

The comparison operators: EQ, NE, GT, LT, GE and LE

Both IF and WHILE use the same six comparison words inside square brackets.

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

The boundary is the part that catches people out. WHILE [#101 LT 4] runs four times from a start of 0. WHILE [#101 LE 4] runs five times. One letter changes the number of passes, so choose LT or LE on purpose and test it.

DO1, DO2 and DO3: nesting loops

You can put one WHILE inside another. FANUC gives you three nesting levels, DO1 to DO3, and each loop closes with its own number: END1, END2 or END3. The numbers must match and the loops must nest cleanly, with the inner loop finished before the outer one ends.

The bolt hole circle macro below does exactly this. DO1 loops over the holes, and DO2 inside it loops over the depth steps of one hole.

A real example: WHILE and IF working together

This is the depth loop from MacroMill’s bolt hole circle lesson, program O9011. It cuts each hole with a helical path, one quarter turn at a time. Here are the lines from the loop.

#153 = 0. (HELIX ANGLE)
#154 = 0. (DEPTH)
WHILE [#154 LT #110] DO2
#154 = #154 + #114
IF [#154 GT #110] THEN #154 = #110
#155 = #153
#153 = #153 + #145 * 90.
G#144 X[#151+#142*COS[#153]] Y[#152+#142*SIN[#153]] Z[#111-#154] I[-#142*COS[#155]] J[-#142*SIN[#155]] F#119
END2

In this lesson #110 is the hole depth, 39, and #114 is the depth per quarter turn, 0.75. Here is what each part does.

  • WHILE [#154 LT #110] DO2 repeats the quarter turns. It keeps going while the depth so far, #154, is less than the hole depth, #110.
  • #154 = #154 + #114 adds one more step of depth on every pass. This is the line that moves the loop toward its exit.
  • IF [#154 GT #110] THEN #154 = #110 is the decision. If a step would take the depth past the bottom, it clamps #154 to the bottom, so the last pass cannot overshoot.
  • END2 jumps back to the WHILE and tests again.

With a depth of 39 and a step of 0.75, the loop needs 52 quarter turns, and the 52nd lands exactly on Z-39. Because 39 divides evenly by 0.75, the clamp stays quiet in this case. Now change the hole depth to 39.5. The steps no longer divide evenly, and without the IF the last pass would cut past the bottom. With the IF, it stops exactly at the depth you asked for. That is the power of using them together: WHILE does the repeating, and IF protects the edge.

WHILE and IF in a FANUC macro on the MacroMill simulator: O9011 shows WHILE[#154LT#110]DO2, an IF clamp and END2
The O9011 depth loop in the MacroMill simulator: WHILE repeats the quarter turns and IF clamps the last pass to the bottom.

The screenshot shows the same lines in the simulator, with the variables beside them. On pass 6, #154 is 6 × 0.75 = 4.5 and the angle #153 is 540 degrees, so X = 100 + 14 × COS 540 = 86 and Z = -4.5. You can read the program and see exactly why it moved there. For the full walk-through of this macro, read the bolt hole circle and A-axis rope groove lessons.

IF on its own: choosing a direction

Not every decision needs a loop. In the MacroMill rope groove lesson, program O9010 cuts the groove level by level. It must change direction on every second level, so the passes run left to right, then right to left. That is a serpentine path, and it is a single IF.

#157 = 1
IF [[#150-2*FIX[#150/2]]EQ0] THEN #157 = -1

#150 is the depth level. The expression #150 – 2 × FIX[#150 / 2] is the remainder after dividing the level by 2, so it is 0 on even levels and 1 on odd levels. #157 starts at 1. On an even level the IF sets it to -1, which flips the side the passes start from. The IF runs once for each level and makes one choice.

IF THEN lines in a FANUC macro on the MacroMill simulator, with the #157 serpentine direction IF highlighted
The O9010 program panel in MacroMill: three IF…THEN decisions, with the serpentine direction line highlighted.

IF is also how a macro protects itself. The same rope groove program checks its inputs at the start, and stops with an alarm if something is wrong:

IF[#100LE0]THEN#3000=1(STOCK DIAMETER MUST BE ABOVE 0)
IF[#103LE0]THEN#3000=2(PITCH MUST BE ABOVE 0)

Setting #3000 raises a macro alarm and stops the program, so a bad input cannot reach the cutting blocks. This is the kind of check we cover in common FANUC macro mistakes.

When to use WHILE and when to use IF

  • Use IF to choose between two outcomes, to set a value only in certain cases, to clamp a number to a limit, or to check an input and raise an alarm.
  • Use WHILE when the same set of blocks must run several times and you can say when it stops: holes in a circle, depth steps, thread passes or tool-wear steps.
  • Use both when a repeat needs guard rails. A WHILE with an IF inside it is the most common pattern in a working macro.

Why WHILE and IF make one macro do the work of many

Without a loop, a bolt hole circle with 8 holes needs 8 copies of the same blocks, and 52 quarter turns per hole would take hundreds of lines. With WHILE, it is one short loop. Change the hole count or depth in the variables and the same program cuts a different part. With IF, the program adapts to what it is given instead of crashing on a bad value.

That is how a macro becomes reusable for any size: WHILE removes the repeated blocks, and IF handles the cases. For more on the idea, see the CNC macro programming guide and write once, not again.

Try it yourself

1. Which one repeats, WHILE or IF?
WHILE. It tests before each pass and repeats the blocks up to END. IF decides once and moves on.

2. How many times does WHILE [#101 LT 4] DO1 run if #101 starts at 0 and the loop adds 1 each pass?
Four times, for #101 = 0, 1, 2 and 3. At 4 the test is false and the loop exits.

3. What happens if you forget the line that adds to the counter inside the WHILE?
The condition never becomes false, so the loop never ends. The counter update is what moves the loop toward its exit.

4. What does IF [#154 GT #110] THEN #154 = #110 do in the depth loop?
If the next step would go deeper than the hole bottom, it sets the depth to the bottom, so the last pass cannot overshoot.

5. What is the difference between LT and LE?
LT is less than, and LE is less than or equal to. With a counter starting at 0, WHILE [#101 LT 4] runs four times and WHILE [#101 LE 4] runs five.

Practise WHILE and IF in the MacroMill simulator

Reading about WHILE and IF helps, but you learn them fastest by stepping through them and watching the variables change. The MacroMill FANUC VMC simulator lets you write a macro, run it one block at a time, and see each variable, each test and each toolpath result as it happens. You can break a loop on purpose, fix it, and see the program react, all before you go near a machine. Programs and macros you write and test there can be saved or exported and run on a real CNC machine, after a normal prove-out with single block, reduced rapids and a dry run.

Get the simulator here: MacroMill FANUC VMC Advanced Control Suite on Payhip, or see it on the MacroMill homepage. If you have a question about the lesson, write to info@macromillcnc.online.

For tips and updates, join the MacroMill WhatsApp group: chat.whatsapp.com/Htae97asBiD14QiwMXjGJA.

The software is for training and simulation only. Yes, programs and macros you write and test in a MacroMill simulator can be saved or exported and run on a real CNC machine. As with any new program, prove it out first with single block, reduced rapids and a dry run before you cut. Addresses, variable numbers, feeds, speeds, clearances and tool offsets in these examples must be confirmed against your machine, your tooling and your control’s Custom Macro B manual.

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.

Leave a Reply