0

Currently, I have a code that relies on the concept of having to use either an increment or decrement for the for loop depending on a certain condition. Example would be two tact switches for Arduino. One would trigger the positive increment and the other for the negative increment.

int inc; //Increment that is +1 or -1
int limit; //Minimum value for decrement and maximum value for increment

void loop() {
    if(CONDITION) { //Condition for positive increment
        i = 1;
        limit = 15;

        for(int i = 0; i <= limit; i = i + inc) {
            //Statements
        }
    } else { //Condition for negative increment
        i = -1;
        limit = 0;
        for(int i = 0; i >= limit; i = i + inc) {
            //Statements
        }
    }
}

I want to make a uniform code for both situations where you just change a parameter, such as the variable. Now I have no problem with changing the increment. Just set a variable that becomes positive or negative 1. The same goes for the minimum and maximum values. My problem is the comparison operator.

Now I've done some searching myself but can't quite understand how to apply what I've read for this situation I've mentioned here. I will reference the links here.

Is there a way to make a comparison operator a variable?

Are Variable Operators Possible?

My big issue is none of the content in those links as far as from what I can recall have applied a comparison operator inside a loop such as for. Is there a way to create a variable operator of sorts for the situation I have stated? My goal is to be able to remove the for statements inside the if else conditions I have created and move it outside. I would instead put inside the if else conditions the code that would allow me to set the comparison operators <= and >=. Here is an idea of what I would want it to look like.

int inc; //Increment that is +1 or -1
int limit; //Minimum value for decrement and maximum value for increment

void loop() {
    if(CONDITION) { //Condition for positive increment
        i = 1;
        limit = 15;
        /*
        (Code to make the operator <=)
        Wrong code technically but the concept would be
        VariableOperator = <=
        */
    } else { //Condition for negative increment
        i = -1;
        limit = 0;
        /*
        (Code to make the operator >=)
        Wrong code technically but the concept would be
        VariableOperator = >=
        */
    }
    
    for(int i = 0; /* i VariableOperator limit */; i = i + inc) {
        //Statements
    }
}

Though this is just a concept because I have no idea on how it would work. The code would probably be different. I really don't have an idea.

Casey
  • 10,297
  • 11
  • 59
  • 88
AndroidV11
  • 101
  • 2
  • 1
    I'm guessing from the code presented and from the mention of Arduino that you're asking about C/C++. Is that correct? The questions you linked to are about JavaScript. Tagging your question correctly will help it get seen by people who know the subject matter. – CrazyChucky Apr 28 '21 at 02:58
  • Yes, I am asking about C/C++ and Arduino. Is it wrong to search or ask about this concept here? If so, where do I appropriately search and ask? Thank you, I would delete this post here if needed or let it get closed. – AndroidV11 Apr 28 '21 at 03:01
  • It seems fine to ask about here, but I'd recommend editing your question and adding tags for C++ and Arduino. (And probably removing var, too... that's not even a keyword in C++, which was part of why I was confused abut the language.) – CrazyChucky Apr 28 '21 at 03:03
  • Which part do you think should be edited? Is there anything not clear or could be improved from what I am trying to ask? – AndroidV11 Apr 28 '21 at 03:06
  • I'm not a C++ person myself, so I can't advise. I just meant to add the tags, which you've done, so good job and good luck. – CrazyChucky Apr 28 '21 at 03:07
  • You should decide which language you are using. C and C++ are two separate languages. Stop using the term "C/C++" as if it's a single language and then tagging both. You are almost certainly only targeting _one_ of these with whatever compiler you're using. – paddy Apr 28 '21 at 03:09
  • I've removed the C tag because I am referencing Arduino. Thanks for the note! – AndroidV11 Apr 28 '21 at 03:11
  • Does `if(signal from switch A) { increment } else if(signal from switch B) { decrement }` not work? (Checking of course that both are not being pressed at the same time) – Casey Apr 28 '21 at 03:19

1 Answers1

2

There are many ways you could do this in C++, with varying complexity.

But the very simplest and clearest (in my opinion) is:

const int limit = 15;
for (int i = 0; i <= limit; ++i)
{
    int index = (inc > 0 ? i : limit - i);
    // Now use index instead of i
}

I have made a bit of a stretch here, assuming that all you want to do is loop 16 times (either counting from 0 to 15 or 15 to 0). It wasn't completely clear from your code, because it was full of errors and you never actually described in simple terms what you want.

Please note that your non-standard indentation style makes your code quite difficult to read. Maybe you like Python, but this is not how we do it in C++. Edit: question has since been edited to fix indentation, but I will leave this comment for the benefit of author.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • Yes, I do want to loop 16 times. Thank you for expressing that in simple terms. Also, I don't really know what the standard indentation style is. Thanks for pointing that out. I don't understand how the code inside the index works though. How does it make a code for incrementing and decrementing uniform? Not familiar with ? and : codes. – AndroidV11 Apr 28 '21 at 03:40
  • It is basically saying "if inc > 0 then (i) else (limit - i)". So here, if inc is positive, then you'll have `index = i`, otherwise you will have `index = limit - i`. This is just simple math that computes the value you want instead of messing with your loops to achieve it. This way, you have just one loop, which means less code duplication. – paddy Apr 28 '21 at 03:42
  • 1
    Equally, if `inc` is actually not required, but invented purely by you while attempting to solve this, then consider just a boolean: `bool forward = CONDITION;` -- then in the loop: `int index = (forward ? i : limit - i);` – paddy Apr 28 '21 at 03:50