Jump to content


Photo

What is wrong with this Statement? Code: beware..


  • Please log in to reply
1 reply to this topic

#1 Chris lund

Chris lund

    Advanced Member

  • Members
  • PipPipPip
  • 567 posts
  • LocationWestcliffe Colorado

Posted 14 April 2016 - 02:19 AM

if (topoType == (FZ_OBJT_TOPO_OWRE || FZ_OBJT_TOPO_CWRE || FZ_OBJT_TOPO_SURF) && LengthAbs_v_Perc== 0)

 

the Compiler says it is an invalid cast.    But doesn't everything cast to a boolean any way??

 

I played a little bit with O-o-Ops but get the same result.

 

I end up having to be more explicit

if ((topoType == FZ_OBJT_TOPO_OWRE || topoType == FZ_OBJT_TOPO_CWRE ||topoType == FZ_OBJT_TOPO_SURF ) && LengthAbs_v_Perc== 0)

 

Is there a shorter way?

 

Thanks!


Edited by Chris lund, 14 April 2016 - 02:20 AM.

  • ritawex likes this

Christopher Lund

Neurascenic - Industrial Design


#2 Pobo

Pobo

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 17 June 2016 - 06:58 PM

Surely what you're writing here first would be invalid in any programming language?

 

Basically you are comparing a bunch of values, resulting in true if they are and false if you don't. In your case they are topotypes but it could be anything really. As a thought experiment when you substitute integers for topotypes, I find it becomes easier to see that there is something strange going on:

 

if ( 1 == ( 2 || 3 || 4) && LengthAbs_v_Perc == 0)

 

The compiler will try to evaluate the ( 2 || 3 || 4) first as it lives between brackets but there isn't really an operation to evaluate there.

 

The other one you write makes more sense

if ((1 == 2 || 1 == 3 || 1 == 4) && LengthAbs_v_Perc == 0)

 

I don't think there is any way of writing it shorter. There is no shorter syntax and you can't shorten the variable names as they are predefined. If it's just about readability you could consider doing the comparisons before the if statement (but you already knew that):

 

in pseudocode:

bool comparison1 = topoType == FZ_OBJT_TOPO_OWRE

bool comparison2 = topoType == FZ_OBJT_TOPO_CWRE

bool comparison3 = topoType == FZ_OBJT_TOPO_SURF

 

if ((comparison1 || comparison2 || comparison3) && LengthAbs_v_Perc == 0)

{

//

}

 

 

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users