Preventing Code Execution in the First While Loop Iteration

Because a 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 condition and does not execute any code if the condition is FALSE on the first iteration.

  1. Add a While Loop to the block diagram and drag out a rectangular region.

     Add  Find
  2. Add a Case structure inside the While Loop and drag out a rectangular region inside the While Loop.

     Add  Find
  3. Create a Boolean control to control the VI.
    1. Right-click the selector terminal of the Case structure and select Create Control from the shortcut menu to create a Boolean control.
    2. 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.
  4. Change the behavior of the conditional terminal to Continue if True.
  5. Add the Random Number (0-1) function inside the True case.

     Add  Find
  6. Add a waveform chart to the front panel.

     Add  Find
  7. 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.

Complete the following steps to verify that the While Loop operates correctly and to run the VI.

  1. Examine the front panel. Notice that the default value of the Boolean control is OFF.
  2. Click the Run button a few times to run the VI. Notice that the chart does not display any values. The While Loop executes once, but the Case structure does not execute while the Boolean control is set to OFF.
  3. With the Operating tool, click the Boolean button to ON.
  4. Click the Run button. The While Loop executes continuously, generating random numbers and displaying them on the chart.
  5. Click the Boolean button to stop the VI.