Go Down

Topic: How to use a "?" when coding for servo control (Read 644 times) previous topic - next topic

Refiki

Hello,

I'm trying to figure out what a bit of code does.  I will paste it below and highlight the line that is confusing me.  I found this bit of code on the lynxmotion forums and I can't get a hold of anyone who can explain it to me.  I understand the purpose of everything generally BUT the glowing yellow portion.  What does that question mark do?

{
    int Delta = NewPosition - aiLastPositions[iServo];
    int CycleTime = 20;
    int CntOfCycles = Time / 20; 
    int DeltaPerCycle = Delta/CntOfCycles;
   
    if (DeltaPerCycle  == 0)
    {
      // Not enough change to do it so...
      DeltaPerCycle = (Delta > 0)?1 : -1;
      CntOfCycles = abs(Delta);
      CycleTime = Time/CntOfCycles;
    }


Wyldhunt

That's a fancy way of writing an 'if' statement.
DeltaPerCycle = (Delta > 0)?1 : -1;
That line is the same as:

Code: [Select]
if (Delta > 0){
  DeltaPerCycle = 1;
}
else{
  DeltaPerCycle = -1;
}

AWOL

Quote
That's a fancy succinct and potentially more flexible way of writing an 'if' statement

Refiki

Thanks everyone, that really helps me analyze this code better!

Go Up