Why is the program NOT stuck in an infinite loop?

103 Views Asked by At

Noob question here. I've just started to learn programming, and I've taken the cs50 course. I've done the first 3 problems from the problem set 1 in C, but I don't understand why in my 3rd program, there is a loop that should cause the program to get stuck, but it doesn't, and it works as intended. The last else if statement shouldn't cause the program to get stuck in a loop? If for example we reach the last loop with a value of 4. So change(4) - nickel(5) would result to -1, which is lower than 0, making the condition true, why doesn't the program get stuck in this last loop, and keep on substracting the value of nickel and increasing the count of pennys?

int main()
{
    int change, count_p = 0, count_n = 0, count_d = 0, count_q = 0;
    const int penny = 1;
    const int nickel = 5;
    const int dime = 10;
    const int quarter = 25;

    do
    {
        change = get_int("Enter amount of change: ");
    }
    while (change < 0);

    while (change > 0)
    {
        if (change - quarter >= 0)
        {
            change -= quarter;
            count_q += 1;
        }
        else if ((change - quarter < 0) && (change - dime >= 0))
        {
            change -= dime;
            count_d += 1;
        }
        else if ((change - dime < 0) && (change - nickel >= 0))
        {
            change -= nickel;
            count_n += 1;
        }
        else if (change - nickel < 0)
        {
            change -= penny;
            count_p += 1;
        }
    }
    // int count_tot = count_q + count_d + count_n + count_p;
    // printf("This is the total amount of coins: %i\n", count_tot);
    printf("This is number of quarters: %i\n", count_q);
    printf("This is number of dimes: %i\n", count_d);
    printf("This is number of nickels: %i\n", count_n);
    printf("This is number of pennys: %i\n", count_p);
}

2

There are 2 best solutions below

1
Chris On

I can't see anything that would cause an infinite loop. Your last condition will always be true if the preceding conditions were false.

But you can clean up your conditions:

int main() {
    int change, count_p = 0, count_n = 0, count_d = 0, count_q = 0;
    const int penny = 1;
    const int nickel = 5;
    const int dime = 10;
    const int quarter = 25;

    do {
        change = get_int("Enter amount of change: ");
    } while (change < 0);

    while (change > 0) {
        if (change >= quarter) {
            change -= quarter;
            count_q += 1;
        }
        else if (change >= dime) {
            change -= dime;
            count_d += 1;
        }
        else if (change >= nickel) {
            change -= nickel;
            count_n += 1;
        }
        else {
            change -= penny;
            count_p += 1;
        }
    }

    printf("This is number of quarters: %i\n", count_q);
    printf("This is number of dimes: %i\n", count_d);
    printf("This is number of nickels: %i\n", count_n);
    printf("This is number of pennys: %i\n", count_p);
}

Secondly, since repeated subtraction is really just division and then we want to test the remainder of the change...

int main() {
    int change, count_p = 0, count_n = 0, count_d = 0, count_q = 0;
    const int penny = 1;
    const int nickel = 5;
    const int dime = 10;
    const int quarter = 25;

    do {
        change = get_int("Enter amount of change: ");
    } while (change < 0);

    count_q = change / quarter;
    change %= quarter;
    count_d = change / dime;
    change %= dime;
    count_n = change / nickel;
    change %= nickel;
    count_p = change;

    printf("This is number of quarters: %i\n", count_q);
    printf("This is number of dimes: %i\n", count_d);
    printf("This is number of nickels: %i\n", count_n);
    printf("This is number of pennys: %i\n", count_p);
}
0
Jean-Baptiste Yunès On

In every condition you have something like change -= something. This means that on every loop change decreases stricly. The while loop condition is change>0, thus at some time either change==0 or change<0 and this will break the loop.