Arduino 中的邏輯非運算\n


使用 ! 運算子執行邏輯非運算。真值表如下 −

表示式輸出
TF
FT

如你所見,邏輯非運算會反轉表示式的真值。

示例

可以從下面給出的示例中理解用法 −

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   Serial.println();

   int i = 10;

   if (!(i > 10)) {
      Serial.println("i is NOT greater than 10");
   }
   else {
      Serial.println("i is greater than 10");
   }
}

void loop() {
   // put your main code here, to run repeatedly:
}

輸出

序列監視器輸出如下所示 −

在等待某些事情變為真時通常使用 NOT 函式。

while(!condition){
   //do something
}

例如,如果你在等待序列輸入,

while(!Serial.available()){
   //Wait
}

Serial.available() 返回大於 0 的值(即接收到序列輸入時),上面的迴圈將中斷。

更新於: 2021 年 5 月 31 日

3000+ 次瀏覽

開啟你的 職業 生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.