LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

if else statement brackets

Hi, I'm using Labwindows CVI 8.0.1.  I have multiple, nearly identical test stations all using the same CVI project for my source code.  I have just put a released version of the code on a new station and for some reason on this new station it does not execute if else statements properly if they do not have brackets.  In other words, in all cases where the programmer put statements such as these:
if(var == 1) return 1;
else return 0;
 
or
 
if(a == 5) b = 3;
else if (a == 2) b = 2;
else b = 1;
 
These statements will compile and execute but  for some reason it's as if the else statements don't exist.  If var does not equal 1, the else part is just skipped, same with else if and the else in the second statement.
 
I have tried recompiling and rebuilding all of the code and it still does it.  I've looked at the build options and compared them on each station and they all look identical.
 
I'm about ready to re-ghost the hard drive and hope that takes care of the problem.
Message 1 of 5
(5,405 Views)
Hello AceUM21,

I just want to verify a few things about your post.

How are you knowing if your program is skipping the else and if else statements?  You said this is in release mode so you cannot be stepping through your code, so I just am wondering how you are verifying this.

Also, just to check, if you use brackets, you say the behavior is changed?

Do you have a small example of code that you can reproduce the problem with that you could post to this forum?
Kristen
National Instruments
Message 2 of 5
(5,379 Views)

Sorry to resurrect a very old post.  But I too have run into a situation where an entire else condition block was not executed.  It's as if the compiler has optimized it away?  But that doesn't make much sense to me.

 

In my case, I have braces in my else block, because I'm executing compound statements.  But I did not have braces in my if / else if preceding blocks.  Here's what I did initially:

 

if (fVoltage < vSettingLo || fVoltage > vSettingHi)
	if (++unitRetry[slot] >= MAX_UNIT_TRIES)
	{
		GoStopUnit(bank,unit,VFAIL);	// now fail the unit
		unitRetry[slot] = 0;				// reset the retry counter
	}
else if (fCurrent < iSettingLo || fCurrent > iSettingHi)
	if (++unitRetry[slot] >= MAX_UNIT_TRIES)
	{
		GoStopUnit(bank,unit,IFAIL);	// now fail the unit
		unitRetry[slot] = 0;				// reset the retry counter
	}
else  
{
	// update the meters
	SetCtrlAttribute(panel,GetCtrlArrayItem(serialArray,slot),ATTR_TEXT_COLOR,VAL_DK_GREEN);
	SetCtrlAttribute(panel,GetCtrlArrayItem(snArray,slot),ATTR_TEXT_COLOR,VAL_DK_GREEN);
	SetCtrlAttribute(panel,GetCtrlArrayItem(fvoltageArray,slot),ATTR_TEXT_BGCOLOR,VAL_DK_GREEN);
	SetCtrlAttribute(panel,GetCtrlArrayItem(fcurrentArray,slot),ATTR_TEXT_BGCOLOR,VAL_DK_GREEN);
}  

 

In my view, since the if and the else if conditions on executed one line (a nested if block), that shouldn't have affected my last else block.  But it did!  This is repeatable.  The last else block gets ignored.  The solution is to put braces around the nested if blocks in the first if and in the next else if conditions.  Then my last else gets left in by the compiler.

 

Odd!

 

 

Message 3 of 5
(4,938 Views)

Well, it seems logic to me, instead! Let me rephrase your code a little:


if ( -1- )
	if ( -2- )
	{
	}
else if ( -3- )
	if ( -4- )
	{
	}
else ( -5- ) 
{
}  

 

It seems to me that regardless the indentation, if #3 is treated as the else condition of if #2. Similarly, condition #5 is treated as the else of if #4. That is: if the signal is within boundaries, the code does nothing! Smiley Surprised

A proper set of braces on ifs #1 and #3 permits to execute block #5 is signals are correct.



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?
Message 4 of 5
(4,934 Views)

Yes, Roberto, you are correct. Apparently, this is a revelation of my lack of ANSI C knowledge.  😉

 

Somewhere along my professional life, I had made the assumption that the line immediately following a conditional statement was executed if that conditional resolved TRUE.  And compound statements were supported with braces. For instance, this is correct given this example:

 

if (i)
   temp++;
else if (j)
   temp--;

 As you said, indentations are for humans.  But now I see the logic problem with my assumption.  How does the compiler know which if goes with which else?

Message 5 of 5
(4,919 Views)