darkmagic0xx
darkmagic0xx

Reputation: 3

How to convert a C ternary statement into an if/else if declared within an if condition?

So after looking on the massive interweb, I was unable to find an answer to this.

Say I have this piece of code:

if(P4IN & GPIO_PIN1?0:1){
        if (state==1){
            state = 0;
            //Wait for this state to pass -- ends up saving the current state on button press.
            while (counter < 10000){
                counter++;
            }
        }
        else{
            state = 1;
            while (counter < 10000){
                counter++;
            }
        }
    }

How would I rewrite this so that if(P4IN & GPIO_PIN1?0:1) is not written like this. I do not mind creating extra if/else conditions or extending this block of code (intended for the MSP432)

Thanks for your time.

Upvotes: 0

Views: 94

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50883

You can simplify the whole thing to this:

if (!(P4IN & GPIO_PIN1)) {
  state = !state;

  while (counter < 10000) {
    counter++;
  }
}

Upvotes: 7

Related Questions