1

Hi I started learning Java and am currently trying to learn C++. I have this piece of code and cannot workout what it does. I am assuming it makes the program wait for a certain period of time before it starts. But some further explanation would very useful.

I have added comments to sections for which I would like some further explanation.

for (;;) {
        wait (0.02); //What does this do?

        if (ab1_On) {
            con += 104;
            ab1_On = 0; //Why is the value reset to 0?
        }
        if (ab2_On) {
            con += 208;
            ab2_On = 0; //Why is the value reset to 0?
        }

        con++;
        if (con > 311) {
            con -= 312;
        }
        for (int i=0; i<3; i++) {
            bright[i] = brilvl (con + (i * 104));
        }
    }
}
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
user5647516
  • 1,083
  • 1
  • 9
  • 19
  • 4
    `wait` there is not a part of standard C++. Probably it is defined somewhere in program you are trying to figure out. About variables: there is to little context and variables are not example of good naming, but it seems that code is "_turning off_" these variables, so they would yield false on next iteration. – Revolver_Ocelot Jan 30 '16 at 19:21
  • This is horrible code. Unless there's a good reason for figuring out what it does, don't waste your time with it. – Pete Becker Jan 30 '16 at 19:54
  • @PeteBecker basically it is code on an mbed device, I did not post the entire code. From what i can understand ab1 and ab2 are switches and they pass their value to con when they are switched on. But i cannot see the reason for the wait(); function – user5647516 Jan 30 '16 at 20:00

1 Answers1

2

wait() is a function defined in the mbed SDK.

https://developer.mbed.org/handbook/Wait

In your program wait(0.02) will block execution for 20 milliseconds.

for (;;) is an infinite loop, it will run forever. The wait() may be being used to prevent the effects of switch bounce if ab1_on and ab2_on are being set by some mechanical switch.

Stephen Paulger
  • 5,204
  • 3
  • 28
  • 46