This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

compile level else statement

I'm not sure if this would work but I thought I'd bounce this off you guys anyway.

So, I have an if/elseif/else statement and cut out the else part to cut down on code space. I thought about leaving it in and only compiling this section if I set a certain bit using #define statements. For example, my code started off like this:

unsigned char variable;

if(variable==1)
{
        //Condition 1
}
else if(variable==2)
{
        //Condition 2
}
//else
//{
//      //Condition 3
//}

I wanted to include Condition 3, but only if I set the #define bit so now my code looks like this:

#define definecondition 1
unsigned char variable;

if(variable==1)
{
        //Condition 1
}
else if(variable==2)
{
        //Condition 2
}
#if(definecondition==1)
{
        else
        {
                //Condition 3
        }
}
#endif

It turns out the way I am trying to implement this does not compile due to the else statement currently within a separate if statement. Now, I can still do this by using:

#if(definecondition==1)
{
        if((variable!=1)&&(variable!=2))
        {
                //Condition 3
        }
}
#endif

but I was curious if there was a way to use the #define statement to still be able to work with the previous if statement (as demonstrated above). I tried using a #else statement but that didn't work either (it was worth a shot). Any other methods that might work out? I can force it to work using my method but if there's a better way, I'd like to know.

Thanks, guys!

Parents
  • I am trying to keep my code as small as possible. Depending on the version of code I am running, I enable and disable features. In the past, I've kept everything in the code and just switched options on and off in runtime. Due to size constraints, depending on the code version, I've commented parts of the code in and out, which is tedious and unnecessary.

    What I'm trying to do is use statements such as:

    #define definecondition 1
    

    in order to change the code that is being compiled (effectively reducing the size of the compiled code) just by changing a single bit (either setting define condition as 1 or 0).

    In the example I gave, I want to be able to provide Condition 3 only when definecondition is equal to 1 via the else command I gave (in my previous post, I gave an alternative way I can do this but I am interested in adding an else statement or something equivalent. I'm not trying to find an alternative logic as described in my first post). I tried adding an else statement that would only be present if definecondition is equal to 1 (as mentioned in my previous post) but the code I was using didn't work.

    So, that said, my question was: Is there any way to create a code else statement that is compiled only is a #define condition is met?

Reply
  • I am trying to keep my code as small as possible. Depending on the version of code I am running, I enable and disable features. In the past, I've kept everything in the code and just switched options on and off in runtime. Due to size constraints, depending on the code version, I've commented parts of the code in and out, which is tedious and unnecessary.

    What I'm trying to do is use statements such as:

    #define definecondition 1
    

    in order to change the code that is being compiled (effectively reducing the size of the compiled code) just by changing a single bit (either setting define condition as 1 or 0).

    In the example I gave, I want to be able to provide Condition 3 only when definecondition is equal to 1 via the else command I gave (in my previous post, I gave an alternative way I can do this but I am interested in adding an else statement or something equivalent. I'm not trying to find an alternative logic as described in my first post). I tried adding an else statement that would only be present if definecondition is equal to 1 (as mentioned in my previous post) but the code I was using didn't work.

    So, that said, my question was: Is there any way to create a code else statement that is compiled only is a #define condition is met?

Children