Whats does this code do?

Hello,

i am new to aduino and programming and i do not understand what this part of the code do?

Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

What is the ? operator, i try to search on google but i can not find it, maybe i searched wrong or do not know the right pronouncing of the operators.
the same with the : operator.
Serial.Println is clear to me

maybe someone is so nice and would explain the code part and the operators or how to search on google.

background, i want to know if the connection was sucessful and based on this an if statement.

Will this work:

if (mpu.testConnection()== TRUE) {
  //do something
}

Or do i have to write:

if (mpu.testConnection()== F("MPU6050 connection successful") {
  //do something
}

Thanks and nice day

It's an inline if-statement, basically.

To write it out:


if (mp.testConnection())
{
  Serial.println(F("MPU6050 connection successful"));
}
else
{
  Serial.println(F("MPU6050 connection failed"));
}

This does exactly the same.

Yes.

No, that doesn't make sense.

keep in mind the outcome variants should be the same type

Thanks, for your answer!!!!

Oh perfect, nice link.
Thanks, i could not find it, now it's clear.

Thx

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.