LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

for loop structure

Solved!
Go to solution

I have an inherited program that I am trying to make sense of.  I don't understand the snippet below.  He initiates a for loop, but there is no code contained by curly braces following it.  Is it just assumed that the value array assignation line is the only code to be executed 9999 times?

 

for (i=0;i<=9999;i++)
value[i]=0.0;
ProcessSystemEvents ();
start_pressure = 0.0;
valve_discharged = 0;
if (test_aborted == 1)
break;
WriteToDigitalPort (1, "0", 8, 1, 0x00);
SetPower (OFF);
ps_set_ok = SetPowerSupplyVoltage (discharge_voltage, 0.1, 10.0, SET_VOLTAGE_ON);

if (ps_set_ok == 1)
{
DIG_Out_Line (1, 0, 4, 1);

0 Kudos
Message 1 of 4
(1,174 Views)
Solution
Accepted by topic author Jim_Marihew

Yes.

 

If there is no enclosed "{...}" pair following a FOR, only the next line is repeated. In this case, that's "value[i] = 0.0". So the loop just sets value[0 - 9999] to zero.

 

A better way to do this is using "memset".

0 Kudos
Message 2 of 4
(1,151 Views)

Thanks

0 Kudos
Message 3 of 4
(1,148 Views)

You're right: if no braces are present then the first line following the for is executed in the loop.

It may not be evident at a first glance, so I normally either put the braces even in this case or put the statement on the same line of the loop to make things clear.

 

That for is equivalent to 

Set1D (value, 10000, 0.0);

which makes use of an Analysis library function which in my opinion is even more clear for initializations.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 4
(1,113 Views)