04-12-2023 07:30 AM
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);
Solved! Go to Solution.
04-12-2023 11:45 AM
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".
04-12-2023 11:51 AM
Thanks
04-13-2023 02:32 AM
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.