Milltronics USA, Inc.
Published

Which Is Better For Looping—IF Or WHILE?

With custom macro B, there are two logic words that can be used for creating loops—the two types of loops are called IF and WHILE statement loops. So which one should you use for your applications?

Share

Leaders-In background

With custom macro B, there are two logic words that can be used for creating loops—the two types of loops are called IF and WHILE statement loops. Everything that can be done with one type of loop can be done with the other, and for most applications, there is no obvious advantage to either. So which one should you use for your applications?

I’ll begin to answer this question with a quick example of each. In each case, the loop will simply count to ten (no other activities).

IF statement loop that counts to ten:

.
#101 = 1 (Initialize counter)
N1 IF [#101 GT 10.0] GOTO 99 (Test if finished)
.
.
#101 = #101 + 1 (Step counter)
GOTO 1 (Go back to test)
N99…
.

While statement loop that counts to ten:

.
#101 = 1 (Initialize counter)
WHILE [#101 LT 10] DO 1 (Beginning of loop)
.
.
#101 = #101 + 1 (Step counter)
END 1 (End of loop)
.

As you can see, there isn’t much of a difference between these two loops. Both loops require a counter to be initialized before the loop starts and stepped before the loop ends. Both require a test at the beginning of the loop to determine whether the loop is finished.

I’ve always taught people to use the IF statement loop. It was my feeling that because it is no simpler to set up a WHILE statement loop—and because people are already familiar with the IF statement from other applications (it is used any time conditional branching is required)—programmers usually find it easier to learn and use the IF statement loop.

Additionally, there is a limit to how deep you can nest WHILE statement loops. You can nest them up to three deep (DO1, DO2, D3, END3, END2, END 1). There is no such limitation to the IF statement loop. Admittedly, applications that require nesting more than three deep are few and far between—but this just contributed to my feeling that using the IF statement loop is better.

However, I was not aware of one important advantage to using the WHILE statement loop. A WHILE statement loop will execute much faster than an IF statement loop in applications where the loop is placed many commands into a program. Consider, for example, a loop placed at the end of a very long program.

With an IF statement loop, the GOTO statement at the end of the loop tells the control to go back to the N word preceding the IF statement. To do so, the control must go all the way back to the beginning of the program and start searching for the N word from there. The greater the distance from the program’s beginning to the N word, the longer it will take to find. Even with newer machines, it is not unusual to experience a noticeable pause during this time (0.5 second or more).

With older controls, you may not experience any improvement with a WHILE statement loop. But with newer controls, a WHILE statement loop will nearly eliminate the noticeable pause. It is my understanding that the END command at the end of the loop will cause the control to search backward to find the corresponding DO command, which dramatically shortens the search time.

So which looping type should you use? For most applications, it probably doesn’t matter. Use the one you are most comfortable with. But if you do use IF statement loops, be alert for times when the control pauses after each execution. For these applications, you should probably switch to a WHILE statement loop.

Milltronics INSPIRE Control
Campro USA
Okuma LB2000 EX III MYW
VERISURF
YCM Alliance
RazorStar
Have it all with Mazak Ez Series Machines
Techspex
Paperless Parts machine shop software
JTEKT
Metal Forming Complex Parts
SolidCAM

Read Next

5 Rules of Thumb for Buying CNC Machine Tools

Use these tips to carefully plan your machine tool purchases and to avoid regretting your decision later.

Read More
Workforce Development

Building Out a Foundation for Student Machinists

Autodesk and Haas have teamed up to produce an introductory course for students that covers the basics of CAD, CAM and CNC while providing them with a portfolio part.

Read More
CNC & Machine Controls

Setting Up the Building Blocks for a Digital Factory

Woodward Inc. spent over a year developing an API to connect machines to its digital factory. Caron Engineering’s MiConnect has cut most of this process while also granting the shop greater access to machine information.

Read More
Milltronics INSPIRE Control