You are here

While Loop

26 July, 2019 - 09:47
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at

The next program will generate Fahrenheit values and convert them to Celsius until a condition is met to stop the iterations in a While Loop. In the Block Diagram window, select the While Loop structure by clicking on it from the Functions » Programming » Structures menu.

Click and drag to create the While Loop structure.

Figure 1.56 While Loop Structure 
Figure 1.57 Creating a While Loop 
Figure 1.58 While Loop 

In the Front Panel window, create two numeric output arrays. Label them Fahrenheit and Celsius.

Figure 1.59 Numeric Output Arrays 

Re-arrange the diagram as in While Loop Diagram .

Figure 1.60 While Loop Diagram 

From the Functions menu, select Multiply function and a couple of numeric constants. Type in 20.0 and 300.0 for the numeric constants. Select the Fahrenheit to and drop it inside the While Loop. Re-arrange the diagram to look like Generating Fahrenheit Values

Figure 1.61 Generating Fahrenheit Values  

From the Functions » Programming » Comparison menu select the Greater or Equal operator.

Figure 1.62 Greater or Equal Function

Wire the While Loop components as shown in Generating Fahrenheit Values & Stop Condition.

Figure 1.63 Generating Fahrenheit Values & Stop Condition  

Wire the output of the Multiply operation to theFahrenheit and the output of the Fahrenheit to Celsius function to the Celsius numeric output arrays. The connections between the While Loop and the Fahrenheit andCelsius arrays are broken (see Broken Wires).

Figure 1.64 Broken Wires 

To repair the broken connections, roll over the mouse pointer to the Loop Tunnel.

Figure 1.65 Loop Tunnel  

Right click on the Loop Tunnel and select Enable Indexing from the pop-up menu.

Figure 1.66 [/topic/body/fig/title/title {"- topic/title "}) Enable Loop Indexing (title] 

This enables values to accumulate and store the results into an array. Repeat for the Celsius array.

Figure 1.67 Broken Wire Repaired  

Each iteration of the While Loop in this program generates an i ×20 Fahrenheit value and converts it to Celsius. The While Loop stops iterating when the generated Fahrenheit value is greater than or equal to 300. The resulting arrays are stored in the Fahrenheit and Celsius numeric output arrays.

Save the program as Fahrenheit to Celsius While and run it. The program generates the following results:

Figure 1.68 Fahrenheit to Celsius While Loop 

Fahrenheit {0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 240, 260, 280, 300}

Celsius {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333, 104.444, 115.556, 126.667, 137.778, 148.889}

Figure 1.69 Fahrenheit and Celsius Arrays