Preventing Code Execution in the First While Loop Iteration

Because the VI checks the conditional terminal at the end of each iteration, the While Loop always executes at least one time.

Complete the following steps to create a While Loop that tests the conditional terminal and does not execute any code if the condition is FALSE on the first iteration.

  1. Place a Functions»Structures»While Loop on the block diagram and drag out a rectangular region.
  2. Place a Functions»Structures»Case structure inside the While Loop and drag out a rectangular region inside the While Loop.
  3. Right-click the selector terminal of the Case structure and select Create Control to create a Boolean control.
  4. Wire the Boolean control to the conditional terminal of the While Loop. To keep the wiring neat, move the conditional terminal below the Boolean control and create a wire branch from the existing wire segment.
  5. Click the increment or decrement arrows at the top of the Case structure to switch to the TRUE case if the conditional terminal is Continue If True or switch to the FALSE case if the conditional terminal is Stop If True.
  6. Place the Functions»Numeric»Random Number (0-1) function inside the case you selected.
  7. On the front panel, create a chart by selecting Controls»Graph»Waveform Chart.
  8. On the block diagram, wire the Random Number (0-1) function to the waveform chart. Make sure that both the function and the chart terminal are inside the case you selected.
  9. Return to the front panel. Notice that the default value of the Boolean control is OFF.
  10. Click the Run button a few times to run the VI. Notice that the chart does not display any values. The While Loop does not execute while the Boolean control is set to OFF.
  11. With the Operating tool, click the Boolean button to ON.
  12. Click the Run button. The While Loop executes continuously, generating random numbers and displaying them on the chart.
  13. Click the Boolean button to stop the VI.