lundi 4 janvier 2016

Why does this code work with if statement but not while loop?

public void timerCallback()
{
    if (count < 8)
    {
        System.out.println("My timer woke up!");
        this.setOutputState(this.pinNumber, this.pinState);
        this.pinState = !this.pinState;
        this.setTimer(this.timerDelay);
        count++;
    }else
     {
        this.stopMonitoring();
     }
}

That works in the sense that it prints the statement (with the delay) 8 times then terminates the program. Now this:

public void timerCallback()
{
    while (count < 8)
    {
        System.out.println("My timer woke up!");
        this.setOutputState(this.pinNumber, this.pinState);
        this.pinState = !this.pinState;
        this.setTimer(this.timerDelay);
        count++;
    } 
        this.stopMonitoring();
}

That code just prints the statement 8 times at once, then terminates. Why is that?

Aucun commentaire:

Enregistrer un commentaire