如何在if条件中声明将C三元语句转换为if / else?

时间:2017-03-27 12:31:28

标签: c if-statement microcontroller ternary-operator

因此,在查看大规模的互联网后,我无法找到答案。

说我有这段代码:

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++;
            }
        }
    }

我如何重写这个,以便if(P4IN & GPIO_PIN1?0:1)不是这样写的。我不介意创建额外的if / else条件或扩展这段代码(用于MSP432)

感谢您的时间。

1 个答案:

答案 0 :(得分:7)

你可以简化整个过程:

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

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