dimanche 7 février 2021

While loop is not checking the condition, is not terminating

The while loop is not checking the condition and it is not terminating. How can I rearrange the code?

int capacity = 2;
java_files = 2;
python_files = 2;
c_files = 2;
while (capacity > 0)
{
    if (java_files > 0)
    {
        java_files--;
        capacity--;
        // capacity = 1 now
    }
    if (python_files > 0)
    {
        python_files--;
        capacity--;
        // capacity = 0 now -> the loop has to be terminated, but still continues
    }
    if (c_files > 0)
    {
        c_files--;
        capacity--;
        // capacity = -1 now
    }
}

Aucun commentaire:

Enregistrer un commentaire